-1

I have been trying to make a class called MyKey with a function called getValue() that returns an int. For example, I want it to return a 1 if the up key is pressed, 2 if the down key is pressed, and a zero if neither is pressed. I would like it to work in an instance like this:

if(MyKey.getValue() == 1)
{
   System.out.println("up");
}

(if the above code was in some type of while loop)

The oracle tutorials have only been confusing me even more and I haven't found a request quite like mine on this site. The closest I found was here: How do I check if the user is pressing a key? but that provides a boolean, not an integer, and I could not make it work in the above code after working on it for about an hour.

Thanks!

Community
  • 1
  • 1
tochf
  • 1
  • 2
  • possible duplicate of [How to check if the key pressed was an arrow key in Java KeyListener?](http://stackoverflow.com/questions/616924/how-to-check-if-the-key-pressed-was-an-arrow-key-in-java-keylistener) – jnd Feb 03 '15 at 01:38
  • What if the user is pressing up and down at the same time? – user253751 Feb 03 '15 at 02:08
  • I intended for it to be in that order, so it would return 1. – tochf Feb 03 '15 at 12:34

1 Answers1

0

You have to implement KeyListener,take a look here: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html

More details on how to use it: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html