How do you check if arrow keys are pressed?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: How do you check if arrow keys are pressed?

 
2
  #11
Sep 23rd, 2006
Originally Posted by Diode View Post
What is the top decimal value that kbhit() catches when you hit a key? I didn't know about kbhit. So I coded the entire thing from scratch. It works. But the UP arrow key for example is " H", that is: NULL, capital h. It is actually 2 characters. Does kbhit() catch 2 characters? And could you provide a sample program that prints on the screen that an arrow key was hit, like I coded my sample program? I am curious about this apparent quick way that you provided.
kbhit() doesn't catch characters. It returns TRUE if a key has been pressed, FALSE if no key is waiting. That way your program can do whatever processing it needs and not wait for a key. In effect it makes the keyboard interrupt driven. Leave out the kbhit() call if you don't need to test for keyboard readyness.

As for a sample program, just output the value of the integers ch1 and ch2.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 236
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: How do you check if arrow keys are pressed?

 
1
  #12
Sep 26th, 2006
Okay, so what libraries are getch() and kbhit() included in? (I think I might test this one out, after I reformat my computer...)
Last edited by FireSBurnsmuP; Sep 26th, 2006 at 1:31 am. Reason: needed different tone
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: How do you check if arrow keys are pressed?

 
0
  #13
Sep 26th, 2006
Originally Posted by FireSBurnsmuP View Post
Okay, so what libraries are getch() and kbhit() included in? (I think I might test this one out, after I reformat my computer...)
In conio.h but this is not standard so there are posibility that you don't have this library.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 236
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: How do you check if arrow keys are pressed?

 
0
  #14
Oct 31st, 2006
diode, I ran your code in Dev-C++, and I found one error, last: didn't have anything happen, so I put in a nonsense integer declaration.

Also, when I ran it, if I press an arrow key, it displays a lowercase alpha, then the letter you defined. It doesn't detect that an arrow key was pressed (it doesn't say, "You pressed the [insert key here] key!"). I don't know, maybe Dev-C++ does something different. I haven't attempted to look through all of the code yet; it's a little late (early?) and I am not that great at C++ yet.
[EDIT] Wait! you used a lot of c libraries, no wonder, I haven't dealt with those at all... [/EDIT]

Anyways, I was hoping maybe you could explain this error I encountered.
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: How do you check if arrow keys are pressed?

 
0
  #15
Sep 2nd, 2008
Originally Posted by FireSBurnsmuP View Post
diode, I ran your code in Dev-C++, and I found one error, last: didn't have anything happen, so I put in a nonsense integer declaration.

Also, when I ran it, if I press an arrow key, it displays a lowercase alpha, then the letter you defined. It doesn't detect that an arrow key was pressed (it doesn't say, "You pressed the [insert key here] key!"). I don't know, maybe Dev-C++ does something different. I haven't attempted to look through all of the code yet; it's a little late (early?) and I am not that great at C++ yet.
[EDIT] Wait! you used a lot of c libraries, no wonder, I haven't dealt with those at all... [/EDIT]

Anyways, I was hoping maybe you could explain this error I encountered.
Sorry, I just now found your reply. I haven't messed with this program for a long time. In fact, I forgot all about it. I've moved on to working with (as much as possible) ANSI standard C, and now I am moving onto C++. Sorry if I wasn't more help.

BTW, about your sig, I didn't know Ozzy Osbourne said that, but that is also on a Magic: the Gathering card, though I forget which one.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: chandangang is an unknown quantity at this point 
Solved Threads: 0
chandangang chandangang is offline Offline
Newbie Poster

Re: How do you check if arrow keys are pressed?

 
0
  #16
Dec 21st, 2008
Originally Posted by WaltP View Post
I usually do it with this:
  1. if (kbhit())
  2. {
  3. ch1 = getch();
  4. if (ch1 != '\0')
  5. {
  6. // process a normal keystroke
  7. }
  8. else
  9. {
  10. ch2 = getch(); // get the arrow or function key
  11. switch(ch2)
  12. {
  13. // process the arrow or function key
  14. }
  15. }
  16. }
You can use this to discover what the key values are.

This only works for compilers that have getch() and kbhit() defined, of course.
please explain your code
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do you check if arrow keys are pressed?

 
0
  #17
Dec 21st, 2008
Originally Posted by chandangang View Post
please explain your code
getch() returns 0 when one of the special keys is pressed, such as one of the Function or arrow keys. When that happens the program has to call getch() again which will return the key code of the key that was pressed. The reason for this behavior is that special keys have the same key codes as other normal keys, and the program needs some way to distinguish them. When I write such programs I normally make those key codes negative values so that the rest of the program can easily distintuish between normal and special keys. Other programmers have added 255 to the value, which works ok too.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: chandangang is an unknown quantity at this point 
Solved Threads: 0
chandangang chandangang is offline Offline
Newbie Poster

Re: How do you check if arrow keys are pressed?

 
0
  #18
Dec 23rd, 2008
thanx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 10667 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC