• Post Reply Bookmark Topic Watch Topic
  • New Topic

"real" keyreleased events?

 
Gilbert Powers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have searched high and low for this answer.
Does anyone know how to catch "real" key pressed or key released events?
I am trying to write an app, that has a fastforward and rewind action. I need to know how long they have held down the button.
Java for some reason, enlighten me please, sends the key press and key released any type a key is pressed...the names are quite misleading to me.
I have tried many different things and nothing i have tried works. I am so close to finishing the alpha stage of my app, and i ran into this wall.
Greatly appreciated for an answer,
Gil
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Gilbert.
It seems to me that two JToogleButtons could do the job. Say the one titled "Forward" is pressed, listen that ActionEvent, check the state of the source of the event with AbstractButton.isSelected and find out when it happended by ActionEvent.getWhen. Just subtract the timestamps of two hits of the button.
Alternatively listen for ItemEvent.SELECTED and ItemEvent.DESELECTED. Because there is no getWhen in ItemEvent, compute the timestamps yourself.
 
Gilbert Powers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might try something like that.
What makes all this so tricky is that this application will not have that buttoon. THis application is remote control driven. There are no buttons to press. You press a button on a remote for x amount of time and that is how much u ffwd or rew. I am writing this for keyboard before i just to remote. BUt maybe i could rigg some kind of state pattern up or something, man this is tricky.
 
Gilbert Powers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the key events are OS dependent. Because some guy found the answer for me that works in windows, however it does not work in linux.
Can anyone tell me if this is true.
Here is a link to the post i have over at java.sun.com
real key event
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I remember reading recently (maybe it was in Developing Games in Java by David Brackeen unfortunately this book is at home and I am at work...) that keyPressed() and keyReleased() work differently on Linux and Windows because of the way the OSes handle key repeats. If you hold a button down, multiple key events get fired. On windows you keyPressed event for every repeat, but a keyReleased event only when you actually pick up your finger. On Linux you get a keyPressed and a keyReleased for every key repeat.
 
Gilbert Powers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you know of any way, to get the keyreleased keypressed working like it does in windows for linux.
The code that was posted at java.sun.com link above. It works great in windows, it does exactly what i want, but not in linux because of continous events fired.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!