Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
6.2 KiB

  1. /*
  2. * Copyright (c) Microsoft Corporation
  3. *
  4. * Module Name :
  5. * console.c
  6. *
  7. * Hacks to read characters from the NT console
  8. * Taken from nt\sdktools\vi\nt.c
  9. *
  10. *
  11. * Sadagopan Rajaram -- Nov 5, 1999
  12. *
  13. */
  14. #include "tcclnt.h"
  15. #include "keymap.h"
  16. #define MAX_VK 0x7f
  17. TCHAR RegularTable[] = {ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  18. /* 08 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  19. /* 10 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  20. /* 18 */ ZERO, ZERO, ZERO, ESCP, ZERO, ZERO, ZERO, ZERO,
  21. /* 20 */ ZERO, K_PU, K_PD, K_EN, K_HO, K_LE, K_UP, K_RI,
  22. /* 28 */ K_DO, ZERO, ZERO, ZERO, ZERO, K_IN, K_DE, ZERO,
  23. /* 30 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  24. /* 38 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  25. /* 40 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  26. /* 48 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  27. /* 50 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  28. /* 58 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  29. /* 60 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  30. /* 68 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  31. /* 70 */ K_F1, K_F2, K_F3, K_F4, K_F5, K_F6, K_F7, K_F8,
  32. /* 78 */ K_F9, K_FA, K_FB, K_FC, ZERO, ZERO, ZERO, ZERO};
  33. TCHAR ShiftedTable[] = { ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  34. /* 08 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  35. /* 10 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  36. /* 18 */ ZERO, ZERO, ZERO, ESCP, ZERO, ZERO, ZERO, ZERO,
  37. /* 20 */ ZERO, K_PU, K_PD, K_EN, K_HO, K_LE, K_UP, K_RI,
  38. /* 28 */ K_DO, ZERO, ZERO, ZERO, ZERO, K_IN, K_DE, ZERO,
  39. /* 30 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  40. /* 38 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  41. /* 40 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  42. /* 48 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  43. /* 50 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  44. /* 58 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  45. /* 60 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  46. /* 68 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  47. /* 70 */ K_S1, K_S2, K_S3, K_S4, K_S5, K_S6, K_S7, K_S8,
  48. /* 78 */ K_S9, K_SA, K_SB, K_SC, ZERO, ZERO, ZERO, ZERO};
  49. TCHAR ControlTable[] = { ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  50. /* 08 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  51. /* 10 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  52. /* 18 */ ZERO, ZERO, ZERO, ESCP, ZERO, ZERO, K_CG, ZERO,
  53. /* 20 */ ZERO, K_PU, K_PD, K_EN, K_HO, K_LE, K_UP, K_RI,
  54. /* 28 */ K_DO, ZERO, ZERO, ZERO, ZERO, K_IN, K_DE, ZERO,
  55. /* 30 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  56. /* 38 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  57. /* 40 */ ZERO, CTLA, CTLB, CTLC, CTLD, CTLE, CTLF, CTLG,
  58. /* 48 */ CTLH, CTLI, CTLJ, CTLK, CTLL, CTLM, CTLN, CTLO,
  59. /* 50 */ CTLP, CTLQ, CTLR, CTLS, CTLT, CTLU, CTLV, CTLW,
  60. /* 58 */ CTLX, CTLY, CTLZ, CTL1, CTL2, CTL3, CTL4, CTL5,
  61. /* 60 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  62. /* 68 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  63. /* 70 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  64. /* 78 */ ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO};
  65. #define ALT_PRESSED (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
  66. #define CTL_PRESSED (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
  67. #define CONTROL_ALT (ALT_PRESSED | CTL_PRESSED)
  68. /*
  69. * inTCHAR() - get a TCHARacter from the keyboard
  70. * Reads only ascii TCHARacters from the keyboard.
  71. */
  72. void
  73. inchar(CHAR *buff)
  74. {
  75. INPUT_RECORD InputRec;
  76. DWORD NumRead;
  77. int len;
  78. while(1) { /* loop until we get a valid console event */
  79. ReadConsoleInput(InputHandle,&InputRec,1,&NumRead);
  80. if((InputRec.EventType == KEY_EVENT)
  81. && (InputRec.Event.KeyEvent.bKeyDown)){
  82. KEY_EVENT_RECORD *KE = &InputRec.Event.KeyEvent;
  83. TCHAR *Table;
  84. if(KE->dwControlKeyState & ALT_PRESSED) {
  85. if ((KE->uChar.AsciiChar == 'x')||(KE->uChar.AsciiChar == 'X')) {
  86. exit(0);
  87. }
  88. continue; // no ALT keys allowed.
  89. } else{
  90. if(KE->dwControlKeyState & CTL_PRESSED) {
  91. Table = ControlTable;
  92. } else {
  93. // We might have a character in Unicode
  94. #ifdef UNICODE
  95. if(KE->uChar.UnicodeChar) { // no control, no alt
  96. // convert to dbcs character and return
  97. len= wctomb(buff,KE->uChar.UnicodeChar);
  98. if(len == -1){
  99. continue;
  100. }
  101. buff[len] = (CHAR) 0;
  102. return;
  103. #else
  104. if(KE->uChar.AsciiChar){
  105. buff[0] = KE->uChar.AsciiChar;
  106. buff[1] = (CHAR) 0;
  107. return;
  108. #endif
  109. } else {
  110. if(KE->dwControlKeyState & SHIFT_PRESSED) {
  111. Table = ShiftedTable;
  112. } else {
  113. Table = RegularTable;
  114. }
  115. }
  116. }
  117. }
  118. if((KE->wVirtualKeyCode > MAX_VK) || !Table[KE->wVirtualKeyCode]) {
  119. continue;
  120. }
  121. #ifdef UNICODE
  122. // convert to dbcs character and return
  123. len= wctomb(buff,Table[KE->wVirtualKeyCode]);
  124. if(len == -1){
  125. continue;
  126. }
  127. buff[len] = (CHAR) 0;
  128. #else
  129. buff[0] = Table[KE->wVirtualKeyCode];
  130. buff[1] = (CHAR) 0;
  131. #endif
  132. return;
  133. }
  134. }
  135. }