Hello everyone. This little code snippet shows you how to read in scan codes from the keyboard. It is slightly different than reading in a regular character. When you use the getch() function, it is normally returning an ASCII value. Some keyboards have extra keys though. These include the F1 - F12 function keys and the directional arrows to start. These keys do not have an ASCII code. A char data type is a one byte item. When you press a key that doesn't have an ASCII code, it returns two bytes. If the first byte is 0, then the next byte contains the scan code. In the following code, I include the scan code for many typical keys and how to read them.
Even though there are two calls to getch() , it does not require two keystrokes.
Not all of these scan codes may be 100% accurate for all computers. It may, of course, need some modifications.
/* This program shows how to pick up the scan codes from a keyboard */
/* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/
#define PAGE_UP 73
#define HOME 71
#define END 79
#define PAGE_DOWN 81
#define UP_ARROW 72
#define LEFT_ARROW 75
#define DOWN_ARROW 80
#define RIGHT_ARROW 77
#define F1 59
#define F2 60
#define F3 61
#define F4 62
#define F5 63
#define F6 64
#define F7 65
#define F8 66
#define F9 67
#define F10 68
using namespace std;
cout << "Press Escape to quit." << endl;
KeyStroke = getch();
if (KeyStroke == 0)
KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
cout << "PAGE UP" << endl;
cout << "PAGE DOWN" << endl;
cout << "HOME" << endl;
cout << "END" << endl;
cout << "UP ARROW" << endl;
cout << "DOWN ARROW" << endl;
cout << "LEFT_ARROW" << endl;
cout << "RIGHT_ARROW" << endl;
cout << "F1" << endl;
cout << "F2" << endl;
cout << "F3" << endl;
cout << "F4" << endl;
cout << "F5" << endl;
cout << "F6" << endl;
cout << "F7" << endl;
cout << "F8" << endl;
cout << "F9" << endl;
cout << "F10" << endl;
cout << "Some other key." << endl;
cout << KeyStroke << endl;
while (KeyStroke != 27); // 27 = Escape key
A simple program that shows you how to read the keyboard scan code for keys that don't have a normal ASCII code. It includes typical scan codes for the function keys and and the arrow keys.
I tried this and the left,right,up,down keys are returning aH aP and so on. F1-F9 work fine. So, If I wanted to control a stepper motor, I would need to learn how to use scan keys. Problem is, I need to figure why the scancodes for the arrow keys are not outputting correctly.
The only problem with these changes and the code at all, as you can see, is the conflic with conio.h library and getch(). And thats why when you press arrow keys or F11, F12 appear a letter (p) before it. I still can't resolve this but hope I'll find other solution.
getch() will return either 0 or 224 when you press special keys like F1-F10 and arrow keys. If it doesn't return one of those two values then you just pressed a normal key. After getting either 0 or 224 you have to call getch() again, then convert that value to something with will not duplicate normal keys. I've seen two different methods of conversion, either can be used as long as you use them consistently.
make the keystroke value negative. Such as keystroke = -getch();
Add 255 to the value such as keystroke = getch() + 255;
Run this little program and press several keys to find out how it works.
In your post you said that "getch() will return either 0 or 224 when you press special keys like F1-F10 and arrow keys" and I agree with you.
But how we can see and from your program code for every key there are 2 equal values except for the special keys where the first value is always 224, but the second value is different and unique for every funct. key. For example F11 and F12 have next 2 values: 1st 224 and 2nd 133 for F11 & 224 and 134 for F12.
In my code I am using the second value for my goal in lines 37 - 49. I am sure this code is not correct and thats why I am searching for another solution.
As I said before, when a special key is pressed add the value 255 to the second call to getch() to make the key unique from all other keys on the keyboard. That way your program will always be able to distinguish F1 key from the semicolon, which also has key code of 59.