Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Key event repeating input.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
facultyofmusic




PostPosted: Sat Jul 25, 2009 9:47 am   Post subject: Key event repeating input.

I'm making this Guitar Hero game, and the problem I found with my code is that the user can just keep on pressing the right node key and the stroke key [Enter] so get all the nodes. So I came up with a solution. When the user presses the stoke key, it turns boolean variable [stroked] true, and then after about 0.1 seconds I'll turn it back to false. Seemed pretty logical to me. So I did that, but soon to find that the keyDown method starts to repeat the input after about half a second. I don't know exactly what this is called. If your confused, you know when you open something like Microsoft word, you hold down the "a" key. It will output an "a", and then after about half a second it starts to repeatedly outputting "a" until you release the key. This function of the keyDown method makes my attempt trying to turn the stroked variable back to false useless.

Is there a way to stop it from repeatedly triggering? or is there another method that does the same thing as the keyDown method but without the repeatedly triggering after about half a second?

I'm just starting java on my own and I'm not very good at these things, so if you can help this situation please do.

Thank you so so much.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Jul 26, 2009 12:28 pm   Post subject: RE:Key event repeating input.

Keys have 4 states. Two obvious ones are "up" and "down". Two others are transitional edges: "up-down", and "down-up". That is, the frame that key changes it's state between up and down. You want one of those latter ones.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
facultyofmusic




PostPosted: Sun Jul 26, 2009 1:09 pm   Post subject: Re: RE:Key event repeating input.

Tony @ 26th July, 12:28 pm wrote:
Keys have 4 states. Two obvious ones are "up" and "down". Two others are transitional edges: "up-down", and "down-up". That is, the frame that key changes it's state between up and down. You want one of those latter ones.


I don't quite understand what you (Tony) meant by "up-down" and "down-up". Is there a method that does this, or is it some kind of advanced programming concept thingy Confused . If I'm not asking for too much, is it possible to post maybe an example source code so that I can probably understand it from that?
Tony




PostPosted: Sun Jul 26, 2009 1:16 pm   Post subject: RE:Key event repeating input.

Instead of looking at just what they key is right now, you also look at what they key was last time you checked it.
code:

loop
   old_keys = now_keys
   now_keys = read_keys()
   state(old_keys, now_keys)
end

Previously you were using just "now_keys", a single boolean variable (up/down). When you use two boolean variables, there are now 4 possible states.

up, up -- key is up
up, down -- key has just been pressed down
down, down -- key is down
down, up -- key has just been lifted up

The transitional states may sometimes be referred to as "on_key_down" and "on_key_up", if your software has event driven callbacks.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
facultyofmusic




PostPosted: Sun Jul 26, 2009 1:46 pm   Post subject: Re: Key event repeating input.

So basically I need to have a loop running separately (i donno, in a Thread?) and watches for the down-up state? Ok, it sorta makes sense to me. But is there a way to disable the keyDown method from repeating input? something like "KeyEvent.setKeyDownRepeat (false);" or something like that?
Tony




PostPosted: Sun Jul 26, 2009 2:10 pm   Post subject: Re: Key event repeating input.

facultyofmusic @ Sun Jul 26, 2009 1:46 pm wrote:
So basically I need to have a loop running separately

No, use the same loop.
facultyofmusic @ Sun Jul 26, 2009 1:46 pm wrote:
is there a way to disable the keyDown method from repeating input?

No, that's an OS thing.

FYI, if you are using the KeyEvent, you could just read the docs. http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html (or find one appropriate for your version of Java used).
Quote:

"Key pressed" and "key released" events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don't generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the getKeyCode method, which returns a virtual key code.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: