How do you check if arrow keys are pressed?

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

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

How do you check if arrow keys are pressed?

 
1
  #1
Sep 21st, 2006
I know they don't have standard ASCII values, but how do you check for them?

I am making a command prompt-type interface for a text-based RPG a friend and I are making, and I would like to be able to use the arrow keys to navigate in menus.

I am using getch(); in a do-while loop. The exiting condition of the loop is the user hits ENTER. This loop just keeps putting chars (gotten from getch() ) into a char array, with 0D hex or '\n' being the char that the loop stops on. Just a real simple loop. I included the following line into my developmental program to see what the values are of the keys that you press:

  1. printf("%c %d ", str[num], str[num]);

and when the UP arrow key is pressed, this line displays " 0 H 72", with " 0 H" being the value of the UP arrow key. This apparently doesn't look like it will fit into a char, so how would I code for it? Because I wrote a function that displayed the length of the string passed to it and it said the string length was 0 characters long. It looks like the UP arrow key has the value of 72, but this is incorrect, because capital h has this decimal value, which leads me to believe that there are possibly NULL characters in the " 0 H", because my function that returns the length of the string counts '\0' (or NULL) as the end of the string. At face value, this looks like the "string" is 4 characters long.

But if the first 'char' in it is not a space, and instead, actually a NULL character, then it would return 0 characters long just like it is doing.

So what do you recommend me doing to check to see if the user hits an arrow key? I would like it to work just like the command prompt in DOS or Windows...when the user hits left or right, it takes them left or right one character in the text they have entered on the line. And when they hit up or down, it takes them up or down one command in their command history.

Thanks,
Diode
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
  #2
Sep 21st, 2006
Oops, I quickly realized that I was inaccurate in stating that the UP arrow key contained 4 characters. I forgot about that line of code I supplied which includes spaces and values.

The UP arrow key really contains: two characters. The first character is a NULL space, or '\0', and the second character is a capital h, 'H'.

Sorry for the mix up.

Diode
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?

 
1
  #3
Sep 22nd, 2006
Nevermind, I figured it out.

But thanks anyway
Diode
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
  #4
Sep 22nd, 2006
Wait, wait! How did you do it, Diode?
(I don't mean to hijack the thread, but this is more efficient than posting
another thread asking the same thing, only to have Diode answer the question)
Last edited by FireSBurnsmuP; Sep 22nd, 2006 at 3:29 pm. Reason: disclaimer, I don't like ruffling the mods' feathers
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?

 
1
  #5
Sep 23rd, 2006
Ok let me whip up a simple program to demonstrate it. No point in pasting all my hundreds of lines of code for one trivial matter.

It will take me a little while to make a simple program but I will post it in a short while.


Diode
Reply With Quote Quick reply to this message  
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?

 
1
  #6
Sep 23rd, 2006
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.
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: 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
  #7
Sep 23rd, 2006
This compiled with no errors on Borland C++ 5 and gcc. So this should compile on yours.

  1. /******************************* 0026.c *******************************/
  2. /* */
  3. /* Date: September 22, 2006 */
  4. /* Author: diode */
  5. /* ndweiler@mchsi.com */
  6. /* Purpose: To practice checking for arrow keys being pressed */
  7. /* in a header file */
  8. /* */
  9. /**********************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <conio.h>
  14. #include <string.h>
  15. #include <process.h>
  16. #include <ctype.h>
  17.  
  18. #define STRING_LENGTH 100
  19. #define ARROW_KEY_NONE 0
  20. #define ARROW_KEY_UP 1
  21. #define ARROW_KEY_DOWN 2
  22. #define ARROW_KEY_LEFT 3
  23. #define ARROW_KEY_RIGHT 4
  24. #define ARROW_KEY_POSSIBLE 5
  25.  
  26. // BEGINNING OF PROTOTYPES
  27.  
  28. void settext(void);
  29. void getline(char * str2);
  30. void showoutput(char * str2);
  31. int checkarrowkey(char key);
  32.  
  33. // BEGINNING OF PROGRAM
  34.  
  35. int main()
  36. {
  37. char strexit[STRING_LENGTH];
  38. char strquit[STRING_LENGTH];
  39. char holdtextline[STRING_LENGTH];
  40.  
  41. system("cls");
  42. holdtextline[0] = '\0';
  43. getline(holdtextline);
  44. showoutput(holdtextline);
  45. printf("\n");
  46.  
  47. return 0;
  48. }
  49. // END OF PROGRAM
  50.  
  51. // BEGINNING OF FUNCTIONS
  52.  
  53. void settext(void)
  54. {
  55. char text;
  56. text = getch(); // holds the output on screen
  57. }
  58.  
  59. void getline(char * str2)
  60. {
  61. char str1[STRING_LENGTH];
  62. int num = 0;
  63. int checkkey = 0; // used to check for special keys
  64.  
  65. printf("Enter your text:"); // instructs you to enter something
  66.  
  67. do // starts loop for input
  68. {
  69. if (num == STRING_LENGTH)
  70. {
  71. break; // exits when the string fills up
  72. }
  73. else
  74. {
  75. str1[num] = getch(); // puts a char into 'str'
  76. }
  77.  
  78. if ((str1[num] == '\b') && (num <= 0)) // starts the backspace handler
  79. { // if you're at the beginning...
  80. // do nothing
  81. }
  82. else // but if everything's alright...
  83. {
  84.  
  85. /////////////////////////////////////////////////////////////////
  86. // This is where I check to see if the user typed an arrow
  87. /////////////////////////////////////////////////////////////////
  88. if((str1[num] == '\0') && (num >= 0))
  89. {
  90. num++;
  91. goto last;
  92. }
  93. else
  94. {
  95. if((num > 0) && (str1[num - 1] == '\0') && (str1[num] != '\0'))
  96. {
  97. checkkey = checkarrowkey(str1[num]);
  98.  
  99. switch(checkkey)
  100. {
  101. case ARROW_KEY_UP:
  102. printf("\nYou pressed the UP arrow key!");
  103. return;
  104.  
  105. case ARROW_KEY_DOWN:
  106. printf("\nYou pressed the DOWN arrow key!");
  107. return;
  108.  
  109. case ARROW_KEY_LEFT:
  110. printf("\nYou pressed the LEFT arrow key!");
  111. return;
  112.  
  113. case ARROW_KEY_RIGHT:
  114. printf("\nYou pressed the RIGHT arrow key!");
  115. return;
  116.  
  117. default:
  118. return;
  119. }
  120. }
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////
  124. // This ends where I check to see if the user typed an arrow
  125. /////////////////////////////////////////////////////////////////
  126.  
  127. printf("%c", str1[num]); // echoes your char to the screen
  128. }
  129.  
  130. str1[num] = tolower(str1[num]); // converts it to lowercase
  131.  
  132. if ((str1[num] == '\b') && (num >= 1)) // removes the last-typed char
  133. { // from the screen and from the
  134. printf(" \b"); // string
  135. str1[num - 1] = '\0'; //
  136. num = num - 1; //
  137. }
  138. else
  139. {
  140. if (str1[num] != '\b') // if everything is alright...
  141. { //
  142. num++; // it increments 'num' by 1
  143. }
  144. }
  145.  
  146. last:
  147.  
  148. } while(str1[num-1] != 0x0D); // loops until ENTER is hit
  149.  
  150. str1[num - 1] = '\0'; // create the NULL string char
  151.  
  152. strcpy(str2, str1); // copy 'str1' to 'str2'
  153. }
  154.  
  155. void showoutput(char * str2) // beginning of 'showoutput'
  156. {
  157. int length = strlen(str2); // sets 'length' to the length of
  158. // the string 'str2'
  159.  
  160. printf("\n\nYour text was '%s' ", str2); // outputs 'str2' to screen
  161.  
  162.  
  163. printf("\n'%s' is %d characters long",// outputs 'str2' and its
  164. str2, length); // length to the screen
  165. }
  166.  
  167. int checkarrowkey(char key)
  168. {
  169. int whichkey = 0;
  170. char str1 = '\0';
  171. //char str2[STRING_LENGTH];
  172.  
  173. str1 = key;
  174.  
  175. switch(str1)
  176. {
  177. case 'H':
  178. whichkey = ARROW_KEY_UP;
  179. return whichkey;
  180.  
  181. case 'P':
  182. whichkey = ARROW_KEY_DOWN;
  183. return whichkey;
  184.  
  185. case 'K':
  186. whichkey = ARROW_KEY_LEFT;
  187. return whichkey;
  188.  
  189. case 'M':
  190. whichkey = ARROW_KEY_RIGHT;
  191. return whichkey;
  192.  
  193. default:
  194. return 0;
  195. }
  196. }

I hope this was helpful to some people.

Cheers,
Diode
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
  #8
Sep 23rd, 2006
Oops, I forgot to delete the #define ARROW_KEY_NONE and #define ARROW_KEY_POSSIBLE constant declarations. This was when I was fooling around trying to figure it out, and just forgot to delete it.



Diode
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
  #9
Sep 23rd, 2006
Oh and in regards to the title of my file, that is not how many programs I attempted before I figured it out, lol! It is the amount of programs I have compiled since Monday. Just noticed that I included that as well. Heh.



Diode
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?

 
1
  #10
Sep 23rd, 2006
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.

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.

Thanks,
Diode
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: 10665 | 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