Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   ASCII value for left and right? (http://www.programmingforums.org/showthread.php?t=10760)

Jakeyman_I_Am Jul 18th, 2006 7:02 PM

ASCII value for left and right?
 
Well, here is the issue. I am expirenced in the world of Java now (that is how my teacher describes it..) and I have yet to work much with KeyEvent. I am trying to make a simple game involving moving a small object (a fillRect object) around on the screen. The problem is, what to I compare for in the KeyEvent method to find what directional arrown was pressed??? Note this is my KeyEvent code, now I just need the value to test for via if statement and I'll have this whole project done.. anyone??? Thanks for your time in advance...

:

public void keyPressed(KeyEvent event)
{
    if(event.getKeyText(event.getKeyCode()) == ?????)  //I don't know what to test for..
    {
               
    }
} //Note that I'm looking for the left, right, up and down keys here..


Sane Jul 18th, 2006 7:13 PM

If my memory serves me: 200, 203, 205, 208. In some arbitrary order. Play around with it a bit. Or try making your program output what key is currently being pressed. You could determine them that way. It may be different in Java than it was in the language these numbers applied to.

Good luck. :)

Jakeyman_I_Am Jul 18th, 2006 7:41 PM

Thank you!

titaniumdecoy Jul 18th, 2006 7:43 PM

You shouldn't compare key codes to plain integers; they make your code difficult to read and maintain.

When in doubt, look it up in the Java docs. In this case, the documentation for KeyEvent is particularly useful. Note the enormous number of static int fields; those are what you should be comparing your KeyEvent to. For example, the following will test whether the key pressed is the up arrow key:

:

if (event.getKeyCode() == KeyEvent.VK_UP) ...

ReggaetonKing Jul 18th, 2006 7:46 PM

:

  1. KeyEvent.VK_LEFT //is for the Left arrow key
  2. KeyEvent.VK_RIGHT //is for the right arrow key

I got it from this using Google. It was the first link. Please search around before posting a question.

Sane Jul 18th, 2006 7:46 PM

Oh, then I presume those are constants representing the integer values I stated? Yes... that would be much better than what I had said. Easier for yourself and other readers. :D

lectricpharaoh Jul 18th, 2006 11:10 PM

Another option, and the better one for a non-trivial program, would be to use sensible defaults (such as the arrow keys), but allow the user to configure the keys. You would start the configuration process, and it would query the user for which physical key to use for each logical input, like "Please press the key to use for UP", or whatever. If you have a large number of logical inputs, you might want to allow the user to click on one from a menu, and then it would prompt them, but for a small number (say, half dozen or so), it's not too onerous to iterate through them all each time the user wants to configure the keys.

For bonus points, you could use other physical inputs to generate the same logical input to your program. For example, you could have arrow buttons as part of your GUI, and the user could either click them or use the assigned arrow keys on the keyboard to control movement. It's only really sensible to restrict the user to a single input method if only one makes sense, or if you're on a resource-constrained system, but in most cases, flexibility is good.

Jakeyman_I_Am Jul 20th, 2006 9:22 AM

Quote:

Originally Posted by reggaeton_king
:

  1. KeyEvent.VK_LEFT //is for the Left arrow key
  2. KeyEvent.VK_RIGHT //is for the right arrow key

I got it from this using Google. It was the first link. Please search around before posting a question.


Thank you for your help. However, I must say that the ProgrammingForums is a awesome resource. You can search these forums and find an answer to almost any question. Now, if everyone searched (in my case it was aprently easy to find the question. Although I did try I did not find it. I guess you have to know what you are looking for in order to find what you are looking for) and found the answer to the apending question, we wouldn't have this amazing resource, would we? If you don't want to help me because you think I didn't search for anything, simply don't respond to the post. Thank you once again for your help! :D

DaWei Jul 20th, 2006 9:44 AM

Everyone doesn't want critical posts in their thread. BS. Live with it. If searching wasn't a recommended procedure, it would be part of the rules. Yes, you have to know what you're seaching for, but it isn't really rocket science. You used the term, "KeyEvent", in your original post. That turns up a plethora of resources. Yes, it's nice to take advantage of this forum. No, it's not nice to take undue advantage by asking people to repeat their answers a zillion times, or to do one's searching for one.

ReggaetonKing Jul 20th, 2006 11:12 AM

@O.P. - Glad I can help and DaWei is right though.


All times are GMT -5. The time now is 12:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC