• Post Reply Bookmark Topic Watch Topic
  • New Topic

listen to different keys simultaneously  RSS feed

 
kedar kanade
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have written a key listener for the keypressed event. the problem is that when a key is pressed and held the listener does not respond to any other key pressed. i want different keys recognized independently and simultaneously.how should i use multithreading and event handling in this case?
kindly note that i am using awt package in an applet.
waiting for your reply
kedar
 
Rob Whelan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Kedar -
Threads won't help you -- because the AWT thread is the one responsible for handing out all of the key events -- but I think what you're trying to do is quite possible.
Look at the KeyListener interface again. If you implement the keyPressed and keyReleased methods, you will be able to figure it out when one key is pressed and another is also pressed before the first is released.
Dealing with the modifier keys (i.e., Ctrl, Shift, Alt) is even easier. InputEvent (the superclass of KeyEvent) has methods isShiftDown(), isAltDown() and so on.
One caveat here: I tested this in WinNT, JDK 1.2. Obviously, there might be bugs in other configurations (i.e., Java 1.1 in an applet) that could prevent it from working properly...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!