push 8724397e7ede0f147b6e05994a72142d44d4fecc
[wine/hacks.git] / dlls / user32 / tests / input.c
blobe4ad1de77d0ba6b096361499ffe7ce98210dd70f
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* test whether the right type of messages:
21 * WM_KEYUP/DOWN vs WM_SYSKEYUP/DOWN are sent in case of combined
22 * keystrokes.
24 * For instance <ALT>-X can be accomplished 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 */
34 /* TODO:
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
40 * offers a nice way.
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
43 * if it is a problem.
47 #define _WIN32_WINNT 0x401
48 #define _WIN32_IE 0x0500
50 #include <stdarg.h>
51 #include <assert.h>
53 #include "windef.h"
54 #include "winbase.h"
55 #include "winuser.h"
57 #include "wine/test.h"
59 /* globals */
60 static HWND hWndTest;
61 static long timetag = 0x10000000;
63 static UINT (WINAPI *pSendInput) (UINT, INPUT*, size_t);
64 static int (WINAPI *pGetMouseMovePointsEx) (UINT, LPMOUSEMOVEPOINT, LPMOUSEMOVEPOINT, int, DWORD);
66 #define MAXKEYEVENTS 10
67 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
68 and only one message */
70 /* keyboard message names, sorted as their value */
71 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
72 "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
74 /* keyevents, add more as needed */
75 typedef enum KEVtag
76 { ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
77 /* matching VK's */
78 static const int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
79 /* matching scan codes */
80 static const int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
81 /* matching updown events */
82 static const int GETFLAGS[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
83 /* matching descriptions */
84 static const char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
86 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
87 typedef struct
89 DWORD type;
90 union
92 MOUSEINPUT mi;
93 KEYBDINPUT ki;
94 HARDWAREINPUT hi;
95 } u;
96 } TEST_INPUT;
98 #define ADDTOINPUTS(kev) \
99 inputs[evtctr].type = INPUT_KEYBOARD; \
100 ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
101 ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
102 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETFLAGS[ kev]; \
103 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
104 ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
105 if( kev) evtctr++;
107 typedef struct {
108 UINT message;
109 WPARAM wParam;
110 LPARAM lParam;
111 } KMSG;
113 /*******************************************
114 * add new test sets here
115 * the software will make all combinations of the
116 * keyevent defined here
118 static const struct {
119 int nrkev;
120 KEV keydwn[MAXKEYEVENTS];
121 KEV keyup[MAXKEYEVENTS];
122 } testkeyset[]= {
123 { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
124 { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
125 { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
126 { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
127 { 0 } /* mark the end */
130 /**********************adapted from input.c **********************************/
132 static BYTE InputKeyStateTable[256];
133 static BYTE AsyncKeyStateTable[256];
134 static BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
135 or a WM_KEYUP message */
137 static void init_function_pointers(void)
139 HMODULE hdll = GetModuleHandleA("user32");
141 #define GET_PROC(func) \
142 p ## func = (void*)GetProcAddress(hdll, #func); \
143 if(!p ## func) \
144 trace("GetProcAddress(%s) failed\n", #func);
146 GET_PROC(SendInput)
147 GET_PROC(GetMouseMovePointsEx)
149 #undef GET_PROC
152 static int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
154 UINT message;
155 int VKey = GETVKEY[kev];
156 WORD flags;
158 flags = LOBYTE(GETSCAN[kev]);
159 if (GETFLAGS[kev] & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
161 if (GETFLAGS[kev] & KEYEVENTF_KEYUP )
163 message = WM_KEYUP;
164 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
165 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
166 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
167 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
168 (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
169 message = WM_SYSKEYUP;
170 TrackSysKey = 0;
172 InputKeyStateTable[VKey] &= ~0x80;
173 flags |= KF_REPEAT | KF_UP;
175 else
177 if (InputKeyStateTable[VKey] & 0x80) flags |= KF_REPEAT;
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;
186 TrackSysKey = VKey;
190 if (InputKeyStateTable[VK_MENU] & 0x80) flags |= KF_ALTDOWN;
192 if( plParam) *plParam = MAKELPARAM( 1, flags );
193 if( pwParam) *pwParam = VKey;
194 return message;
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
204 * . verify
206 static BOOL do_test( HWND hwnd, int seqnr, const KEV td[] )
208 INPUT inputs[MAXKEYEVENTS];
209 KMSG expmsg[MAXKEYEVENTS];
210 MSG msg;
211 char buf[100];
212 UINT evtctr=0;
213 int kmctr, i;
215 buf[0]='\0';
216 TrackSysKey=0; /* see input.c */
217 for( i = 0; i < MAXKEYEVENTS; i++) {
218 ADDTOINPUTS(td[i])
219 strcat(buf, getdesc[td[i]]);
220 if(td[i])
221 expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
222 else
223 expmsg[i].message = 0;
225 for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
227 assert( evtctr <= MAXKEYEVENTS );
228 assert( evtctr == pSendInput(evtctr, &inputs[0], sizeof(INPUT)));
229 i = 0;
230 if (winetest_debug > 1)
231 trace("======== key stroke sequence #%d: %s =============\n",
232 seqnr + 1, buf);
233 while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
234 if (winetest_debug > 1)
235 trace("message[%d] %-15s wParam %04lx lParam %08lx time %x\n", i,
236 MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
237 if( i < kmctr ) {
238 ok( msg.message == expmsg[i].message &&
239 msg.wParam == expmsg[i].wParam &&
240 msg.lParam == expmsg[i].lParam,
241 "%u/%u: wrong message %x/%08lx/%08lx expected %s/%08lx/%08lx\n",
242 seqnr, i, msg.message, msg.wParam, msg.lParam,
243 MSGNAME[(expmsg[i]).message - WM_KEYFIRST], expmsg[i].wParam, expmsg[i].lParam );
245 i++;
247 if (winetest_debug > 1)
248 trace("%d messages retrieved\n", i);
249 if (!i && kmctr)
251 skip( "simulated keyboard input doesn't work\n" );
252 return FALSE;
254 ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
255 return TRUE;
258 /* test all combinations of the specified key events */
259 static BOOL TestASet( HWND hWnd, int nrkev, const KEV kevdwn[], const KEV kevup[] )
261 int i,j,k,l,m,n;
262 static int count=0;
263 KEV kbuf[MAXKEYEVENTS];
264 assert( nrkev==2 || nrkev==3);
265 for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
266 /* two keys involved gives 4 test cases */
267 if(nrkev==2) {
268 for(i=0;i<nrkev;i++) {
269 for(j=0;j<nrkev;j++) {
270 kbuf[0] = kevdwn[i];
271 kbuf[1] = kevdwn[1-i];
272 kbuf[2] = kevup[j];
273 kbuf[3] = kevup[1-j];
274 if (!do_test( hWnd, count++, kbuf)) return FALSE;
278 /* three keys involved gives 36 test cases */
279 if(nrkev==3){
280 for(i=0;i<nrkev;i++){
281 for(j=0;j<nrkev;j++){
282 if(j==i) continue;
283 for(k=0;k<nrkev;k++){
284 if(k==i || k==j) continue;
285 for(l=0;l<nrkev;l++){
286 for(m=0;m<nrkev;m++){
287 if(m==l) continue;
288 for(n=0;n<nrkev;n++){
289 if(n==l ||n==m) continue;
290 kbuf[0] = kevdwn[i];
291 kbuf[1] = kevdwn[j];
292 kbuf[2] = kevdwn[k];
293 kbuf[3] = kevup[l];
294 kbuf[4] = kevup[m];
295 kbuf[5] = kevup[n];
296 if (!do_test( hWnd, count++, kbuf)) return FALSE;
304 return TRUE;
307 /* test each set specified in the global testkeyset array */
308 static void TestSysKeys( HWND hWnd)
310 int i;
311 for(i=0; testkeyset[i].nrkev;i++)
312 if (!TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn, testkeyset[i].keyup)) break;
315 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
316 LPARAM lParam )
318 return DefWindowProcA( hWnd, msg, wParam, lParam );
321 static void test_Input_whitebox(void)
323 MSG msg;
324 WNDCLASSA wclass;
325 HANDLE hInstance = GetModuleHandleA( NULL );
327 wclass.lpszClassName = "InputSysKeyTestClass";
328 wclass.style = CS_HREDRAW | CS_VREDRAW;
329 wclass.lpfnWndProc = WndProc;
330 wclass.hInstance = hInstance;
331 wclass.hIcon = LoadIconA( 0, IDI_APPLICATION );
332 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW );
333 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
334 wclass.lpszMenuName = 0;
335 wclass.cbClsExtra = 0;
336 wclass.cbWndExtra = 0;
337 RegisterClassA( &wclass );
338 /* create the test window that will receive the keystrokes */
339 hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
340 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
341 NULL, NULL, hInstance, NULL);
342 assert( hWndTest );
343 ShowWindow( hWndTest, SW_SHOW);
344 SetWindowPos( hWndTest, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
345 SetForegroundWindow( hWndTest );
346 UpdateWindow( hWndTest);
348 /* flush pending messages */
349 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
351 SetFocus( hWndTest );
352 TestSysKeys( hWndTest );
353 DestroyWindow(hWndTest);
356 /* try to make sure pending X events have been processed before continuing */
357 static void empty_message_queue(void)
359 MSG msg;
360 int diff = 200;
361 int min_timeout = 50;
362 DWORD time = GetTickCount() + diff;
364 while (diff > 0)
366 if (MsgWaitForMultipleObjects(0, NULL, FALSE, min_timeout, QS_ALLINPUT) == WAIT_TIMEOUT) break;
367 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
369 TranslateMessage(&msg);
370 DispatchMessage(&msg);
372 diff = time - GetTickCount();
376 struct transition_s {
377 WORD wVk;
378 BYTE before_state;
379 BYTE optional;
382 typedef enum {
383 sent=0x1,
384 posted=0x2,
385 parent=0x4,
386 wparam=0x8,
387 lparam=0x10,
388 defwinproc=0x20,
389 beginpaint=0x40,
390 optional=0x80,
391 hook=0x100,
392 winevent_hook=0x200
393 } msg_flags_t;
395 struct message {
396 UINT message; /* the WM_* code */
397 msg_flags_t flags; /* message props */
398 WPARAM wParam; /* expected value of wParam */
399 LPARAM lParam; /* expected value of lParam */
402 struct sendinput_test_s {
403 WORD wVk;
404 DWORD dwFlags;
405 BOOL _todo_wine;
406 struct transition_s expected_transitions[MAXKEYEVENTS+1];
407 struct message expected_messages[MAXKEYMESSAGES+1];
408 } sendinput_test[] = {
409 /* test ALT+F */
410 /* 0 */
411 {VK_LMENU, 0, 0, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
412 {{WM_SYSKEYDOWN, hook|wparam, VK_LMENU}, {WM_SYSKEYDOWN}, {0}}},
413 {'F', 0, 0, {{'F', 0x00}, {0}},
414 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN},
415 {WM_SYSCHAR},
416 {WM_SYSCOMMAND}, {0}}},
417 {'F', KEYEVENTF_KEYUP, 0, {{'F', 0x80}, {0}},
418 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
419 {VK_LMENU, KEYEVENTF_KEYUP, 0, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
420 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
422 /* test CTRL+O */
423 /* 4 */
424 {VK_LCONTROL, 0, 0, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
425 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
426 {'O', 0, 0, {{'O', 0x00}, {0}},
427 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
428 {'O', KEYEVENTF_KEYUP, 0, {{'O', 0x80}, {0}},
429 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
430 {VK_LCONTROL, KEYEVENTF_KEYUP, 0, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
431 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
433 /* test ALT+CTRL+X */
434 /* 8 */
435 {VK_LMENU, 0, 0, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
436 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN}, {0}}},
437 {VK_LCONTROL, 0, 0, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
438 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
439 {'X', 0, 0, {{'X', 0x00}, {0}},
440 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
441 {'X', KEYEVENTF_KEYUP, 0, {{'X', 0x80}, {0}},
442 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
443 {VK_LCONTROL, KEYEVENTF_KEYUP, 0, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
444 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
445 {VK_LMENU, KEYEVENTF_KEYUP, 0, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
446 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
448 /* test SHIFT+A */
449 /* 14 */
450 {VK_LSHIFT, 0, 0, {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
451 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
452 {'A', 0, 0, {{'A', 0x00}, {0}},
453 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
454 {'A', KEYEVENTF_KEYUP, 0, {{'A', 0x80}, {0}},
455 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
456 {VK_LSHIFT, KEYEVENTF_KEYUP, 0, {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
457 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
458 /* test L-SHIFT & R-SHIFT: */
459 /* RSHIFT == LSHIFT */
460 /* 18 */
461 {VK_RSHIFT, 0, 0,
462 /* recent windows versions (>= w2k3) correctly report an RSHIFT transition */
463 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00, TRUE}, {VK_RSHIFT, 0x00, TRUE}, {0}},
464 {{WM_KEYDOWN, hook|wparam, VK_RSHIFT},
465 {WM_KEYDOWN}, {0}}},
466 {VK_RSHIFT, KEYEVENTF_KEYUP, 0,
467 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80, TRUE}, {VK_RSHIFT, 0x80, TRUE}, {0}},
468 {{WM_KEYUP, hook, hook|wparam, VK_RSHIFT},
469 {WM_KEYUP}, {0}}},
471 /* LSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
472 /* 20 */
473 {VK_LSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
474 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
475 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, LLKHF_EXTENDED},
476 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
477 {VK_LSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
478 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
479 {{WM_KEYUP, hook|wparam|lparam, VK_LSHIFT, LLKHF_UP|LLKHF_EXTENDED},
480 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
481 /* RSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
482 /* 22 */
483 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
484 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
485 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
486 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
487 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
488 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
489 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
490 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
492 /* Note about wparam for hook with generic key (VK_SHIFT, VK_CONTROL, VK_MENU):
493 win2k - sends to hook whatever we generated here
494 winXP+ - Attempts to convert key to L/R key but not always correct
496 /* SHIFT == LSHIFT */
497 /* 24 */
498 {VK_SHIFT, 0, 0,
499 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
500 {{WM_KEYDOWN, hook/* |wparam */|lparam, VK_SHIFT, 0},
501 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
502 {VK_SHIFT, KEYEVENTF_KEYUP, 0,
503 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
504 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_UP},
505 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
506 /* SHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
507 /* 26 */
508 {VK_SHIFT, KEYEVENTF_EXTENDEDKEY, 0,
509 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
510 {{WM_KEYDOWN, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_EXTENDED},
511 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
512 {VK_SHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
513 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
514 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_UP|LLKHF_EXTENDED},
515 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
517 /* test L-CONTROL & R-CONTROL: */
518 /* RCONTROL == LCONTROL */
519 /* 28 */
520 {VK_RCONTROL, 0, 0,
521 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
522 {{WM_KEYDOWN, hook|wparam, VK_RCONTROL},
523 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
524 {VK_RCONTROL, KEYEVENTF_KEYUP, 0,
525 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
526 {{WM_KEYUP, hook|wparam, VK_RCONTROL},
527 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
528 /* LCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
529 /* 30 */
530 {VK_LCONTROL, KEYEVENTF_EXTENDEDKEY, 0,
531 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
532 {{WM_KEYDOWN, hook|wparam|lparam, VK_LCONTROL, LLKHF_EXTENDED},
533 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
534 {VK_LCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
535 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
536 {{WM_KEYUP, hook|wparam|lparam, VK_LCONTROL, LLKHF_UP|LLKHF_EXTENDED},
537 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
538 /* RCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
539 /* 32 */
540 {VK_RCONTROL, KEYEVENTF_EXTENDEDKEY, 0,
541 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
542 {{WM_KEYDOWN, hook|wparam|lparam, VK_RCONTROL, LLKHF_EXTENDED},
543 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
544 {VK_RCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
545 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
546 {{WM_KEYUP, hook|wparam|lparam, VK_RCONTROL, LLKHF_UP|LLKHF_EXTENDED},
547 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
548 /* CONTROL == LCONTROL */
549 /* 34 */
550 {VK_CONTROL, 0, 0,
551 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
552 {{WM_KEYDOWN, hook/*|wparam, VK_CONTROL*/},
553 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
554 {VK_CONTROL, KEYEVENTF_KEYUP, 0,
555 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
556 {{WM_KEYUP, hook/*|wparam, VK_CONTROL*/},
557 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
558 /* CONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
559 /* 36 */
560 {VK_CONTROL, KEYEVENTF_EXTENDEDKEY, 0,
561 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
562 {{WM_KEYDOWN, hook/*|wparam*/|lparam, VK_CONTROL, LLKHF_EXTENDED},
563 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
564 {VK_CONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
565 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
566 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_CONTROL, LLKHF_UP|LLKHF_EXTENDED},
567 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
569 /* test L-MENU & R-MENU: */
570 /* RMENU == LMENU */
571 /* 38 */
572 {VK_RMENU, 0, 0,
573 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
574 {{WM_SYSKEYDOWN, hook|wparam|optional, VK_LCONTROL},
575 {WM_SYSKEYDOWN, hook|wparam, VK_RMENU},
576 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
577 {VK_RMENU, KEYEVENTF_KEYUP, 1,
578 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
579 {{WM_KEYUP, hook|wparam|optional, VK_LCONTROL},
580 {WM_KEYUP, hook|wparam, VK_RMENU},
581 {WM_SYSKEYUP, wparam|lparam|optional, VK_CONTROL, KF_UP},
582 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
583 {WM_SYSCOMMAND}, {0}}},
584 /* LMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
585 /* 40 */
586 {VK_LMENU, KEYEVENTF_EXTENDEDKEY, 0,
587 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {0}},
588 {{WM_SYSKEYDOWN, hook|wparam|lparam, VK_LMENU, LLKHF_EXTENDED},
589 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
590 {VK_LMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
591 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {0}},
592 {{WM_KEYUP, hook|wparam|lparam, VK_LMENU, LLKHF_UP|LLKHF_EXTENDED},
593 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
594 {WM_SYSCOMMAND}, {0}}},
595 /* RMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
596 /* 42 */
597 {VK_RMENU, KEYEVENTF_EXTENDEDKEY, 0,
598 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
599 {{WM_SYSKEYDOWN, hook|wparam|lparam|optional, VK_LCONTROL, 0},
600 {WM_SYSKEYDOWN, hook|wparam|lparam, VK_RMENU, LLKHF_EXTENDED},
601 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
602 {VK_RMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
603 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
604 {{WM_KEYUP, hook|wparam|lparam|optional, VK_LCONTROL, LLKHF_UP},
605 {WM_KEYUP, hook|wparam|lparam, VK_RMENU, LLKHF_UP|LLKHF_EXTENDED},
606 {WM_SYSKEYUP, wparam|lparam|optional, VK_CONTROL, KF_UP},
607 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
608 {WM_SYSCOMMAND}, {0}}},
609 /* MENU == LMENU */
610 /* 44 */
611 {VK_MENU, 0, 0,
612 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
613 {{WM_SYSKEYDOWN, hook/*|wparam, VK_MENU*/},
614 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
615 {VK_MENU, KEYEVENTF_KEYUP, 1,
616 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
617 {{WM_KEYUP, hook/*|wparam, VK_MENU*/},
618 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
619 {WM_SYSCOMMAND}, {0}}},
620 /* MENU | KEYEVENTF_EXTENDEDKEY == RMENU */
621 /* 46 */
622 {VK_MENU, KEYEVENTF_EXTENDEDKEY, 0,
623 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
624 {{WM_SYSKEYDOWN, hook|wparam|lparam|optional, VK_CONTROL, 0},
625 {WM_SYSKEYDOWN, hook/*|wparam*/|lparam, VK_MENU, LLKHF_EXTENDED},
626 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
627 {VK_MENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
628 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
629 {{WM_KEYUP, hook|wparam|lparam|optional, VK_CONTROL, LLKHF_UP},
630 {WM_KEYUP, hook/*|wparam*/|lparam, VK_MENU, LLKHF_UP|LLKHF_EXTENDED},
631 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
632 {WM_SYSCOMMAND}, {0}}},
634 /* test LSHIFT & RSHIFT */
635 /* 48 */
636 {VK_LSHIFT, 0, 0,
637 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
638 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, 0},
639 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
640 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
641 {{VK_RSHIFT, 0x00}, {0}},
642 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
643 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
644 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
645 {{VK_RSHIFT, 0x80}, {0}},
646 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
647 {WM_KEYUP, optional}, {0}}},
648 {VK_LSHIFT, KEYEVENTF_KEYUP, 0,
649 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
650 {{WM_KEYUP, hook|wparam, VK_LSHIFT},
651 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
653 {0, 0, 0, {{0}}, {{0}}} /* end */
656 static struct message sent_messages[MAXKEYMESSAGES];
657 static UINT sent_messages_cnt;
659 /* Verify that only specified key state transitions occur */
660 static void compare_and_check(int id, BYTE *ks1, BYTE *ks2, struct sendinput_test_s *test)
662 int i, failcount = 0;
663 struct transition_s *t = test->expected_transitions;
664 UINT actual_cnt = 0;
665 const struct message *expected = test->expected_messages;
667 while (t->wVk) {
668 int matched = ((ks1[t->wVk]&0x80) == (t->before_state&0x80)
669 && (ks2[t->wVk]&0x80) == (~t->before_state&0x80));
671 if (!matched && !t->optional && test->_todo_wine)
673 failcount++;
674 todo_wine {
675 ok(matched, "%2d (%x/%x): %02x from %02x -> %02x "
676 "instead of %02x -> %02x\n", id, test->wVk, test->dwFlags,
677 t->wVk, ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
678 ~t->before_state&0x80);
680 } else {
681 ok(matched || t->optional, "%2d (%x/%x): %02x from %02x -> %02x "
682 "instead of %02x -> %02x\n", id, test->wVk, test->dwFlags,
683 t->wVk, ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
684 ~t->before_state&0x80);
686 ks2[t->wVk] = ks1[t->wVk]; /* clear the match */
687 t++;
689 for (i = 0; i < 256; i++)
690 if (ks2[i] != ks1[i] && test->_todo_wine)
692 failcount++;
693 todo_wine
694 ok(FALSE, "%2d (%x/%x): %02x from %02x -> %02x unexpected\n",
695 id, test->wVk, test->dwFlags, i, ks1[i], ks2[i]);
697 else
698 ok(ks2[i] == ks1[i], "%2d (%x/%x): %02x from %02x -> %02x unexpected\n",
699 id, test->wVk, test->dwFlags, i, ks1[i], ks2[i]);
701 while (expected->message && actual_cnt < sent_messages_cnt)
703 const struct message *actual = &sent_messages[actual_cnt];
705 if (expected->message == actual->message)
707 if (expected->flags & wparam)
709 if ((expected->flags & optional) && (expected->wParam != actual->wParam))
711 expected++;
712 continue;
714 if (expected->wParam != actual->wParam && test->_todo_wine)
716 failcount++;
717 todo_wine
718 ok(FALSE, "%2d (%x/%x): in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
719 id, test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
721 else
722 ok(expected->wParam == actual->wParam,
723 "%2d (%x/%x): in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
724 id, test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
726 if (expected->flags & lparam)
728 if (expected->lParam != actual->lParam && test->_todo_wine)
730 failcount++;
731 todo_wine
732 ok(FALSE, "%2d (%x/%x): in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
733 id, test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
735 else
736 ok(expected->lParam == actual->lParam,
737 "%2d (%x/%x): in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
738 id, test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
740 ok((expected->flags & hook) == (actual->flags & hook),
741 "%2d (%x/%x): the msg 0x%04x should have been sent by a hook\n",
742 id, test->wVk, test->dwFlags, expected->message);
745 else if (expected->flags & optional)
747 expected++;
748 continue;
750 else if (test->_todo_wine)
752 failcount++;
753 todo_wine
754 ok(FALSE,
755 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
756 id, test->wVk, test->dwFlags, expected->message, actual->message);
758 /* NT4 doesn't send SYSKEYDOWN/UP to hooks, only KEYDOWN/UP */
759 else if ((expected->flags & hook) &&
760 (expected->message == WM_SYSKEYDOWN || expected->message == WM_SYSKEYUP) &&
761 (actual->message == expected->message - 4))
763 ok((expected->flags & hook) == (actual->flags & hook),
764 "%2d (%x/%x): the msg 0x%04x should have been sent by a hook\n",
765 id, test->wVk, test->dwFlags, expected->message);
767 else
768 ok(FALSE,
769 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
770 id, test->wVk, test->dwFlags, expected->message, actual->message);
772 actual_cnt++;
773 expected++;
775 /* skip all optional trailing messages */
776 while (expected->message && (expected->flags & optional))
777 expected++;
780 if (expected->message || actual_cnt < sent_messages_cnt)
782 if (test->_todo_wine)
784 failcount++;
785 todo_wine
786 ok(FALSE, "%2d (%x/%x): the msg sequence is not complete: expected %04x - actual %04x\n",
787 id, test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
789 else
790 ok(FALSE, "%2d (%x/%x): the msg sequence is not complete: expected %04x - actual %04x\n",
791 id, test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
794 if( test->_todo_wine && !failcount) /* succeeded yet marked todo */
795 todo_wine
796 ok(TRUE, "%2d (%x/%x): marked \"todo_wine\" but succeeds\n", id, test->wVk, test->dwFlags);
798 sent_messages_cnt = 0;
801 /* WndProc2 checks that we get at least the messages specified */
802 static LRESULT CALLBACK WndProc2(HWND hWnd, UINT Msg, WPARAM wParam,
803 LPARAM lParam)
805 if (winetest_debug > 1) trace("MSG: %8x W:%8lx L:%8lx\n", Msg, wParam, lParam);
807 if (Msg != WM_PAINT &&
808 Msg != WM_NCPAINT &&
809 Msg != WM_SYNCPAINT &&
810 Msg != WM_ERASEBKGND &&
811 Msg != WM_NCHITTEST &&
812 Msg != WM_GETTEXT &&
813 Msg != WM_GETICON &&
814 Msg != WM_IME_SELECT &&
815 Msg != WM_DEVICECHANGE)
817 ok(sent_messages_cnt < MAXKEYMESSAGES, "Too many messages\n");
818 if (sent_messages_cnt < MAXKEYMESSAGES)
820 sent_messages[sent_messages_cnt].message = Msg;
821 sent_messages[sent_messages_cnt].flags = 0;
822 sent_messages[sent_messages_cnt].wParam = wParam;
823 sent_messages[sent_messages_cnt++].lParam = HIWORD(lParam) & (KF_UP|KF_EXTENDED);
826 return DefWindowProc(hWnd, Msg, wParam, lParam);
829 static LRESULT CALLBACK hook_proc(int code, WPARAM wparam, LPARAM lparam)
831 KBDLLHOOKSTRUCT *hook_info = (KBDLLHOOKSTRUCT *)lparam;
833 if (code == HC_ACTION)
835 ok(sent_messages_cnt < MAXKEYMESSAGES, "Too many messages\n");
836 if (sent_messages_cnt < MAXKEYMESSAGES)
838 sent_messages[sent_messages_cnt].message = wparam;
839 sent_messages[sent_messages_cnt].flags = hook;
840 sent_messages[sent_messages_cnt].wParam = hook_info->vkCode;
841 sent_messages[sent_messages_cnt++].lParam = hook_info->flags & (LLKHF_UP|LLKHF_EXTENDED);
844 if(0) /* For some reason not stable on Wine */
846 if (wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
847 ok(!(GetAsyncKeyState(hook_info->vkCode) & 0x8000), "key %x should be up\n", hook_info->vkCode);
848 else if (wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
849 ok(GetAsyncKeyState(hook_info->vkCode) & 0x8000, "key %x should be down\n", hook_info->vkCode);
852 if (winetest_debug > 1)
853 trace("Hook: w=%lx vk:%8x sc:%8x fl:%8x %lx\n", wparam,
854 hook_info->vkCode, hook_info->scanCode, hook_info->flags, hook_info->dwExtraInfo);
856 return CallNextHookEx( 0, code, wparam, lparam );
858 static void test_Input_blackbox(void)
860 TEST_INPUT i;
861 int ii;
862 BYTE ks1[256], ks2[256];
863 LONG_PTR prevWndProc;
864 HWND window;
865 HHOOK hook;
867 window = CreateWindow("Static", NULL, WS_POPUP|WS_HSCROLL|WS_VSCROLL
868 |WS_VISIBLE, 0, 0, 200, 60, NULL, NULL,
869 NULL, NULL);
870 ok(window != NULL, "error: %d\n", (int) GetLastError());
871 SetWindowPos( window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
872 SetForegroundWindow( window );
874 hook = SetWindowsHookExA(WH_KEYBOARD_LL, hook_proc, GetModuleHandleA( NULL ), 0);
876 /* must process all initial messages, otherwise X11DRV_KeymapNotify unsets
877 * key state set by SendInput(). */
878 empty_message_queue();
880 prevWndProc = SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR) WndProc2);
881 ok(prevWndProc != 0 || (prevWndProc == 0 && GetLastError() == 0),
882 "error: %d\n", (int) GetLastError());
884 i.type = INPUT_KEYBOARD;
885 i.u.ki.time = 0;
886 i.u.ki.dwExtraInfo = 0;
888 for (ii = 0; ii < sizeof(sendinput_test)/sizeof(struct sendinput_test_s)-1;
889 ii++) {
890 GetKeyboardState(ks1);
891 i.u.ki.wScan = ii+1 /* useful for debugging */;
892 i.u.ki.dwFlags = sendinput_test[ii].dwFlags;
893 i.u.ki.wVk = sendinput_test[ii].wVk;
894 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
895 empty_message_queue();
896 GetKeyboardState(ks2);
897 if (!ii && sent_messages_cnt <= 1 && !memcmp( ks1, ks2, sizeof(ks1) ))
899 win_skip( "window doesn't receive the queued input\n" );
900 /* release the key */
901 i.u.ki.dwFlags |= KEYEVENTF_KEYUP;
902 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
903 break;
905 compare_and_check(ii, ks1, ks2, &sendinput_test[ii]);
908 empty_message_queue();
909 DestroyWindow(window);
910 UnhookWindowsHookEx(hook);
913 static void test_keynames(void)
915 int i, len;
916 char buff[256];
918 for (i = 0; i < 512; i++)
920 strcpy(buff, "----");
921 len = GetKeyNameTextA(i << 16, buff, sizeof(buff));
922 ok(len || !buff[0], "%d: Buffer is not zeroed\n", i);
926 static POINT pt_old, pt_new;
927 static BOOL clipped;
928 #define STEP 3
930 static LRESULT CALLBACK hook_proc1( int code, WPARAM wparam, LPARAM lparam )
932 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
933 POINT pt, pt1;
935 if (code == HC_ACTION)
937 /* This is our new cursor position */
938 pt_new = hook->pt;
939 /* Should return previous position */
940 GetCursorPos(&pt);
941 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
943 /* Should set new position until hook chain is finished. */
944 pt.x = pt_old.x + STEP;
945 pt.y = pt_old.y + STEP;
946 SetCursorPos(pt.x, pt.y);
947 GetCursorPos(&pt1);
948 if (clipped)
949 ok(pt1.x == pt_old.x && pt1.y == pt_old.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
950 else
951 ok(pt1.x == pt.x && pt1.y == pt.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
953 return CallNextHookEx( 0, code, wparam, lparam );
956 static LRESULT CALLBACK hook_proc2( int code, WPARAM wparam, LPARAM lparam )
958 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
959 POINT pt;
961 if (code == HC_ACTION)
963 ok(hook->pt.x == pt_new.x && hook->pt.y == pt_new.y,
964 "Wrong hook coords: (%d %d) != (%d,%d)\n", hook->pt.x, hook->pt.y, pt_new.x, pt_new.y);
966 /* Should match position set above */
967 GetCursorPos(&pt);
968 if (clipped)
969 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
970 else
971 ok(pt.x == pt_old.x +STEP && pt.y == pt_old.y +STEP, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
973 return CallNextHookEx( 0, code, wparam, lparam );
976 static void test_mouse_ll_hook(void)
978 HWND hwnd;
979 HHOOK hook1, hook2;
980 POINT pt_org, pt;
981 RECT rc;
983 GetCursorPos(&pt_org);
984 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
985 10, 10, 200, 200, NULL, NULL, NULL, NULL);
986 SetCursorPos(100, 100);
988 if (!(hook2 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc2, GetModuleHandleA(0), 0)))
990 win_skip( "cannot set MOUSE_LL hook\n" );
991 goto done;
993 hook1 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc1, GetModuleHandleA(0), 0);
995 GetCursorPos(&pt_old);
996 mouse_event(MOUSEEVENTF_MOVE, -STEP, 0, 0, 0);
997 GetCursorPos(&pt_old);
998 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
999 mouse_event(MOUSEEVENTF_MOVE, +STEP, 0, 0, 0);
1000 GetCursorPos(&pt_old);
1001 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1002 mouse_event(MOUSEEVENTF_MOVE, 0, -STEP, 0, 0);
1003 GetCursorPos(&pt_old);
1004 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1005 mouse_event(MOUSEEVENTF_MOVE, 0, +STEP, 0, 0);
1006 GetCursorPos(&pt_old);
1007 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
1009 SetRect(&rc, 50, 50, 151, 151);
1010 ClipCursor(&rc);
1011 clipped = TRUE;
1013 SetCursorPos(40, 40);
1014 GetCursorPos(&pt_old);
1015 ok(pt_old.x == 50 && pt_old.y == 50, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1016 SetCursorPos(160, 160);
1017 GetCursorPos(&pt_old);
1018 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1019 mouse_event(MOUSEEVENTF_MOVE, +STEP, +STEP, 0, 0);
1020 GetCursorPos(&pt_old);
1021 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1023 clipped = FALSE;
1024 pt_new.x = pt_new.y = 150;
1025 ClipCursor(NULL);
1026 UnhookWindowsHookEx(hook1);
1028 /* Now check that mouse buttons do not change mouse position
1029 if we don't have MOUSEEVENTF_MOVE flag specified. */
1031 /* We reusing the same hook callback, so make it happy */
1032 pt_old.x = pt_new.x - STEP;
1033 pt_old.y = pt_new.y - STEP;
1034 mouse_event(MOUSEEVENTF_LEFTUP, 123, 456, 0, 0);
1035 GetCursorPos(&pt);
1036 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1037 mouse_event(MOUSEEVENTF_RIGHTUP, 456, 123, 0, 0);
1038 GetCursorPos(&pt);
1039 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1041 mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 123, 456, 0, 0);
1042 GetCursorPos(&pt);
1043 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1044 mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 456, 123, 0, 0);
1045 GetCursorPos(&pt);
1046 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1048 UnhookWindowsHookEx(hook2);
1049 done:
1050 DestroyWindow(hwnd);
1051 SetCursorPos(pt_org.x, pt_org.y);
1054 static void test_GetMouseMovePointsEx(void)
1056 #define BUFLIM 64
1057 #define MYERROR 0xdeadbeef
1058 int count, retval;
1059 MOUSEMOVEPOINT in;
1060 MOUSEMOVEPOINT out[200];
1061 POINT point;
1063 /* Get a valid content for the input struct */
1064 if(!GetCursorPos(&point)) {
1065 skip("GetCursorPos() failed with error %u\n", GetLastError());
1066 return;
1068 memset(&in, 0, sizeof(MOUSEMOVEPOINT));
1069 in.x = point.x;
1070 in.y = point.y;
1072 /* test first parameter
1073 * everything different than sizeof(MOUSEMOVEPOINT)
1074 * is expected to fail with ERROR_INVALID_PARAMETER
1076 SetLastError(MYERROR);
1077 retval = pGetMouseMovePointsEx(0, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1078 if (retval == ERROR_INVALID_PARAMETER)
1080 win_skip( "GetMouseMovePointsEx broken on WinME\n" );
1081 return;
1083 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1084 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1085 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1087 SetLastError(MYERROR);
1088 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1089 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1090 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1091 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1093 SetLastError(MYERROR);
1094 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)+1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1095 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1096 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1097 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1099 /* test second and third parameter
1101 SetLastError(MYERROR);
1102 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1103 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1104 ok(GetLastError() == ERROR_NOACCESS || GetLastError() == MYERROR,
1105 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1107 SetLastError(MYERROR);
1108 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1109 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1110 ok(ERROR_NOACCESS == GetLastError(),
1111 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1113 SetLastError(MYERROR);
1114 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1115 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1116 ok(ERROR_NOACCESS == GetLastError(),
1117 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1119 SetLastError(MYERROR);
1120 count = 0;
1121 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, count, GMMP_USE_DISPLAY_POINTS);
1122 if (retval == -1)
1123 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1124 else
1125 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1127 /* test fourth parameter
1128 * a value higher than 64 is expected to fail with ERROR_INVALID_PARAMETER
1130 SetLastError(MYERROR);
1131 count = -1;
1132 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1133 ok(retval == count, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1134 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1135 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1137 SetLastError(MYERROR);
1138 count = 0;
1139 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1140 if (retval == -1)
1141 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1142 else
1143 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1145 SetLastError(MYERROR);
1146 count = BUFLIM;
1147 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1148 if (retval == -1)
1149 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1150 else
1151 ok((0 <= retval) && (retval <= count), "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1153 SetLastError(MYERROR);
1154 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1155 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1156 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1157 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1159 /* it was not possible to force an error with the fifth parameter on win2k */
1161 /* test combinations of wrong parameters to see which error wins */
1162 SetLastError(MYERROR);
1163 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1164 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1165 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1166 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1168 SetLastError(MYERROR);
1169 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1170 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1171 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1172 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1174 SetLastError(MYERROR);
1175 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1176 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1177 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1178 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1180 SetLastError(MYERROR);
1181 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1182 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1183 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1184 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1186 #undef BUFLIM
1187 #undef MYERROR
1190 static void test_key_map(void)
1192 HKL kl = GetKeyboardLayout(0);
1193 UINT kL, kR, s, sL;
1194 int i;
1195 static const UINT numpad_collisions[][2] = {
1196 { VK_NUMPAD0, VK_INSERT },
1197 { VK_NUMPAD1, VK_END },
1198 { VK_NUMPAD2, VK_DOWN },
1199 { VK_NUMPAD3, VK_NEXT },
1200 { VK_NUMPAD4, VK_LEFT },
1201 { VK_NUMPAD6, VK_RIGHT },
1202 { VK_NUMPAD7, VK_HOME },
1203 { VK_NUMPAD8, VK_UP },
1204 { VK_NUMPAD9, VK_PRIOR },
1207 s = MapVirtualKeyEx(VK_SHIFT, MAPVK_VK_TO_VSC, kl);
1208 ok(s != 0, "MapVirtualKeyEx(VK_SHIFT) should return non-zero\n");
1209 sL = MapVirtualKeyEx(VK_LSHIFT, MAPVK_VK_TO_VSC, kl);
1210 ok(s == sL || broken(sL == 0), /* win9x */
1211 "%x != %x\n", s, sL);
1213 kL = MapVirtualKeyEx(0x2a, MAPVK_VSC_TO_VK, kl);
1214 ok(kL == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kL);
1215 kR = MapVirtualKeyEx(0x36, MAPVK_VSC_TO_VK, kl);
1216 ok(kR == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kR);
1218 kL = MapVirtualKeyEx(0x2a, MAPVK_VSC_TO_VK_EX, kl);
1219 ok(kL == VK_LSHIFT || broken(kL == 0), /* win9x */
1220 "Scan code -> vKey = %x (not VK_LSHIFT)\n", kL);
1221 kR = MapVirtualKeyEx(0x36, MAPVK_VSC_TO_VK_EX, kl);
1222 ok(kR == VK_RSHIFT || broken(kR == 0), /* win9x */
1223 "Scan code -> vKey = %x (not VK_RSHIFT)\n", kR);
1225 /* test that MAPVK_VSC_TO_VK prefers the non-numpad vkey if there's ambiguity */
1226 for (i = 0; i < sizeof(numpad_collisions)/sizeof(numpad_collisions[0]); i++)
1228 UINT numpad_scan = MapVirtualKeyEx(numpad_collisions[i][0], MAPVK_VK_TO_VSC, kl);
1229 UINT other_scan = MapVirtualKeyEx(numpad_collisions[i][1], MAPVK_VK_TO_VSC, kl);
1231 /* do they really collide for this layout? */
1232 if (numpad_scan && other_scan == numpad_scan)
1234 UINT vkey = MapVirtualKeyEx(numpad_scan, MAPVK_VSC_TO_VK, kl);
1235 ok(vkey != numpad_collisions[i][0],
1236 "Got numpad vKey %x for scan code %x when there was another choice\n",
1237 vkey, numpad_scan);
1242 static void test_ToUnicode(void)
1244 WCHAR wStr[2];
1245 BYTE state[256];
1246 const BYTE SC_RETURN = 0x1c, SC_TAB = 0x0f;
1247 const BYTE HIGHEST_BIT = 0x80;
1248 int i, ret;
1249 for(i=0; i<256; i++)
1250 state[i]=0;
1252 SetLastError(0xdeadbeef);
1253 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 2, 0);
1254 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1256 win_skip("ToUnicode is not implemented\n");
1257 return;
1260 ok(ret == 1, "ToUnicode for Return key didn't return 1 (was %i)\n", ret);
1261 if(ret == 1)
1262 ok(wStr[0]=='\r', "ToUnicode for CTRL + Return was %i (expected 13)\n", wStr[0]);
1263 state[VK_CONTROL] |= HIGHEST_BIT;
1264 state[VK_LCONTROL] |= HIGHEST_BIT;
1266 ret = ToUnicode(VK_TAB, SC_TAB, state, wStr, 2, 0);
1267 todo_wine ok(ret == 0, "ToUnicode for CTRL + Tab didn't return 0 (was %i)\n", ret);
1269 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 2, 0);
1270 ok(ret == 1, "ToUnicode for CTRL + Return didn't return 1 (was %i)\n", ret);
1271 if(ret == 1)
1272 ok(wStr[0]=='\n', "ToUnicode for CTRL + Return was %i (expected 10)\n", wStr[0]);
1274 state[VK_SHIFT] |= HIGHEST_BIT;
1275 state[VK_LSHIFT] |= HIGHEST_BIT;
1276 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 2, 0);
1277 todo_wine ok(ret == 0, "ToUnicode for CTRL + SHIFT + Return didn't return 0 (was %i)\n", ret);
1280 static void test_get_async_key_state(void)
1282 /* input value sanity checks */
1283 ok(0 == GetAsyncKeyState(1000000), "GetAsyncKeyState did not return 0\n");
1284 ok(0 == GetAsyncKeyState(-1000000), "GetAsyncKeyState did not return 0\n");
1287 START_TEST(input)
1289 init_function_pointers();
1291 if (pSendInput)
1293 test_Input_blackbox();
1294 test_Input_whitebox();
1296 else win_skip("SendInput is not available\n");
1298 test_keynames();
1299 test_mouse_ll_hook();
1300 test_key_map();
1301 test_ToUnicode();
1302 test_get_async_key_state();
1304 if(pGetMouseMovePointsEx)
1305 test_GetMouseMovePointsEx();
1306 else
1307 win_skip("GetMouseMovePointsEx is not available\n");