Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Detect when user presses arrow key

Tags:

I have been having a problem with detecting arrow key presses in my C++ console application. I have tried everything I have found, both here and on other tutorial sites, but all of them give me the same thing whenever I press the arrow:

Process returned 0 <0x0> execution time : 2.249 s Press any key to continue. 

Here are all the methods of detecting the key press that I have tried, all ending up the same way. These are the only two left in my code, the others I attempted I deleted instead of commenting out.

Method one:

c1 = getch(); if(c1 == 0) {      c2 = getch();      if(c2 == 72) {cout << endl << "Up Arrow" << endl;}     else if(c2 == 80) {cout << endl << "Down Arrow" << endl;}     else{cout << endl << "Incorrect Input" << endl;}  } 

Method two:

switch(getch()) { case 65:        cout << endl << "Up" << endl;//key up     break; case 66:     cout << endl << "Down" << endl;   // key down     break; case 67:     cout << endl << "Right" << endl;  // key right     break; case 68:     cout << endl << "Left" << endl;  // key left     break; } 

Is there some error in my code which made me go back to my main method, or did it skip over some code? Is there a faster way to do this? I'm almost 100% sure that my other code doesn't have anything to do with this problem, because I isolated the code from be dependent on any other aspect of the program, and I kept having the same problem.

Again, I tried every method of getting the arrow key press that I could find, and I keep getting the same problem. If it matters, I'm on a Windows 8 Samsung ATIV Smart PC and using the keyboard dock.

Thanks in advance for any help.

like image 680
SplatFace Development Avatar asked Jul 12 '14 01:07

SplatFace Development


People also ask

How do you check if a key is pressed in C?

kbhit() is present in conio. h and used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file “conio. h”.

How check if up arrow is pressed in C++?

This is because to determine if an arrow key was pressed, we use ASCII value 224, which can only be stored in an 8-bit character (unsigned char) and not the 7-bit signed char. You can use below code snippet. 2 types of inputs are processed here. ch1 is the 1st character that user enters.

What happens when Up arrow key is pressed?

First practice moving the cursor around in the file with the arrow keys. Use the up and down arrow keys to move the cursor up and down in the file.

How do I know which arrow key to press?

Conclusion. We can detect arrow key presses by listening to the keydown event. And in the event listener, we can either check the key or keydown properties of the event object to see which key is pressed. And there you have it.


2 Answers

#include <conio.h> #include <iostream> using namespace std;  #define KEY_UP 72 #define KEY_DOWN 80 #define KEY_LEFT 75 #define KEY_RIGHT 77  int main() {     int c = 0;     while(1)     {         c = 0;          switch((c=getch())) {         case KEY_UP:             cout << endl << "Up" << endl;//key up             break;         case KEY_DOWN:             cout << endl << "Down" << endl;   // key down             break;         case KEY_LEFT:             cout << endl << "Left" << endl;  // key left             break;         case KEY_RIGHT:             cout << endl << "Right" << endl;  // key right             break;         default:             cout << endl << "null" << endl;  // not arrow             break;         }      }      return 0; } 

output like this:

Up  Down  Right  Left  Up  Left  Right  Right  Up 

detected arrow key press!

like image 105
arbboter Avatar answered Nov 11 '22 07:11

arbboter


The previous answer by arbboter is close but neglects the fact the arrow keys (and other special keys) return a scan code of two characters. The first is either (0) or (224) indicating the key is an extended one; the second contains the scan code value.

Without accounting for this, the ASCII values for "H", "K", "M", and "P" are misinterpreted as "Up", "Down", "Left", and "Right".

Here's a modified version of arbboter's code to demonstrate reading the extended value when one of the arrow keys is pressed:

#include <conio.h> #include <iostream> using namespace std;  #define KEY_UP    72 #define KEY_LEFT  75 #define KEY_RIGHT 77 #define KEY_DOWN  80  int main() {     int c, ex;      while(1)     {         c = getch();          if (c && c != 224)         {             cout << endl << "Not arrow: " << (char) c << endl;         }         else         {             switch(ex = getch())             {                 case KEY_UP     /* H */:                     cout << endl << "Up" << endl;//key up                     break;                 case KEY_DOWN   /* K */:                     cout << endl << "Down" << endl;   // key down                     break;                 case KEY_LEFT   /* M */:                     cout << endl << "Left" << endl;  // key left                     break;                 case KEY_RIGHT: /* P */                     cout << endl << "Right" << endl;  // key right                     break;                 default:                     cout << endl << (char) ex << endl;  // not arrow                     break;             }         }     }      return 0; } 
like image 42
Erik Anderson Avatar answered Nov 11 '22 07:11

Erik Anderson