1 /* Test Key event to Key message translation
3 * Copyright 2003 Rein Klazes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* test whether the right type of messages:
21 * WM_KEYUP/DOWN vs WM_SYSKEYUP/DOWN are sent in case of combined
24 * For instance <ALT>-X can be accompished by
25 * the sequence ALT-KEY-DOWN, X-KEY-DOWN, ALT-KEY-UP, X-KEY-UP
26 * but also X-KEY-DOWN, ALT-KEY-DOWN, X-KEY-UP, ALT-KEY-UP
27 * Whether a KEY or a SYSKEY message is sent is not always clear, it is
28 * also not the same in WINNT as in WIN9X */
30 /* NOTE that there will be test failures under WIN9X
31 * No applications are known to me that rely on this
32 * so I don't fix it */
35 * 1. extend it to the wm_command and wm_syscommand notifications
36 * 2. add some more tests with special cases like dead keys or right (alt) key
37 * 3. there is some adapted code from input.c in here. Should really
38 * make that code exactly the same.
39 * 4. resolve the win9x case when there is a need or the testing frame work
41 * 5. The test app creates a window, the user should not take the focus
42 * away during its short existence. I could do something to prevent that
47 #define _WIN32_WINNT 0x401
49 #include "wine/test.h"
57 long timetag = 0x10000000;
59 static UINT (WINAPI *ptr_SendInput) (UINT, INPUT*, size_t);
61 #define MAXKEYEVENTS 6
62 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
63 and only one message */
65 /* keyboard message names, sorted as their value */
66 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
67 "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
69 /* keyevents, add more as needed */
71 { ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
73 int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
74 /* matching scan codes */
75 int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
76 /* matching updown events */
77 int GETUPDOWN[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
78 /* matching descripts */
79 char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
81 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
93 #define ADDTOINPUTS(kev) \
94 inputs[evtctr].type = INPUT_KEYBOARD; \
95 ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
96 ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
97 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETUPDOWN[ kev]; \
98 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
99 ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
108 /*******************************************
109 * add new test sets here
110 * the software will make all combinations of the
111 * keyevent defined here
114 KEV keydwn[MAXKEYEVENTS];
115 KEV keyup[MAXKEYEVENTS];
117 { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
118 { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
119 { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
120 { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
121 { 0 } /* mark the end */
124 /**********************adapted from input.c **********************************/
126 BYTE InputKeyStateTable[256];
127 BYTE AsyncKeyStateTable[256];
128 BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
129 or a WM_KEYUP message */
134 unsigned long count : 16;
135 unsigned long code : 8;
136 unsigned long extended : 1;
137 unsigned long unused : 2;
138 unsigned long win_internal : 2;
139 unsigned long context : 1;
140 unsigned long previous : 1;
141 unsigned long transition : 1;
146 int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
149 int VKey = GETVKEY[kev];
155 keylp.lp1.code = GETSCAN[kev];
156 keylp.lp1.extended = 0 ;/* FIXME (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0; */
157 keylp.lp1.win_internal = 0;
159 if (GETUPDOWN[kev] & KEYEVENTF_KEYUP )
162 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
163 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
164 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
165 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
166 (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
167 message = WM_SYSKEYUP;
170 InputKeyStateTable[VKey] &= ~0x80;
171 keylp.lp1.previous = 1;
172 keylp.lp1.transition = 1;
176 keylp.lp1.previous = (InputKeyStateTable[VKey] & 0x80) != 0;
177 keylp.lp1.transition = 0;
178 if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
179 InputKeyStateTable[VKey] |= 0x80;
180 AsyncKeyStateTable[VKey] |= 0x80;
182 message = WM_KEYDOWN;
183 if( (InputKeyStateTable[VK_MENU] & 0x80) &&
184 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
185 message = WM_SYSKEYDOWN;
190 keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
192 if( plParam) *plParam = keylp.lp2;
193 if( pwParam) *pwParam = VKey;
197 /****************************** end copy input.c ****************************/
200 * . prepare the keyevents for SendInputs
201 * . calculate the "expected" messages
202 * . Send the events to our window
203 * . retrieve the messages from the input queue
206 void do_test( HWND hwnd, int seqnr, KEV td[] )
209 INPUT inputs[MAXKEYEVENTS];
210 KMSG expmsg[MAXKEYEVENTS];
216 module = GetModuleHandleA("user32");
218 ptr_SendInput = (void *)GetProcAddress(module, "SendInput");
219 if (!ptr_SendInput) return;
222 TrackSysKey=0; /* see input.c */
223 for( i = 0; i < MAXKEYEVENTS; i++) {
225 strcat(buf, getdesc[td[i]]);
227 expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
229 expmsg[i].message = 0;
231 for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
233 assert( evtctr <= MAXKEYEVENTS );
234 assert( evtctr == ptr_SendInput(evtctr, &inputs[0], sizeof(INPUT)));
236 trace("======== key stroke sequence #%d: %s =============\n",
238 while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
239 trace("message[%d] %-15s wParam %04x lParam %08lx time %lx\n", i,
240 MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
242 ok( msg.message == expmsg[i].message &&
243 msg.wParam == expmsg[i].wParam &&
244 msg.lParam == expmsg[i].lParam,
245 "wrong message! expected:\n"
246 "message[%d] %-15s wParam %04x lParam %08lx\n",i,
247 MSGNAME[(expmsg[i]).message - WM_KEYFIRST],
248 expmsg[i].wParam, expmsg[i].lParam );
252 trace("%d messages retrieved\n", i);
253 ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
256 /* test all combinations of the specified key events */
257 void TestASet( HWND hWnd, int nrkev, KEV kevdwn[], KEV kevup[] )
261 KEV kbuf[MAXKEYEVENTS];
262 assert( nrkev==2 || nrkev==3);
263 for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
264 /* two keys involved gives 4 test cases */
266 for(i=0;i<nrkev;i++) {
267 for(j=0;j<nrkev;j++) {
269 kbuf[1] = kevdwn[1-i];
271 kbuf[3] = kevup[1-j];
272 do_test( hWnd, count++, kbuf);
276 /* three keys involved gives 36 test cases */
278 for(i=0;i<nrkev;i++){
279 for(j=0;j<nrkev;j++){
281 for(k=0;k<nrkev;k++){
282 if(k==i || k==j) continue;
283 for(l=0;l<nrkev;l++){
284 for(m=0;m<nrkev;m++){
286 for(n=0;n<nrkev;n++){
287 if(n==l ||n==m) continue;
294 do_test( hWnd, count++, kbuf);
304 /* test each set specified in the global testkeyset array */
305 void TestSysKeys( HWND hWnd)
308 for(i=0; testkeyset[i].nrkev;i++)
309 TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn,
310 testkeyset[i].keyup);
314 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
319 /* window has focus, now do the test */
320 if( hWnd == hWndTest) TestSysKeys( hWnd);
326 PostQuitMessage( 0 );
330 return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
340 HANDLE hInstance = GetModuleHandleA( NULL );
342 wclass.lpszClassName = "InputSysKeyTestClass";
343 wclass.style = CS_HREDRAW | CS_VREDRAW;
344 wclass.lpfnWndProc = (WNDPROC)WndProc;
345 wclass.hInstance = hInstance;
346 wclass.hIcon = LoadIconA( 0, (LPSTR)IDI_APPLICATION );
347 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW);
348 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1);
349 wclass.lpszMenuName = 0;
350 wclass.cbClsExtra = 0;
351 wclass.cbWndExtra = 0;
352 assert (RegisterClassA( &wclass ));
353 /* create the test window that will receive the keystrokes */
354 assert ( hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
355 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
356 NULL, NULL, hInstance, NULL) );
357 ShowWindow( hWndTest, SW_SHOW);
358 UpdateWindow( hWndTest);
360 while( GetMessageA( &msg, 0, 0, 0 )) {
361 TranslateMessage( &msg );
362 DispatchMessageA( &msg );