![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 14
Rep Power: 0
![]() |
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..
__________________
The elder newb. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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. ![]() |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 14
Rep Power: 0
![]() |
Thank you!
__________________
The elder newb. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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) ... |
|
|
|
|
|
#5 |
|
Sexy Programmer
|
Java Syntax (Toggle Plain Text)
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
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.
![]() |
|
|
|
|
|
#7 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,032
Rep Power: 5
![]() |
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Mar 2005
Posts: 14
Rep Power: 0
![]() |
Quote:
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! ![]()
__________________
The elder newb. |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Sexy Programmer
|
@O.P. - Glad I can help and DaWei is right though.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|