Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Keystrokes: physically-pressed-event needed

Thread view: 
Simon - 01 Mar 2006 15:47 GMT
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
Josh Falter - 02 Mar 2006 14:03 GMT
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));
}
Simon - 02 Mar 2006 14:46 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.