| | |
Reading Scan Codes from the Keyboard
Introduction
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
Notes:
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.Notes:
- 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.
C Syntax (Toggle Plain Text)
/* 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 #include <iostream> #include <conio.h> using namespace std; void main() { char KeyStroke; cout << "Press Escape to quit." << endl; do { KeyStroke = getch(); if (KeyStroke == 0) { KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke switch (KeyStroke) { case PAGE_UP: cout << "PAGE UP" << endl; break; case PAGE_DOWN: cout << "PAGE DOWN" << endl; break; case HOME: cout << "HOME" << endl; break; case END: cout << "END" << endl; break; case UP_ARROW: cout << "UP ARROW" << endl; break; case DOWN_ARROW: cout << "DOWN ARROW" << endl; break; case LEFT_ARROW: cout << "LEFT_ARROW" << endl; break; case RIGHT_ARROW: cout << "RIGHT_ARROW" << endl; break; case F1: cout << "F1" << endl; break; case F2: cout << "F2" << endl; break; case F3: cout << "F3" << endl; break; case F4: cout << "F4" << endl; break; case F5: cout << "F5" << endl; break; case F6: cout << "F6" << endl; break; case F7: cout << "F7" << endl; break; case F8: cout << "F8" << endl; break; case F9: cout << "F9" << endl; break; case F10: cout << "F10" << endl; break; default: cout << "Some other key." << endl; } } else 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.
1
•
•
•
•
One modification for those under vista/visual 2008:
thk you for the ideea. :hat off:
C Syntax (Toggle Plain Text)
KeyStroke =getch(); if (KeyStroke == -32) //weird, i know.... but it works { KeyStroke = getch(); ...}
0
•
•
•
•
-1
•
•
•
•
This "code" has no sense at all (case, case, case... horrible !!!!)
It's done in 1 line of code with Win32 api (VK)
It's done in 1 line of code with Win32 api (VK)
Similar Threads
- Reading input from the keyboard? (Java)
- Reading from keyboard in linux (Assembly)
- Problem with scan codes. (C)
- Reading keyboard from background process (C++)
- reading operators from the keyboard (C#)
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming stack standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi



