943,844 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 19529
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 23rd, 2006
2

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

Click to Expand / Collapse  Quote originally posted by Diode ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Sep 26th, 2006
1

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

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
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Sep 26th, 2006
0

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

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.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 31st, 2006
0

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

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.
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Sep 2nd, 2008
0

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

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.
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Dec 21st, 2008
0

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

Click to Expand / Collapse  Quote originally posted by WaltP ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chandangang is offline Offline
3 posts
since Dec 2008
Dec 21st, 2008
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Dec 23rd, 2008
0

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

thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chandangang is offline Offline
3 posts
since Dec 2008
Feb 5th, 2010
-1
Re: How do you check if arrow keys are pressed?
****(1)*****
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6. char c;
  7. clrscr();
  8. printf("Press \"Esc\" to exit");
  9. gotoxy(1,10);
  10. c=getch();
  11. while(c!=27)
  12. {
  13. if(c==0)
  14. {
  15. c=getch();
  16. gotoxy(1,10);
  17. clreol();
  18. printf("This key has two ascii values: 0 and %d",c);
  19. }
  20. else
  21. {
  22. gotoxy(1,10);
  23. clreol();
  24. printf("%d",c);
  25. }
  26. c=getch();
  27. }
  28. }
************

*****(2)******
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6. char c;
  7. int x=1,y=1;
  8. clrscr();
  9. for(y=1;y<17;y++)
  10. {
  11. gotoxy(52,y);
  12. printf("|");
  13. }
  14. gotoxy(1,16);
  15. printf("___________________________________________________/");
  16. gotoxy(1,19);
  17. printf("Press \"Esc\" to exit\n\n");
  18. printf("Use arrow keys to move cursor");
  19. x=1;
  20. y=1;
  21. gotoxy(x,y);
  22. c=getch();
  23. while(c!=27)
  24. {
  25. if(c==0)
  26. {
  27. c=getch();
  28. if(c==77)
  29. {
  30. if(x==51)
  31. {
  32. x=1;
  33. gotoxy(x,y);
  34. }
  35. else
  36. {
  37. x++;
  38. gotoxy(x,y);
  39. }
  40. }
  41.  
  42. if(c==75)
  43. {
  44. if(x==1)
  45. {
  46. x=51;
  47. gotoxy(x,y);
  48. }
  49. else
  50. {
  51. x--;
  52. gotoxy(x,y);
  53. }
  54. }
  55.  
  56. if(c==80)
  57. {
  58. if(y==15)
  59. {
  60. y=1;
  61. gotoxy(x,y);
  62. }
  63. else
  64. {
  65. y++;
  66. gotoxy(x,y);
  67. }
  68. }
  69.  
  70. if(c==72)
  71. {
  72. if(y==1)
  73. {
  74. y=15;
  75. gotoxy(x,y);
  76. }
  77. else
  78. {
  79. y--;
  80. gotoxy(x,y);
  81. }
  82. }
  83. }
  84.  
  85. else if(c==8)
  86. {
  87. if(x==1)
  88. {
  89. x=51;
  90. gotoxy(x,y);
  91. }
  92. else
  93. {
  94. x--;
  95. gotoxy(x,y);
  96. }
  97. }
  98.  
  99. else
  100. {
  101. if(x==51)
  102. {
  103. if(y==15)
  104. {
  105. y--;
  106. }
  107. x=x-50;
  108. y++;
  109. gotoxy(x,y);
  110. }
  111. else
  112. {
  113. printf("%c",c);
  114. x++;
  115. }
  116. }
  117. c=getch();
  118. }
  119. }
************

Compiler: Borland's Turbo C++ (Version 3.0)

Special keys(arrow keys,function keys,delete,home,page up,end,....etc) have ascii values but
they have TWO ascii values, and thats why we have to use getch() TWICE
to sense them.
I have NOT used kbhit(), but I got the idea of using getch()
TWICE for special keys from WaltP's post, Thanx WaltP.
Last edited by hsp700; Feb 5th, 2010 at 7:33 am.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
hsp700 is offline Offline
11 posts
since Feb 2010
Feb 5th, 2010
0
Re: How do you check if arrow keys are pressed?
Add.......
  1. else if(c==13)
  2. {
  3. gotoxy(x,y);
  4. }

****(2)*****
  1. void main()
  2. {
  3. ...
  4. ...
  5. while(c!=27)
  6. {
  7. if(c==0)
  8. {
  9. }
  10.  
  11. else if(c==8)
  12. {
  13. }
  14.  
  15. >> H E R E <<
  16.  
  17. else
  18. {
  19. }
  20. c=getch();
  21. }
  22. }

...before running the code, Thank you.
Last edited by hsp700; Feb 5th, 2010 at 9:01 am.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
hsp700 is offline Offline
11 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reversering a user's input integer
Next Thread in C Forum Timeline: status access violation in BST





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC