C ++ Detect when user presses arrow key

I had a problem with detecting arrow keys in my C ++ console application. I tried everything I found, both here and on other sites, but they all give me the same thing when I click the arrow:

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

Here are all the keystroke detection methods I tried, it all ends the same way. These are just the two remaining in my code, the others that I tried to delete, instead of commenting.

First method:

 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 kind of error in my code that made me return to my main method or skip some code? Is there a faster way to do this? I am almost 100% sure that my other code has nothing to do with this problem, because I isolated the code from dependency on any other aspect of the program, and I had the same problem.

Again, I tried every way to get the arrow key press that I could find, and I have the same problems. If that matters, I am on a Windows 8 Samsung ATIV Smart PC and using a keyboard dock.

Thanks in advance for any help.

+8
c ++ windows-8 console-application arrow-keys
source share
6 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; } 

follow these steps:

 Up Down Right Left Up Left Right Right Up 

Arrow key detected!

+13
source share

Here is an alternative way to do this without getch () using events (well-commented, and I tried to make it as simple as I could)

 #include <iostream> #include <Windows.h> int main(int argc, char *argv[]){ HANDLE rhnd = GetStdHandle(STD_INPUT_HANDLE); // handle to read console DWORD Events = 0; // Event count DWORD EventsRead = 0; // Events read from console bool Running = true; //programs main loop while(Running) { // gets the systems current "event" count GetNumberOfConsoleInputEvents(rhnd, &Events); if(Events != 0){ // if something happened we will handle the events we want // create event buffer the size of how many Events INPUT_RECORD eventBuffer[Events]; // fills the event buffer with the events and saves count in EventsRead ReadConsoleInput(rhnd, eventBuffer, Events, &EventsRead); // loop through the event buffer using the saved count for(DWORD i = 0; i < EventsRead; ++i){ // check if event[i] is a key event && if so is a press not a release if(eventBuffer[i].EventType == KEY_EVENT && eventBuffer[i].Event.KeyEvent.bKeyDown){ // check if the key press was an arrow key switch(eventBuffer[i].Event.KeyEvent.wVirtualKeyCode){ case VK_LEFT: case VK_RIGHT: case VK_UP: case VK_DOWN: // if any arrow key was pressed break here std::cout<< "arrow key pressed.\n"; break; case VK_ESCAPE: // if escape key was pressed end program loop std::cout<< "escape key pressed.\n"; Running = false; break; default: // no handled cases where pressed std::cout<< "key not handled pressed.\n"; break; } } } // end EventsRead loop } } // end program loop return 0; } 

(Thanks to the commentator, I know that this code is not standard, although it will work if you compile g++ , more information in the comments)

+5
source share

Check out http://msdn.microsoft.com/en-us/library/windows/desktop/ms684961(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/ desktop / dd375731 (v = vs. 85) .aspx

 #include<windows.h> #include <stdio.h> int main() { HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); DWORD NumInputs = 0; DWORD InputsRead = 0; bool running = true; INPUT_RECORD irInput; GetNumberOfConsoleInputEvents(hInput, &NumInputs); ReadConsoleInput(hInput, &irInput, 1, &InputsRead); switch(irInput.Event.KeyEvent.wVirtualKeyCode) { case VK_ESCAPE: puts("Escape"); break; case VK_LEFT: puts("Left"); break; case VK_UP: puts("Up"); break; case VK_RIGHT: puts("Right"); break; case VK_DOWN: puts("Down"); break; } } 
+1
source share
 // Example for inputting a single keystroke in C++ on Linux // by Adam Pierce < adam@doctort.org > on http://www.doctort.org/adam/nerd-notes/reading-single-keystroke-on-linux.html // This code is freeware. You are free to copy and modify it any way you like. // Modify by me Putra Kusaeri #include <iostream> #include <termios.h> #define STDIN_FILENO 0 using namespace std; int main() { // Black magic to prevent Linux from buffering keystrokes. struct termios t; tcgetattr(STDIN_FILENO, &t); t.c_lflag &= ~ICANON; tcsetattr(STDIN_FILENO, TCSANOW, &t); // Once the buffering is turned off, the rest is simple. cout << "Enter a character: "; char c,d,e; cin >> c; cin >> d; cin >> e; cout << "\nYour character was "; // Using 3 char type, Cause up down right left consist with 3 character if ((c==27)&&(d=91)) { if (e==65) { cout << "UP";} if (e==66) { cout << "DOWN";} if (e==67) { cout << "RIGHT";} if (e==68) { cout << "LEFT";} } return 0; } 
+1
source share

Arbboter's previous answer is close, but ignores the fact that the arrow keys (and other special keys) return a two-character scan code. The first of these (0) or (224) means that the key is extended; the second contains the scan code value.

Without this in mind, the ASCII values ​​for "H", "K", "M" and "P" are incorrectly interpreted as "Up", "Down", "Left" and "Right".

Here is a modified version of arbboter 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; } 
0
source share

Some of the answers given here do not take into account the fact that when you press the arrow key you get 2 characters. In addition, it should be noted that the input character must be an unsigned character.

You can use the code snippet below. 2 types of input data are processed here. ch1 is the first character the user enters. This is the input that the user enters. But in the case of the arrow keys, a sequence of 2 characters is obtained by ch1 and ch2. ch1 determines that some arrow key has been pressed, ch2 defines a particular arrow key pressed.

 const int KEY_ARROW_CHAR1 = 224; const int KEY_ARROW_UP = 72; const int KEY_ARROW_DOWN = 80; const int KEY_ARROW_LEFT = 75; const int KEY_ARROW_RIGHT = 77; unsigned char ch1 = _getch(); if (ch1 == KEY_ARROW_CHAR1) { // Some Arrow key was pressed, determine which? unsigned char ch2 = _getch(); switch (ch2) { case KEY_ARROW_UP: // code for arrow up cout << "KEY_ARROW_UP" << endl; break; case KEY_ARROW_DOWN: // code for arrow down cout << "KEY_ARROW_DOWN" << endl; break; case KEY_ARROW_LEFT: // code for arrow right cout << "KEY_ARROW_LEFT" << endl; break; case KEY_ARROW_RIGHT: // code for arrow left cout << "KEY_ARROW_RIGHT" << endl; break; } } else { switch (ch1) { // Process other key presses if required. } } 
0
source share

All Articles