In java I have a program that needs to check continuously if a user is pressing a key. So In psuedocode, somthing like
if (isPressing("w"))
{
//do somthing
}
Thanks in advance!
In java I have a program that needs to check continuously if a user is pressing a key. So In psuedocode, somthing like
if (isPressing("w"))
{
//do somthing
}
Thanks in advance!
Try this:
In java you don't check if a key is pressed, instead you listen to
KeyEvent
s. The right way to achieve your goal is to register aKeyEventDispatcher
, and implement it to maintain the state of the desired key:Then you can always use:
You can, of course, use same method to implement
isPressing("<some key>")
with a map of keys and their state wrapped insideIsKeyPressed
.You have to implement
KeyListener
,take a look here: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.htmlMore details on how to use it: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html