how to detect arrow keys??

Reply

Join Date: Aug 2008
Posts: 2
Reputation: mail2shrid is an unknown quantity at this point 
Solved Threads: 0
mail2shrid mail2shrid is offline Offline
Newbie Poster

how to detect arrow keys??

 
0
  #1
Aug 4th, 2008
If i'm scanning from keyboard using getch(), how do i check which arrow key has the user pressed??(Arrow keys have ASCII values same as some other keys so we can't directly check for ASCII values)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how to detect arrow keys??

 
0
  #2
Aug 4th, 2008
That would depend on your operating system and compiler.

So which do you have?
Try to be more specific than "windows" and "borland", as there are many versions of each.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: mail2shrid is an unknown quantity at this point 
Solved Threads: 0
mail2shrid mail2shrid is offline Offline
Newbie Poster

Re: how to detect arrow keys??

 
0
  #3
Aug 5th, 2008
Originally Posted by Salem View Post
That would depend on your operating system and compiler.

So which do you have?
Try to be more specific than "windows" and "borland", as there are many versions of each.
I'm using DOS and the compiler is turboc..
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to detect arrow keys??

 
0
  #4
Aug 5th, 2008
Try this simplest program, look at these codes on your screen:
  1. #include <stdio.h"
  2. #include <conio.h>
  3. /* Press Esc to quit */
  4. #define ESC 27
  5. int main()
  6. {
  7. int ch;
  8. while ((ch=getch()) != ESC)
  9. {
  10. printf("%d",ch);
  11. while (kbhit())
  12. {
  13. printf(" %d",getch());
  14. }
  15. printf("\n");
  16. }
  17. printf("ESC %d\n",ch);
  18. return 0;
  19. }
Special keys are presented by two codes, as usually the 1st is 0 (zero).
So you need two getch call to accept these keys.
Take a pen and a piece of paper, start the program then press your favorite keys...

This program works not only with unfading Turbo C...

Good luck!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: how to detect arrow keys??

 
0
  #5
Aug 5th, 2008
Originally Posted by mail2shrid View Post
I'm using DOS and the compiler is turboc..
This program only works in Turbo C.
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main(void)
  5. {
  6. unsigned char key, key2;
  7.  
  8. while( (key=getch()) != '\r' )
  9. {
  10. if (key == 0)
  11. {
  12. key2 = getch();
  13. switch(key2)
  14. {
  15. case 72:
  16. printf("up "); break;
  17. case 75:
  18. printf("left "); break;
  19. case 77:
  20. printf("right "); break;
  21. case 80:
  22. printf("down "); break;
  23. default: break;
  24. }
  25. }
  26. }
  27. return 0;
  28. }
Last edited by Colin Mac; Aug 5th, 2008 at 10:03 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to detect arrow keys??

 
0
  #6
Aug 5th, 2008
Look at this wonderful table:
http://www.jimprice.com/jim-asc.shtml#keycodes
Google is your friend ...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: how to detect arrow keys??

 
0
  #7
Aug 6th, 2008
Actually, if you need to simply accept input, you don't need kbhit() at all:
  1. c2 = 0; // reset c2 to 'nothing'
  2. c1 = getch(); // wait for a key
  3. if (c1 == 0) // check if it's a function key
  4. {
  5. c2 = getch(); // it is, get the function value
  6. }
  7. // if c2 is not 0, a function key was pressed

kbhit() is useful when you want the program to continuously run and when a key is pressed do something different. For example:
  1. if (kbhit()) // check to see if a key was pressed
  2. { // a key was pressed so we interrupt the standard
  3. // programming cycle
  4. c2 = 0; // reset c2 to 'nothing'
  5. c1 = getch();
  6. if (c1 == 0)
  7. {
  8. c2 = getch();
  9. }
  10. // if c2 is not 0, a function key was pressed
  11. ...
  12. }
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: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to detect arrow keys??

 
0
  #8
Aug 6th, 2008
About kbhit(): look at my program example then try to print the second byte of a special key without kbhit() at the same line...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: how to detect arrow keys??

 
0
  #9
Aug 7th, 2008
Originally Posted by ArkM View Post
About kbhit(): look at my program example ...
I did, and don't see why you need a kbhit() for that. A simple test for 0 would work.
Originally Posted by ArkM View Post
... then try to print the second byte of a special key without kbhit() at the same line...
Simple without the kbhit() . Look at my code and make the simple change...
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: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to detect arrow keys??

 
0
  #10
Aug 7th, 2008
Alas, not all special keys have 0 as a 1st byte of getch() pair (F11, F12 on Windows XP in console applications, for example)...
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC