Release 961201
[wine] / windows / keyboard.c
1 /*
2  * Keyboard related functions
3  *
4  * Copyright 1993 Bob Amstadt
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include "win.h"
9 #include "windows.h"
10 #include "debug.h"
11
12 extern BOOL MouseButtonsStates[3];
13 extern BOOL AsyncMouseButtonsStates[3];
14 extern BYTE InputKeyStateTable[256];
15 BYTE AsyncKeyStateTable[256];
16
17 extern BYTE QueueKeyStateTable[256];
18
19 /**********************************************************************
20  *              GetKeyState                     [USER.106]
21  * An application calls the GetKeyState function in response to a
22  * keyboard-input message.  This function retrieves the state of the key
23  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
24  */
25 INT GetKeyState(INT keycode)
26 {
27     INT retval;
28
29     if (keycode >= 'a' && keycode <= 'z')
30         keycode += 'A' - 'a';
31     switch(keycode) {
32      case VK_LBUTTON:
33         retval = MouseButtonsStates[0];
34      case VK_MBUTTON:
35         retval = MouseButtonsStates[1];
36      case VK_RBUTTON:
37         retval = MouseButtonsStates[2];
38      default:
39         retval = ( (INT)(QueueKeyStateTable[keycode] & 0x80) << 8 ) |
40                    (INT)(QueueKeyStateTable[keycode] & 0x01);
41     }
42
43     dprintf_key(stddeb, "GetKeyState(%x) -> %x\n", keycode, retval);
44     return retval;
45 }
46
47 /**********************************************************************
48  *              GetKeyboardState                        [USER.222]
49  * An application calls the GetKeyboardState function in response to a
50  * keyboard-input message.  This function retrieves the state of the keyboard
51  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
52  */
53 void GetKeyboardState(BYTE *lpKeyState)
54 {
55     if (lpKeyState != NULL) {
56         QueueKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] >> 8;
57         QueueKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] >> 8;
58         QueueKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] >> 8;
59         memcpy(lpKeyState, QueueKeyStateTable, 256);
60     }
61 }
62
63 /**********************************************************************
64  *      SetKeyboardState            [USER.223]
65  */
66 void SetKeyboardState(BYTE *lpKeyState)
67 {
68     if (lpKeyState != NULL) {
69         memcpy(QueueKeyStateTable, lpKeyState, 256);
70         MouseButtonsStates[0] = QueueKeyStateTable[VK_LBUTTON]? 0x8000: 0;
71         MouseButtonsStates[1] = QueueKeyStateTable[VK_MBUTTON]? 0x8000: 0;
72         MouseButtonsStates[2] = QueueKeyStateTable[VK_RBUTTON]? 0x8000: 0;
73     }
74 }
75
76 /**********************************************************************
77  *            GetAsyncKeyState        (USER.249)
78  *
79  *      Determine if a key is or was pressed.  retval has high-order 
80  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
81  * been pressed.
82  *
83  *      This uses the variable AsyncMouseButtonsStates and
84  * AsyncKeyStateTable (set in event.c) which have the mouse button
85  * number or key number (whichever is applicable) set to true if the
86  * mouse or key had been depressed since the last call to 
87  * GetAsyncKeyState.
88  */
89 int GetAsyncKeyState(int nKey)
90 {
91     short retval;       
92
93     switch (nKey) {
94      case VK_LBUTTON:
95         retval = AsyncMouseButtonsStates[0] | 
96         MouseButtonsStates[0]? 0x0001: 0;
97         break;
98      case VK_MBUTTON:
99         retval = AsyncMouseButtonsStates[1] |
100         MouseButtonsStates[1]? 0x0001: 0;
101         break;
102      case VK_RBUTTON:
103         retval = AsyncMouseButtonsStates[2] |
104         MouseButtonsStates[2]? 0x0001: 0;
105         break;
106      default:
107         retval = AsyncKeyStateTable[nKey] | 
108         (InputKeyStateTable[nKey] ? 0x8000 : 0);
109         break;
110     }
111
112     memset( AsyncMouseButtonsStates, 0, 3 );  /* all states to false */
113     memset( AsyncKeyStateTable, 0, 256 );
114
115     dprintf_key(stddeb, "GetAsyncKeyState(%x) -> %x\n", nKey, retval);
116     return retval;
117 }