Keystrokes: physically-pressed-event needed

S

Simon

Hi,

I need to listen to an event that tells me when the user physically presses and
releases a key. I cannot achieve this easily with the normal keyPressed,
keyReleased, or keyTyped-events, since after when a key is pressed for more than
half a second or so I get repeated events (all three) every other millisecond.
Of course I can write an ugly workaround that discards keyReleased events when
they are immediately followed by a keyPressed of the same key. However, this is
not very reliable. Is there a better way? Or can I just switch of this behaviour
from within Java?

Cheers,
Simon
 
J

Josh Falter

This is speculation, but couldn't you set a lock in keyPressed and
unlock it in keyReleased and only accept the value if the lock is not
in use?

Such as

private int currentKeyCode = -1;

public void keyPressed(KeyEvent event)
{
if(currentKeyCode == -1)
{
if(isKeyOfInterest(event.getKeyCode()))
{
currentKeyCode = event.getKeyCode();
//record key value
}
}
}
public void keyReleased(KeyEvent event)
{
if(event.getKeyCode() == currentKeyCode)
{
currentKeyCode = -1;
}
}
private boolean isKeyOfInterest(int keyCode)
{
return((keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9) ||
(keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z));
}
 
S

Simon

Thanks for your answer. I'm not sure if I really understand it correctly. I will
have a try:

For simplicity, assume that the user does not press several keys at once.

This is your code plus some comments from me:

private int currentKeyCode = -1;

public void keyPressed(KeyEvent event)
{
// I believe that this condition always evaluates to true because:
// 1. It evaulates to true the first time this method is called.
// 2. Before the next keyPressed event is sent, we will receive a
// keyReleased event and this will set currentKeyCode = -1
// (see below)
if(currentKeyCode == -1)
{
if(isKeyOfInterest(event.getKeyCode()))
{
currentKeyCode = event.getKeyCode();
//record key value
}
}
}
public void keyReleased(KeyEvent event)
{
// Same here, since:
// 1. this is true the first time keyReleased is called
// 2. If we receive a keyReleased, there must have been a corresponding
keyPressed and this will have set currentKeyCode = e.getKeyCode()
(see above)
if(event.getKeyCode() == currentKeyCode)
{
currentKeyCode = -1;
}
}
private boolean isKeyOfInterest(int keyCode)
{
return((keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9) ||
(keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z));
}



I believe that there is no way of locking, because keyPressed and keyReleased
always come in pairs and are always alternating. When the keyChar of keyPressed
differs from that of the last keyPressed (or the last keyReleased), you can be
sure that the key was really released, but if it is the same you cannot know
since the user may release the key and press it again after some time. Looking
at the timestamps (getWhen()) will tell you that some time elapsed between the
last keyReleased and the next keyPressed, but how long do you have to wait?
10ms? 100ms? Or does strictly > 0ms suffice? Maybe Java guarantees that the
timestamps for keyReleased and keyPressed are equal if they are "soft" events.

Cheers,
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top