Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 18th, 2006, 7:02 PM   #1
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
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.
Jakeyman_I_Am is offline   Reply With Quote
Old Jul 18th, 2006, 7:13 PM   #2
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Jul 18th, 2006, 7:41 PM   #3
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
Thank you!
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jul 18th, 2006, 7:43 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 936
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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) ...
titaniumdecoy is offline   Reply With Quote
Old Jul 18th, 2006, 7:46 PM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Java Syntax (Toggle Plain Text)
  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.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Jul 18th, 2006, 7:46 PM   #6
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Jul 18th, 2006, 11:10 PM   #7
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Jul 20th, 2006, 9:22 AM   #8
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
Quote:
Originally Posted by reggaeton_king
Java Syntax (Toggle Plain Text)
  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!
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jul 20th, 2006, 9:44 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 20th, 2006, 11:12 AM   #10
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
@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!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:56 PM.

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