Reading Scan Codes from the Keyboard

nanodano nanodano is offline Offline May 25th, 2006, 2:51 am |
2
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 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.

  1. /* This program shows how to pick up the scan codes from a keyboard */
  2.  
  3. /* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/
  4. #define PAGE_UP 73
  5. #define HOME 71
  6. #define END 79
  7. #define PAGE_DOWN 81
  8. #define UP_ARROW 72
  9. #define LEFT_ARROW 75
  10. #define DOWN_ARROW 80
  11. #define RIGHT_ARROW 77
  12. #define F1 59
  13. #define F2 60
  14. #define F3 61
  15. #define F4 62
  16. #define F5 63
  17. #define F6 64
  18. #define F7 65
  19. #define F8 66
  20. #define F9 67
  21. #define F10 68
  22. #include <iostream>
  23. #include <conio.h>
  24.  
  25. using namespace std;
  26.  
  27. void main()
  28. {
  29. char KeyStroke;
  30.  
  31. cout << "Press Escape to quit." << endl;
  32.  
  33. do
  34. {
  35. KeyStroke = getch();
  36.  
  37. if (KeyStroke == 0)
  38. {
  39. KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
  40. switch (KeyStroke)
  41. {
  42. case PAGE_UP:
  43. cout << "PAGE UP" << endl;
  44. break;
  45. case PAGE_DOWN:
  46. cout << "PAGE DOWN" << endl;
  47. break;
  48. case HOME:
  49. cout << "HOME" << endl;
  50. break;
  51. case END:
  52. cout << "END" << endl;
  53. break;
  54. case UP_ARROW:
  55. cout << "UP ARROW" << endl;
  56. break;
  57. case DOWN_ARROW:
  58. cout << "DOWN ARROW" << endl;
  59. break;
  60. case LEFT_ARROW:
  61. cout << "LEFT_ARROW" << endl;
  62. break;
  63. case RIGHT_ARROW:
  64. cout << "RIGHT_ARROW" << endl;
  65. break;
  66. case F1:
  67. cout << "F1" << endl;
  68. break;
  69. case F2:
  70. cout << "F2" << endl;
  71. break;
  72. case F3:
  73. cout << "F3" << endl;
  74. break;
  75. case F4:
  76. cout << "F4" << endl;
  77. break;
  78. case F5:
  79. cout << "F5" << endl;
  80. break;
  81. case F6:
  82. cout << "F6" << endl;
  83. break;
  84. case F7:
  85. cout << "F7" << endl;
  86. break;
  87. case F8:
  88. cout << "F8" << endl;
  89. break;
  90. case F9:
  91. cout << "F9" << endl;
  92. break;
  93. case F10:
  94. cout << "F10" << endl;
  95. break;
  96. default:
  97. cout << "Some other key." << endl;
  98. }
  99. }
  100. else
  101. cout << KeyStroke << endl;
  102. }
  103. while (KeyStroke != 27); // 27 = Escape key
  104. }
Quick reply to this message  
C Syntax
  1. 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.
  2.  
1
viper33m viper33m is offline Offline | Apr 14th, 2008
One modification for those under vista/visual 2008:

  1. KeyStroke =getch();
  2. if (KeyStroke == -32) //weird, i know.... but it works
  3. {
  4. KeyStroke = getch();
  5. ...}
thk you for the ideea. :hat off:
 
0
ramthegreatcv ramthegreatcv is offline Offline | 31 Days Ago
thanks a lot..

  1. if (KeyStroke == -32)

works on DevC++ Windows XP.
 
-1
marco93 marco93 is offline Offline | 30 Days Ago
This "code" has no sense at all (case, case, case... horrible !!!!)
It's done in 1 line of code with Win32 api (VK)
 
0
ramthegreatcv ramthegreatcv is offline Offline | 30 Days Ago
@macro93 can u please explain how it can be done using Win32 api?
 
-1
Ancient Dragon Ancient Dragon is offline Offline | 30 Days Ago
win32 api doesn't work on non-Windows platforms, but this code snippet will work with some possible mods to the defines at the top.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC