user32/tests: Recognize undefined characters in OemKeyScan tests.
[wine.git] / dlls / user32 / tests / input.c
blob030592b02f253757d950197bcc26a5db839cf6bc
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"
56 #include "winnls.h"
58 #include "wine/test.h"
60 /* globals */
61 static HWND hWndTest;
62 static LONG timetag = 0x10000000;
64 static struct {
65 LONG last_key_down;
66 LONG last_key_up;
67 LONG last_syskey_down;
68 LONG last_syskey_up;
69 LONG last_char;
70 LONG last_syschar;
71 LONG last_hook_down;
72 LONG last_hook_up;
73 LONG last_hook_syskey_down;
74 LONG last_hook_syskey_up;
75 BOOL expect_alt;
76 BOOL sendinput_broken;
77 } key_status;
79 static UINT (WINAPI *pSendInput) (UINT, INPUT*, size_t);
80 static int (WINAPI *pGetMouseMovePointsEx) (UINT, LPMOUSEMOVEPOINT, LPMOUSEMOVEPOINT, int, DWORD);
81 static UINT (WINAPI *pGetRawInputDeviceList) (PRAWINPUTDEVICELIST, PUINT, UINT);
83 #define MAXKEYEVENTS 12
84 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
85 and only one message */
87 /* keyboard message names, sorted as their value */
88 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
89 "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
91 /* keyevents, add more as needed */
92 typedef enum KEVtag
93 { ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
94 /* matching VK's */
95 static const int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
96 /* matching scan codes */
97 static const int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
98 /* matching updown events */
99 static const int GETFLAGS[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
100 /* matching descriptions */
101 static const char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
103 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
104 typedef struct
106 DWORD type;
107 union
109 MOUSEINPUT mi;
110 KEYBDINPUT ki;
111 HARDWAREINPUT hi;
112 } u;
113 } TEST_INPUT;
115 #define ADDTOINPUTS(kev) \
116 inputs[evtctr].type = INPUT_KEYBOARD; \
117 ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
118 ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
119 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETFLAGS[ kev]; \
120 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
121 ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
122 if( kev) evtctr++;
124 typedef struct {
125 UINT message;
126 WPARAM wParam;
127 LPARAM lParam;
128 } KMSG;
130 /*******************************************
131 * add new test sets here
132 * the software will make all combinations of the
133 * keyevent defined here
135 static const struct {
136 int nrkev;
137 KEV keydwn[MAXKEYEVENTS];
138 KEV keyup[MAXKEYEVENTS];
139 } testkeyset[]= {
140 { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
141 { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
142 { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
143 { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
144 { 0 } /* mark the end */
147 /**********************adapted from input.c **********************************/
149 static BYTE InputKeyStateTable[256];
150 static BYTE AsyncKeyStateTable[256];
151 static BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
152 or a WM_KEYUP message */
154 static void init_function_pointers(void)
156 HMODULE hdll = GetModuleHandleA("user32");
158 #define GET_PROC(func) \
159 p ## func = (void*)GetProcAddress(hdll, #func); \
160 if(!p ## func) \
161 trace("GetProcAddress(%s) failed\n", #func);
163 GET_PROC(SendInput)
164 GET_PROC(GetMouseMovePointsEx)
165 GET_PROC(GetRawInputDeviceList)
167 #undef GET_PROC
170 static int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
172 UINT message;
173 int VKey = GETVKEY[kev];
174 WORD flags;
176 flags = LOBYTE(GETSCAN[kev]);
177 if (GETFLAGS[kev] & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
179 if (GETFLAGS[kev] & KEYEVENTF_KEYUP )
181 message = WM_KEYUP;
182 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
183 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
184 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
185 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
186 (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
187 message = WM_SYSKEYUP;
188 TrackSysKey = 0;
190 InputKeyStateTable[VKey] &= ~0x80;
191 flags |= KF_REPEAT | KF_UP;
193 else
195 if (InputKeyStateTable[VKey] & 0x80) flags |= KF_REPEAT;
196 if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
197 InputKeyStateTable[VKey] |= 0x80;
198 AsyncKeyStateTable[VKey] |= 0x80;
200 message = WM_KEYDOWN;
201 if( (InputKeyStateTable[VK_MENU] & 0x80) &&
202 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
203 message = WM_SYSKEYDOWN;
204 TrackSysKey = VKey;
208 if (InputKeyStateTable[VK_MENU] & 0x80) flags |= KF_ALTDOWN;
210 if( plParam) *plParam = MAKELPARAM( 1, flags );
211 if( pwParam) *pwParam = VKey;
212 return message;
215 /****************************** end copy input.c ****************************/
218 * . prepare the keyevents for SendInputs
219 * . calculate the "expected" messages
220 * . Send the events to our window
221 * . retrieve the messages from the input queue
222 * . verify
224 static BOOL do_test( HWND hwnd, int seqnr, const KEV td[] )
226 INPUT inputs[MAXKEYEVENTS];
227 KMSG expmsg[MAXKEYEVENTS];
228 MSG msg;
229 char buf[100];
230 UINT evtctr=0;
231 int kmctr, i;
233 buf[0]='\0';
234 TrackSysKey=0; /* see input.c */
235 for( i = 0; i < MAXKEYEVENTS; i++) {
236 ADDTOINPUTS(td[i])
237 strcat(buf, getdesc[td[i]]);
238 if(td[i])
239 expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam));
240 else
241 expmsg[i].message = 0;
243 for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
245 ok( evtctr <= MAXKEYEVENTS, "evtctr is above MAXKEYEVENTS\n" );
246 if( evtctr != pSendInput(evtctr, &inputs[0], sizeof(INPUT)))
247 ok (FALSE, "SendInput failed to send some events\n");
248 i = 0;
249 if (winetest_debug > 1)
250 trace("======== key stroke sequence #%d: %s =============\n",
251 seqnr + 1, buf);
252 while( PeekMessageA(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
253 if (winetest_debug > 1)
254 trace("message[%d] %-15s wParam %04lx lParam %08lx time %x\n", i,
255 MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
256 if( i < kmctr ) {
257 ok( msg.message == expmsg[i].message &&
258 msg.wParam == expmsg[i].wParam &&
259 msg.lParam == expmsg[i].lParam,
260 "%u/%u: wrong message %x/%08lx/%08lx expected %s/%08lx/%08lx\n",
261 seqnr, i, msg.message, msg.wParam, msg.lParam,
262 MSGNAME[(expmsg[i]).message - WM_KEYFIRST], expmsg[i].wParam, expmsg[i].lParam );
264 i++;
266 if (winetest_debug > 1)
267 trace("%d messages retrieved\n", i);
268 if (!i && kmctr)
270 skip( "simulated keyboard input doesn't work\n" );
271 return FALSE;
273 ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
274 return TRUE;
277 /* test all combinations of the specified key events */
278 static BOOL TestASet( HWND hWnd, int nrkev, const KEV kevdwn[], const KEV kevup[] )
280 int i,j,k,l,m,n;
281 static int count=0;
282 KEV kbuf[MAXKEYEVENTS];
283 assert( nrkev==2 || nrkev==3);
284 for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
285 /* two keys involved gives 4 test cases */
286 if(nrkev==2) {
287 for(i=0;i<nrkev;i++) {
288 for(j=0;j<nrkev;j++) {
289 kbuf[0] = kevdwn[i];
290 kbuf[1] = kevdwn[1-i];
291 kbuf[2] = kevup[j];
292 kbuf[3] = kevup[1-j];
293 if (!do_test( hWnd, count++, kbuf)) return FALSE;
297 /* three keys involved gives 36 test cases */
298 if(nrkev==3){
299 for(i=0;i<nrkev;i++){
300 for(j=0;j<nrkev;j++){
301 if(j==i) continue;
302 for(k=0;k<nrkev;k++){
303 if(k==i || k==j) continue;
304 for(l=0;l<nrkev;l++){
305 for(m=0;m<nrkev;m++){
306 if(m==l) continue;
307 for(n=0;n<nrkev;n++){
308 if(n==l ||n==m) continue;
309 kbuf[0] = kevdwn[i];
310 kbuf[1] = kevdwn[j];
311 kbuf[2] = kevdwn[k];
312 kbuf[3] = kevup[l];
313 kbuf[4] = kevup[m];
314 kbuf[5] = kevup[n];
315 if (!do_test( hWnd, count++, kbuf)) return FALSE;
323 return TRUE;
326 /* test each set specified in the global testkeyset array */
327 static void TestSysKeys( HWND hWnd)
329 int i;
330 for(i=0; testkeyset[i].nrkev;i++)
331 if (!TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn, testkeyset[i].keyup)) break;
334 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
335 LPARAM lParam )
337 return DefWindowProcA( hWnd, msg, wParam, lParam );
340 static void test_Input_whitebox(void)
342 MSG msg;
343 WNDCLASSA wclass;
344 HANDLE hInstance = GetModuleHandleA( NULL );
346 wclass.lpszClassName = "InputSysKeyTestClass";
347 wclass.style = CS_HREDRAW | CS_VREDRAW;
348 wclass.lpfnWndProc = WndProc;
349 wclass.hInstance = hInstance;
350 wclass.hIcon = LoadIconA( 0, (LPCSTR)IDI_APPLICATION );
351 wclass.hCursor = LoadCursorA( NULL, (LPCSTR)IDC_ARROW );
352 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
353 wclass.lpszMenuName = 0;
354 wclass.cbClsExtra = 0;
355 wclass.cbWndExtra = 0;
356 RegisterClassA( &wclass );
357 /* create the test window that will receive the keystrokes */
358 hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
359 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
360 NULL, NULL, hInstance, NULL);
361 assert( hWndTest );
362 ShowWindow( hWndTest, SW_SHOW);
363 SetWindowPos( hWndTest, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
364 SetForegroundWindow( hWndTest );
365 UpdateWindow( hWndTest);
367 /* flush pending messages */
368 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
370 SetFocus( hWndTest );
371 TestSysKeys( hWndTest );
372 DestroyWindow(hWndTest);
375 static inline BOOL is_keyboard_message( UINT message )
377 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
380 static inline BOOL is_mouse_message( UINT message )
382 return (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST);
385 /* try to make sure pending X events have been processed before continuing */
386 static void empty_message_queue(void)
388 MSG msg;
389 int diff = 200;
390 int min_timeout = 50;
391 DWORD time = GetTickCount() + diff;
393 while (diff > 0)
395 if (MsgWaitForMultipleObjects(0, NULL, FALSE, min_timeout, QS_ALLINPUT) == WAIT_TIMEOUT) break;
396 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
398 if (is_keyboard_message(msg.message) || is_mouse_message(msg.message))
399 ok(msg.time != 0, "message %#x has time set to 0\n", msg.message);
401 TranslateMessage(&msg);
402 DispatchMessageA(&msg);
404 diff = time - GetTickCount();
408 struct transition_s {
409 WORD wVk;
410 BYTE before_state;
411 BYTE optional;
414 typedef enum {
415 sent=0x1,
416 posted=0x2,
417 parent=0x4,
418 wparam=0x8,
419 lparam=0x10,
420 defwinproc=0x20,
421 beginpaint=0x40,
422 optional=0x80,
423 hook=0x100,
424 winevent_hook=0x200
425 } msg_flags_t;
427 struct message {
428 UINT message; /* the WM_* code */
429 msg_flags_t flags; /* message props */
430 WPARAM wParam; /* expected value of wParam */
431 LPARAM lParam; /* expected value of lParam */
434 static const struct sendinput_test_s {
435 WORD wVk;
436 DWORD dwFlags;
437 BOOL _todo_wine;
438 struct transition_s expected_transitions[MAXKEYEVENTS+1];
439 struct message expected_messages[MAXKEYMESSAGES+1];
440 } sendinput_test[] = {
441 /* test ALT+F */
442 /* 0 */
443 {VK_LMENU, 0, FALSE, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
444 {{WM_SYSKEYDOWN, hook|wparam, VK_LMENU}, {WM_SYSKEYDOWN}, {0}}},
445 {'F', 0, FALSE, {{'F', 0x00}, {0}},
446 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN},
447 {WM_SYSCHAR},
448 {WM_SYSCOMMAND}, {0}}},
449 {'F', KEYEVENTF_KEYUP, FALSE, {{'F', 0x80}, {0}},
450 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
451 {VK_LMENU, KEYEVENTF_KEYUP, FALSE, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
452 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
454 /* test CTRL+O */
455 /* 4 */
456 {VK_LCONTROL, 0, FALSE, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
457 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
458 {'O', 0, FALSE, {{'O', 0x00}, {0}},
459 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
460 {'O', KEYEVENTF_KEYUP, FALSE, {{'O', 0x80}, {0}},
461 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
462 {VK_LCONTROL, KEYEVENTF_KEYUP, FALSE, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
463 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
465 /* test ALT+CTRL+X */
466 /* 8 */
467 {VK_LMENU, 0, FALSE, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
468 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN}, {0}}},
469 {VK_LCONTROL, 0, FALSE, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
470 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
471 {'X', 0, FALSE, {{'X', 0x00}, {0}},
472 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
473 {'X', KEYEVENTF_KEYUP, FALSE, {{'X', 0x80}, {0}},
474 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
475 {VK_LCONTROL, KEYEVENTF_KEYUP, FALSE, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
476 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
477 {VK_LMENU, KEYEVENTF_KEYUP, FALSE, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
478 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
480 /* test SHIFT+A */
481 /* 14 */
482 {VK_LSHIFT, 0, FALSE, {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
483 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
484 {'A', 0, FALSE, {{'A', 0x00}, {0}},
485 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
486 {'A', KEYEVENTF_KEYUP, FALSE, {{'A', 0x80}, {0}},
487 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
488 {VK_LSHIFT, KEYEVENTF_KEYUP, FALSE, {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
489 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
490 /* test L-SHIFT & R-SHIFT: */
491 /* RSHIFT == LSHIFT */
492 /* 18 */
493 {VK_RSHIFT, 0, FALSE,
494 /* recent windows versions (>= w2k3) correctly report an RSHIFT transition */
495 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00, TRUE}, {VK_RSHIFT, 0x00, TRUE}, {0}},
496 {{WM_KEYDOWN, hook|wparam, VK_RSHIFT},
497 {WM_KEYDOWN}, {0}}},
498 {VK_RSHIFT, KEYEVENTF_KEYUP, FALSE,
499 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80, TRUE}, {VK_RSHIFT, 0x80, TRUE}, {0}},
500 {{WM_KEYUP, hook, hook|wparam, VK_RSHIFT},
501 {WM_KEYUP}, {0}}},
503 /* LSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
504 /* 20 */
505 {VK_LSHIFT, KEYEVENTF_EXTENDEDKEY, FALSE,
506 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
507 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, LLKHF_EXTENDED},
508 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
509 {VK_LSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
510 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
511 {{WM_KEYUP, hook|wparam|lparam, VK_LSHIFT, LLKHF_UP|LLKHF_EXTENDED},
512 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
513 /* RSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
514 /* 22 */
515 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, FALSE,
516 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
517 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
518 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
519 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
520 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
521 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
522 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
524 /* Note about wparam for hook with generic key (VK_SHIFT, VK_CONTROL, VK_MENU):
525 win2k - sends to hook whatever we generated here
526 winXP+ - Attempts to convert key to L/R key but not always correct
528 /* SHIFT == LSHIFT */
529 /* 24 */
530 {VK_SHIFT, 0, FALSE,
531 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
532 {{WM_KEYDOWN, hook/* |wparam */|lparam, VK_SHIFT, 0},
533 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
534 {VK_SHIFT, KEYEVENTF_KEYUP, FALSE,
535 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
536 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_UP},
537 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
538 /* SHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
539 /* 26 */
540 {VK_SHIFT, KEYEVENTF_EXTENDEDKEY, FALSE,
541 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
542 {{WM_KEYDOWN, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_EXTENDED},
543 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
544 {VK_SHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
545 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
546 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_SHIFT, LLKHF_UP|LLKHF_EXTENDED},
547 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
549 /* test L-CONTROL & R-CONTROL: */
550 /* RCONTROL == LCONTROL */
551 /* 28 */
552 {VK_RCONTROL, 0, FALSE,
553 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
554 {{WM_KEYDOWN, hook|wparam, VK_RCONTROL},
555 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
556 {VK_RCONTROL, KEYEVENTF_KEYUP, FALSE,
557 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
558 {{WM_KEYUP, hook|wparam, VK_RCONTROL},
559 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
560 /* LCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
561 /* 30 */
562 {VK_LCONTROL, KEYEVENTF_EXTENDEDKEY, FALSE,
563 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
564 {{WM_KEYDOWN, hook|wparam|lparam, VK_LCONTROL, LLKHF_EXTENDED},
565 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
566 {VK_LCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
567 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
568 {{WM_KEYUP, hook|wparam|lparam, VK_LCONTROL, LLKHF_UP|LLKHF_EXTENDED},
569 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
570 /* RCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
571 /* 32 */
572 {VK_RCONTROL, KEYEVENTF_EXTENDEDKEY, FALSE,
573 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
574 {{WM_KEYDOWN, hook|wparam|lparam, VK_RCONTROL, LLKHF_EXTENDED},
575 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
576 {VK_RCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
577 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
578 {{WM_KEYUP, hook|wparam|lparam, VK_RCONTROL, LLKHF_UP|LLKHF_EXTENDED},
579 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
580 /* CONTROL == LCONTROL */
581 /* 34 */
582 {VK_CONTROL, 0, FALSE,
583 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
584 {{WM_KEYDOWN, hook/*|wparam, VK_CONTROL*/},
585 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
586 {VK_CONTROL, KEYEVENTF_KEYUP, FALSE,
587 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
588 {{WM_KEYUP, hook/*|wparam, VK_CONTROL*/},
589 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
590 /* CONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
591 /* 36 */
592 {VK_CONTROL, KEYEVENTF_EXTENDEDKEY, FALSE,
593 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
594 {{WM_KEYDOWN, hook/*|wparam*/|lparam, VK_CONTROL, LLKHF_EXTENDED},
595 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
596 {VK_CONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
597 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
598 {{WM_KEYUP, hook/*|wparam*/|lparam, VK_CONTROL, LLKHF_UP|LLKHF_EXTENDED},
599 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
601 /* test L-MENU & R-MENU: */
602 /* RMENU == LMENU */
603 /* 38 */
604 {VK_RMENU, 0, FALSE,
605 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
606 {{WM_SYSKEYDOWN, hook|wparam|optional, VK_LCONTROL},
607 {WM_SYSKEYDOWN, hook|wparam, VK_RMENU},
608 {WM_KEYDOWN, wparam|lparam|optional, VK_CONTROL, 0},
609 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
610 {VK_RMENU, KEYEVENTF_KEYUP, TRUE,
611 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
612 {{WM_KEYUP, hook|wparam|optional, VK_LCONTROL},
613 {WM_KEYUP, hook|wparam, VK_RMENU},
614 {WM_SYSKEYUP, wparam|lparam|optional, VK_CONTROL, KF_UP},
615 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
616 {WM_SYSCOMMAND, optional}, {0}}},
617 /* LMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
618 /* 40 */
619 {VK_LMENU, KEYEVENTF_EXTENDEDKEY, FALSE,
620 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {0}},
621 {{WM_SYSKEYDOWN, hook|wparam|lparam, VK_LMENU, LLKHF_EXTENDED},
622 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
623 {VK_LMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, TRUE,
624 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {0}},
625 {{WM_KEYUP, hook|wparam|lparam, VK_LMENU, LLKHF_UP|LLKHF_EXTENDED},
626 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
627 {WM_SYSCOMMAND}, {0}}},
628 /* RMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
629 /* 42 */
630 {VK_RMENU, KEYEVENTF_EXTENDEDKEY, FALSE,
631 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
632 {{WM_SYSKEYDOWN, hook|wparam|lparam|optional, VK_LCONTROL, 0},
633 {WM_SYSKEYDOWN, hook|wparam|lparam, VK_RMENU, LLKHF_EXTENDED},
634 {WM_KEYDOWN, wparam|lparam|optional, VK_CONTROL, 0},
635 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
636 {VK_RMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, TRUE,
637 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
638 {{WM_KEYUP, hook|wparam|lparam|optional, VK_LCONTROL, LLKHF_UP},
639 {WM_KEYUP, hook|wparam|lparam, VK_RMENU, LLKHF_UP|LLKHF_EXTENDED},
640 {WM_SYSKEYUP, wparam|lparam|optional, VK_CONTROL, KF_UP},
641 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
642 {WM_SYSCOMMAND, optional}, {0}}},
643 /* MENU == LMENU */
644 /* 44 */
645 {VK_MENU, 0, FALSE,
646 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
647 {{WM_SYSKEYDOWN, hook/*|wparam, VK_MENU*/},
648 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
649 {VK_MENU, KEYEVENTF_KEYUP, TRUE,
650 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
651 {{WM_KEYUP, hook/*|wparam, VK_MENU*/},
652 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
653 {WM_SYSCOMMAND}, {0}}},
654 /* MENU | KEYEVENTF_EXTENDEDKEY == RMENU */
655 /* 46 */
656 {VK_MENU, KEYEVENTF_EXTENDEDKEY, FALSE,
657 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {VK_CONTROL, 0x00, 1}, {VK_LCONTROL, 0x01, 1}, {0}},
658 {{WM_SYSKEYDOWN, hook|wparam|lparam|optional, VK_CONTROL, 0},
659 {WM_SYSKEYDOWN, hook/*|wparam*/|lparam, VK_MENU, LLKHF_EXTENDED},
660 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
661 {VK_MENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, TRUE,
662 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {VK_CONTROL, 0x81, 1}, {VK_LCONTROL, 0x80, 1}, {0}},
663 {{WM_KEYUP, hook|wparam|lparam|optional, VK_CONTROL, LLKHF_UP},
664 {WM_KEYUP, hook/*|wparam*/|lparam, VK_MENU, LLKHF_UP|LLKHF_EXTENDED},
665 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
666 {WM_SYSCOMMAND}, {0}}},
668 /* test LSHIFT & RSHIFT */
669 /* 48 */
670 {VK_LSHIFT, 0, FALSE,
671 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
672 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, 0},
673 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
674 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, FALSE,
675 {{VK_RSHIFT, 0x00}, {0}},
676 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
677 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
678 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, FALSE,
679 {{VK_RSHIFT, 0x80}, {0}},
680 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
681 {WM_KEYUP, optional}, {0}}},
682 {VK_LSHIFT, KEYEVENTF_KEYUP, FALSE,
683 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
684 {{WM_KEYUP, hook|wparam, VK_LSHIFT},
685 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
687 {0, 0, FALSE, {{0}}, {{0}}} /* end */
690 static struct message sent_messages[MAXKEYMESSAGES];
691 static UINT sent_messages_cnt;
693 /* Verify that only specified key state transitions occur */
694 static void compare_and_check(int id, BYTE *ks1, BYTE *ks2, const struct sendinput_test_s *test)
696 int i, failcount = 0;
697 const struct transition_s *t = test->expected_transitions;
698 UINT actual_cnt = 0;
699 const struct message *expected = test->expected_messages;
701 while (t->wVk) {
702 BOOL matched = ((ks1[t->wVk]&0x80) == (t->before_state&0x80)
703 && (ks2[t->wVk]&0x80) == (~t->before_state&0x80));
705 if (!matched && !t->optional && test->_todo_wine)
707 failcount++;
708 todo_wine {
709 ok(matched, "%2d (%x/%x): %02x from %02x -> %02x "
710 "instead of %02x -> %02x\n", id, test->wVk, test->dwFlags,
711 t->wVk, ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
712 ~t->before_state&0x80);
714 } else {
715 ok(matched || t->optional, "%2d (%x/%x): %02x from %02x -> %02x "
716 "instead of %02x -> %02x\n", id, test->wVk, test->dwFlags,
717 t->wVk, ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
718 ~t->before_state&0x80);
720 ks2[t->wVk] = ks1[t->wVk]; /* clear the match */
721 t++;
723 for (i = 0; i < 256; i++)
724 if (ks2[i] != ks1[i] && test->_todo_wine)
726 failcount++;
727 todo_wine
728 ok(FALSE, "%2d (%x/%x): %02x from %02x -> %02x unexpected\n",
729 id, test->wVk, test->dwFlags, i, ks1[i], ks2[i]);
731 else
732 ok(ks2[i] == ks1[i], "%2d (%x/%x): %02x from %02x -> %02x unexpected\n",
733 id, test->wVk, test->dwFlags, i, ks1[i], ks2[i]);
735 while (expected->message && actual_cnt < sent_messages_cnt)
737 const struct message *actual = &sent_messages[actual_cnt];
739 if (expected->message == actual->message)
741 if (expected->flags & wparam)
743 if ((expected->flags & optional) && (expected->wParam != actual->wParam))
745 expected++;
746 continue;
748 if (expected->wParam != actual->wParam && test->_todo_wine)
750 failcount++;
751 todo_wine
752 ok(FALSE, "%2d (%x/%x): in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
753 id, test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
755 else
756 ok(expected->wParam == actual->wParam,
757 "%2d (%x/%x): in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
758 id, test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
760 if (expected->flags & lparam)
762 if (expected->lParam != actual->lParam && test->_todo_wine)
764 failcount++;
765 todo_wine
766 ok(FALSE, "%2d (%x/%x): in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
767 id, test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
769 else
770 ok(expected->lParam == actual->lParam,
771 "%2d (%x/%x): in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
772 id, test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
774 ok((expected->flags & hook) == (actual->flags & hook),
775 "%2d (%x/%x): the msg 0x%04x should have been sent by a hook\n",
776 id, test->wVk, test->dwFlags, expected->message);
779 else if (expected->flags & optional)
781 expected++;
782 continue;
784 /* NT4 doesn't send SYSKEYDOWN/UP to hooks, only KEYDOWN/UP */
785 else if ((expected->flags & hook) &&
786 (expected->message == WM_SYSKEYDOWN || expected->message == WM_SYSKEYUP) &&
787 (actual->message == expected->message - 4))
789 ok((expected->flags & hook) == (actual->flags & hook),
790 "%2d (%x/%x): the msg 0x%04x should have been sent by a hook\n",
791 id, test->wVk, test->dwFlags, expected->message);
793 /* For VK_RMENU, at least localized Win2k/XP sends KEYDOWN/UP
794 * instead of SYSKEYDOWN/UP to the WNDPROC */
795 else if (test->wVk == VK_RMENU && !(expected->flags & hook) &&
796 (expected->message == WM_SYSKEYDOWN || expected->message == WM_SYSKEYUP) &&
797 (actual->message == expected->message - 4))
799 ok(expected->wParam == actual->wParam && expected->lParam == actual->lParam,
800 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
801 id, test->wVk, test->dwFlags, expected->message, actual->message);
803 else if (test->_todo_wine)
805 failcount++;
806 todo_wine
807 ok(FALSE,
808 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
809 id, test->wVk, test->dwFlags, expected->message, actual->message);
811 else
812 ok(FALSE,
813 "%2d (%x/%x): the msg 0x%04x was expected, but got msg 0x%04x instead\n",
814 id, test->wVk, test->dwFlags, expected->message, actual->message);
816 actual_cnt++;
817 expected++;
819 /* skip all optional trailing messages */
820 while (expected->message && (expected->flags & optional))
821 expected++;
824 if (expected->message || actual_cnt < sent_messages_cnt)
826 if (test->_todo_wine)
828 failcount++;
829 todo_wine
830 ok(FALSE, "%2d (%x/%x): the msg sequence is not complete: expected %04x - actual %04x\n",
831 id, test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
833 else
834 ok(FALSE, "%2d (%x/%x): the msg sequence is not complete: expected %04x - actual %04x\n",
835 id, test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
838 if( test->_todo_wine && !failcount) /* succeeded yet marked todo */
839 todo_wine
840 ok(TRUE, "%2d (%x/%x): marked \"todo_wine\" but succeeds\n", id, test->wVk, test->dwFlags);
842 sent_messages_cnt = 0;
845 /* WndProc2 checks that we get at least the messages specified */
846 static LRESULT CALLBACK WndProc2(HWND hWnd, UINT Msg, WPARAM wParam,
847 LPARAM lParam)
849 if (winetest_debug > 1) trace("MSG: %8x W:%8lx L:%8lx\n", Msg, wParam, lParam);
851 if (Msg != WM_PAINT &&
852 Msg != WM_NCPAINT &&
853 Msg != WM_SYNCPAINT &&
854 Msg != WM_ERASEBKGND &&
855 Msg != WM_NCHITTEST &&
856 Msg != WM_GETTEXT &&
857 Msg != WM_GETICON &&
858 Msg != WM_IME_SELECT &&
859 Msg != WM_DEVICECHANGE &&
860 Msg != WM_TIMECHANGE)
862 ok(sent_messages_cnt < MAXKEYMESSAGES, "Too many messages\n");
863 if (sent_messages_cnt < MAXKEYMESSAGES)
865 sent_messages[sent_messages_cnt].message = Msg;
866 sent_messages[sent_messages_cnt].flags = 0;
867 sent_messages[sent_messages_cnt].wParam = wParam;
868 sent_messages[sent_messages_cnt++].lParam = HIWORD(lParam) & (KF_UP|KF_EXTENDED);
871 return DefWindowProcA(hWnd, Msg, wParam, lParam);
874 static LRESULT CALLBACK hook_proc(int code, WPARAM wparam, LPARAM lparam)
876 KBDLLHOOKSTRUCT *hook_info = (KBDLLHOOKSTRUCT *)lparam;
878 if (code == HC_ACTION)
880 ok(sent_messages_cnt < MAXKEYMESSAGES, "Too many messages\n");
881 if (sent_messages_cnt < MAXKEYMESSAGES)
883 sent_messages[sent_messages_cnt].message = wparam;
884 sent_messages[sent_messages_cnt].flags = hook;
885 sent_messages[sent_messages_cnt].wParam = hook_info->vkCode;
886 sent_messages[sent_messages_cnt++].lParam = hook_info->flags & (LLKHF_UP|LLKHF_EXTENDED);
889 if(0) /* For some reason not stable on Wine */
891 if (wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
892 ok(!(GetAsyncKeyState(hook_info->vkCode) & 0x8000), "key %x should be up\n", hook_info->vkCode);
893 else if (wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
894 ok(GetAsyncKeyState(hook_info->vkCode) & 0x8000, "key %x should be down\n", hook_info->vkCode);
897 if (winetest_debug > 1)
898 trace("Hook: w=%lx vk:%8x sc:%8x fl:%8x %lx\n", wparam,
899 hook_info->vkCode, hook_info->scanCode, hook_info->flags, hook_info->dwExtraInfo);
901 return CallNextHookEx( 0, code, wparam, lparam );
903 static void test_Input_blackbox(void)
905 TEST_INPUT i;
906 int ii;
907 BYTE ks1[256], ks2[256];
908 LONG_PTR prevWndProc;
909 HWND window;
910 HHOOK hook;
912 if (GetKeyboardLayout(0) != (HKL)(ULONG_PTR)0x04090409)
914 skip("Skipping Input_blackbox test on non-US keyboard\n");
915 return;
917 window = CreateWindowA("Static", NULL, WS_POPUP|WS_HSCROLL|WS_VSCROLL
918 |WS_VISIBLE, 0, 0, 200, 60, NULL, NULL,
919 NULL, NULL);
920 ok(window != NULL, "error: %d\n", (int) GetLastError());
921 SetWindowPos( window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
922 SetForegroundWindow( window );
924 if (!(hook = SetWindowsHookExA(WH_KEYBOARD_LL, hook_proc, GetModuleHandleA( NULL ), 0)))
926 DestroyWindow(window);
927 win_skip("WH_KEYBOARD_LL is not supported\n");
928 return;
931 /* must process all initial messages, otherwise X11DRV_KeymapNotify unsets
932 * key state set by SendInput(). */
933 empty_message_queue();
935 prevWndProc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR) WndProc2);
936 ok(prevWndProc != 0 || GetLastError() == 0, "error: %d\n", (int) GetLastError());
938 i.type = INPUT_KEYBOARD;
939 i.u.ki.time = 0;
940 i.u.ki.dwExtraInfo = 0;
942 for (ii = 0; ii < sizeof(sendinput_test)/sizeof(struct sendinput_test_s)-1;
943 ii++) {
944 GetKeyboardState(ks1);
945 i.u.ki.wScan = ii+1 /* useful for debugging */;
946 i.u.ki.dwFlags = sendinput_test[ii].dwFlags;
947 i.u.ki.wVk = sendinput_test[ii].wVk;
948 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
949 empty_message_queue();
950 GetKeyboardState(ks2);
951 if (!ii && sent_messages_cnt <= 1 && !memcmp( ks1, ks2, sizeof(ks1) ))
953 win_skip( "window doesn't receive the queued input\n" );
954 /* release the key */
955 i.u.ki.dwFlags |= KEYEVENTF_KEYUP;
956 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
957 break;
959 compare_and_check(ii, ks1, ks2, &sendinput_test[ii]);
962 empty_message_queue();
963 DestroyWindow(window);
964 UnhookWindowsHookEx(hook);
967 static void reset_key_status(void)
969 key_status.last_key_down = -1;
970 key_status.last_key_up = -1;
971 key_status.last_syskey_down = -1;
972 key_status.last_syskey_up = -1;
973 key_status.last_char = -1;
974 key_status.last_syschar = -1;
975 key_status.last_hook_down = -1;
976 key_status.last_hook_up = -1;
977 key_status.last_hook_syskey_down = -1;
978 key_status.last_hook_syskey_up = -1;
979 key_status.expect_alt = FALSE;
980 key_status.sendinput_broken = FALSE;
983 static void test_unicode_keys(HWND hwnd, HHOOK hook)
985 TEST_INPUT inputs[2];
986 MSG msg;
988 /* init input data that never changes */
989 inputs[1].type = inputs[0].type = INPUT_KEYBOARD;
990 inputs[1].u.ki.dwExtraInfo = inputs[0].u.ki.dwExtraInfo = 0;
991 inputs[1].u.ki.time = inputs[0].u.ki.time = 0;
993 /* pressing & releasing a single unicode character */
994 inputs[0].u.ki.wVk = 0;
995 inputs[0].u.ki.wScan = 0x3c0;
996 inputs[0].u.ki.dwFlags = KEYEVENTF_UNICODE;
998 reset_key_status();
999 pSendInput(1, (INPUT*)inputs, sizeof(INPUT));
1000 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1001 if(msg.message == WM_KEYDOWN && msg.wParam == VK_PACKET){
1002 TranslateMessage(&msg);
1004 DispatchMessageW(&msg);
1006 if(!key_status.sendinput_broken){
1007 ok(key_status.last_key_down == VK_PACKET,
1008 "Last keydown msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_key_down);
1009 ok(key_status.last_char == 0x3c0,
1010 "Last char msg wparam should have been 0x3c0 (was: 0x%x)\n", key_status.last_char);
1011 if(hook)
1012 ok(key_status.last_hook_down == 0x3c0,
1013 "Last hookdown msg should have been 0x3c0, was: 0x%x\n", key_status.last_hook_down);
1016 inputs[1].u.ki.wVk = 0;
1017 inputs[1].u.ki.wScan = 0x3c0;
1018 inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1020 reset_key_status();
1021 pSendInput(1, (INPUT*)(inputs+1), sizeof(INPUT));
1022 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1023 if(msg.message == WM_KEYDOWN && msg.wParam == VK_PACKET){
1024 TranslateMessage(&msg);
1026 DispatchMessageW(&msg);
1028 if(!key_status.sendinput_broken){
1029 ok(key_status.last_key_up == VK_PACKET,
1030 "Last keyup msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_key_up);
1031 if(hook)
1032 ok(key_status.last_hook_up == 0x3c0,
1033 "Last hookup msg should have been 0x3c0, was: 0x%x\n", key_status.last_hook_up);
1036 /* holding alt, pressing & releasing a unicode character, releasing alt */
1037 inputs[0].u.ki.wVk = VK_LMENU;
1038 inputs[0].u.ki.wScan = 0;
1039 inputs[0].u.ki.dwFlags = 0;
1041 inputs[1].u.ki.wVk = 0;
1042 inputs[1].u.ki.wScan = 0x3041;
1043 inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE;
1045 reset_key_status();
1046 key_status.expect_alt = TRUE;
1047 pSendInput(2, (INPUT*)inputs, sizeof(INPUT));
1048 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1049 if(msg.message == WM_SYSKEYDOWN && msg.wParam == VK_PACKET){
1050 TranslateMessage(&msg);
1052 DispatchMessageW(&msg);
1054 if(!key_status.sendinput_broken){
1055 ok(key_status.last_syskey_down == VK_PACKET,
1056 "Last syskeydown msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_syskey_down);
1057 ok(key_status.last_syschar == 0x3041,
1058 "Last syschar msg should have been 0x3041 (was: 0x%x)\n", key_status.last_syschar);
1059 if(hook)
1060 ok(key_status.last_hook_syskey_down == 0x3041,
1061 "Last hooksysdown msg should have been 0x3041, was: 0x%x\n", key_status.last_hook_syskey_down);
1064 inputs[1].u.ki.wVk = 0;
1065 inputs[1].u.ki.wScan = 0x3041;
1066 inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
1068 inputs[0].u.ki.wVk = VK_LMENU;
1069 inputs[0].u.ki.wScan = 0;
1070 inputs[0].u.ki.dwFlags = KEYEVENTF_KEYUP;
1072 reset_key_status();
1073 key_status.expect_alt = TRUE;
1074 pSendInput(2, (INPUT*)inputs, sizeof(INPUT));
1075 while(PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)){
1076 if(msg.message == WM_SYSKEYDOWN && msg.wParam == VK_PACKET){
1077 TranslateMessage(&msg);
1079 DispatchMessageW(&msg);
1081 if(!key_status.sendinput_broken){
1082 ok(key_status.last_key_up == VK_PACKET,
1083 "Last keyup msg should have been VK_PACKET[0x%04x] (was: 0x%x)\n", VK_PACKET, key_status.last_key_up);
1084 if(hook)
1085 ok(key_status.last_hook_up == 0x3041,
1086 "Last hook up msg should have been 0x3041, was: 0x%x\n", key_status.last_hook_up);
1090 static LRESULT CALLBACK unicode_wnd_proc( HWND hWnd, UINT msg, WPARAM wParam,
1091 LPARAM lParam )
1093 switch(msg){
1094 case WM_KEYDOWN:
1095 key_status.last_key_down = wParam;
1096 break;
1097 case WM_SYSKEYDOWN:
1098 key_status.last_syskey_down = wParam;
1099 break;
1100 case WM_KEYUP:
1101 key_status.last_key_up = wParam;
1102 break;
1103 case WM_SYSKEYUP:
1104 key_status.last_syskey_up = wParam;
1105 break;
1106 case WM_CHAR:
1107 key_status.last_char = wParam;
1108 break;
1109 case WM_SYSCHAR:
1110 key_status.last_syschar = wParam;
1111 break;
1113 return DefWindowProcW(hWnd, msg, wParam, lParam);
1116 static LRESULT CALLBACK llkbd_unicode_hook(int nCode, WPARAM wParam, LPARAM lParam)
1118 if(nCode == HC_ACTION){
1119 LPKBDLLHOOKSTRUCT info = (LPKBDLLHOOKSTRUCT)lParam;
1120 if(!info->vkCode){
1121 key_status.sendinput_broken = TRUE;
1122 win_skip("SendInput doesn't support unicode on this platform\n");
1123 }else{
1124 if(key_status.expect_alt){
1125 ok(info->vkCode == VK_LMENU, "vkCode should have been VK_LMENU[0x%04x], was: 0x%x\n", VK_LMENU, info->vkCode);
1126 key_status.expect_alt = FALSE;
1127 }else
1128 ok(info->vkCode == VK_PACKET, "vkCode should have been VK_PACKET[0x%04x], was: 0x%x\n", VK_PACKET, info->vkCode);
1130 switch(wParam){
1131 case WM_KEYDOWN:
1132 key_status.last_hook_down = info->scanCode;
1133 break;
1134 case WM_KEYUP:
1135 key_status.last_hook_up = info->scanCode;
1136 break;
1137 case WM_SYSKEYDOWN:
1138 key_status.last_hook_syskey_down = info->scanCode;
1139 break;
1140 case WM_SYSKEYUP:
1141 key_status.last_hook_syskey_up = info->scanCode;
1142 break;
1145 return CallNextHookEx(NULL, nCode, wParam, lParam);
1148 static void test_Input_unicode(void)
1150 WCHAR classNameW[] = {'I','n','p','u','t','U','n','i','c','o','d','e',
1151 'K','e','y','T','e','s','t','C','l','a','s','s',0};
1152 WCHAR windowNameW[] = {'I','n','p','u','t','U','n','i','c','o','d','e',
1153 'K','e','y','T','e','s','t',0};
1154 MSG msg;
1155 WNDCLASSW wclass;
1156 HANDLE hInstance = GetModuleHandleW(NULL);
1157 HHOOK hook;
1158 HMODULE hModuleImm32;
1159 BOOL (WINAPI *pImmDisableIME)(DWORD);
1161 wclass.lpszClassName = classNameW;
1162 wclass.style = CS_HREDRAW | CS_VREDRAW;
1163 wclass.lpfnWndProc = unicode_wnd_proc;
1164 wclass.hInstance = hInstance;
1165 wclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_APPLICATION);
1166 wclass.hCursor = LoadCursorW( NULL, (LPCWSTR)IDC_ARROW);
1167 wclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1168 wclass.lpszMenuName = 0;
1169 wclass.cbClsExtra = 0;
1170 wclass.cbWndExtra = 0;
1171 if(!RegisterClassW(&wclass)){
1172 win_skip("Unicode functions not supported\n");
1173 return;
1176 hModuleImm32 = LoadLibraryA("imm32.dll");
1177 if (hModuleImm32) {
1178 pImmDisableIME = (void *)GetProcAddress(hModuleImm32, "ImmDisableIME");
1179 if (pImmDisableIME)
1180 pImmDisableIME(0);
1182 pImmDisableIME = NULL;
1183 FreeLibrary(hModuleImm32);
1185 /* create the test window that will receive the keystrokes */
1186 hWndTest = CreateWindowW(wclass.lpszClassName, windowNameW,
1187 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
1188 NULL, NULL, hInstance, NULL);
1190 assert(hWndTest);
1191 assert(IsWindowUnicode(hWndTest));
1193 hook = SetWindowsHookExW(WH_KEYBOARD_LL, llkbd_unicode_hook, GetModuleHandleW(NULL), 0);
1194 if(!hook)
1195 win_skip("unable to set WH_KEYBOARD_LL hook\n");
1197 ShowWindow(hWndTest, SW_SHOW);
1198 SetWindowPos(hWndTest, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1199 SetForegroundWindow(hWndTest);
1200 UpdateWindow(hWndTest);
1202 /* flush pending messages */
1203 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageW(&msg);
1205 SetFocus(hWndTest);
1207 test_unicode_keys(hWndTest, hook);
1209 if(hook)
1210 UnhookWindowsHookEx(hook);
1211 DestroyWindow(hWndTest);
1214 static void test_keynames(void)
1216 int i, len;
1217 char buff[256];
1219 for (i = 0; i < 512; i++)
1221 strcpy(buff, "----");
1222 len = GetKeyNameTextA(i << 16, buff, sizeof(buff));
1223 ok(len || !buff[0], "%d: Buffer is not zeroed\n", i);
1227 static POINT pt_old, pt_new;
1228 static BOOL clipped;
1229 #define STEP 3
1231 static LRESULT CALLBACK hook_proc1( int code, WPARAM wparam, LPARAM lparam )
1233 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
1234 POINT pt, pt1;
1236 if (code == HC_ACTION)
1238 /* This is our new cursor position */
1239 pt_new = hook->pt;
1240 /* Should return previous position */
1241 GetCursorPos(&pt);
1242 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
1244 /* Should set new position until hook chain is finished. */
1245 pt.x = pt_old.x + STEP;
1246 pt.y = pt_old.y + STEP;
1247 SetCursorPos(pt.x, pt.y);
1248 GetCursorPos(&pt1);
1249 if (clipped)
1250 ok(pt1.x == pt_old.x && pt1.y == pt_old.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
1251 else
1252 ok(pt1.x == pt.x && pt1.y == pt.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
1254 return CallNextHookEx( 0, code, wparam, lparam );
1257 static LRESULT CALLBACK hook_proc2( int code, WPARAM wparam, LPARAM lparam )
1259 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
1260 POINT pt;
1262 if (code == HC_ACTION)
1264 ok(hook->pt.x == pt_new.x && hook->pt.y == pt_new.y,
1265 "Wrong hook coords: (%d %d) != (%d,%d)\n", hook->pt.x, hook->pt.y, pt_new.x, pt_new.y);
1267 /* Should match position set above */
1268 GetCursorPos(&pt);
1269 if (clipped)
1270 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
1271 else
1272 ok(pt.x == pt_old.x +STEP && pt.y == pt_old.y +STEP, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
1274 return CallNextHookEx( 0, code, wparam, lparam );
1277 static void test_mouse_ll_hook(void)
1279 HWND hwnd;
1280 HHOOK hook1, hook2;
1281 POINT pt_org, pt;
1282 RECT rc;
1284 GetCursorPos(&pt_org);
1285 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
1286 10, 10, 200, 200, NULL, NULL, NULL, NULL);
1287 SetCursorPos(100, 100);
1289 if (!(hook2 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc2, GetModuleHandleA(0), 0)))
1291 win_skip( "cannot set MOUSE_LL hook\n" );
1292 goto done;
1294 hook1 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc1, GetModuleHandleA(0), 0);
1296 GetCursorPos(&pt_old);
1297 mouse_event(MOUSEEVENTF_MOVE, -STEP, 0, 0, 0);
1298 GetCursorPos(&pt_old);
1299 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);
1300 mouse_event(MOUSEEVENTF_MOVE, +STEP, 0, 0, 0);
1301 GetCursorPos(&pt_old);
1302 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);
1303 mouse_event(MOUSEEVENTF_MOVE, 0, -STEP, 0, 0);
1304 GetCursorPos(&pt_old);
1305 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);
1306 mouse_event(MOUSEEVENTF_MOVE, 0, +STEP, 0, 0);
1307 GetCursorPos(&pt_old);
1308 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);
1310 SetRect(&rc, 50, 50, 151, 151);
1311 ClipCursor(&rc);
1312 clipped = TRUE;
1314 SetCursorPos(40, 40);
1315 GetCursorPos(&pt_old);
1316 ok(pt_old.x == 50 && pt_old.y == 50, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1317 SetCursorPos(160, 160);
1318 GetCursorPos(&pt_old);
1319 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1320 mouse_event(MOUSEEVENTF_MOVE, +STEP, +STEP, 0, 0);
1321 GetCursorPos(&pt_old);
1322 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
1324 clipped = FALSE;
1325 pt_new.x = pt_new.y = 150;
1326 ClipCursor(NULL);
1327 UnhookWindowsHookEx(hook1);
1329 /* Now check that mouse buttons do not change mouse position
1330 if we don't have MOUSEEVENTF_MOVE flag specified. */
1332 /* We reusing the same hook callback, so make it happy */
1333 pt_old.x = pt_new.x - STEP;
1334 pt_old.y = pt_new.y - STEP;
1335 mouse_event(MOUSEEVENTF_LEFTUP, 123, 456, 0, 0);
1336 GetCursorPos(&pt);
1337 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1338 mouse_event(MOUSEEVENTF_RIGHTUP, 456, 123, 0, 0);
1339 GetCursorPos(&pt);
1340 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1342 mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 123, 456, 0, 0);
1343 GetCursorPos(&pt);
1344 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1345 mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 456, 123, 0, 0);
1346 GetCursorPos(&pt);
1347 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
1349 UnhookWindowsHookEx(hook2);
1350 done:
1351 DestroyWindow(hwnd);
1352 SetCursorPos(pt_org.x, pt_org.y);
1355 static void test_GetMouseMovePointsEx(void)
1357 #define BUFLIM 64
1358 #define MYERROR 0xdeadbeef
1359 int count, retval;
1360 MOUSEMOVEPOINT in;
1361 MOUSEMOVEPOINT out[200];
1362 POINT point;
1364 /* Get a valid content for the input struct */
1365 if(!GetCursorPos(&point)) {
1366 win_skip("GetCursorPos() failed with error %u\n", GetLastError());
1367 return;
1369 memset(&in, 0, sizeof(MOUSEMOVEPOINT));
1370 in.x = point.x;
1371 in.y = point.y;
1373 /* test first parameter
1374 * everything different than sizeof(MOUSEMOVEPOINT)
1375 * is expected to fail with ERROR_INVALID_PARAMETER
1377 SetLastError(MYERROR);
1378 retval = pGetMouseMovePointsEx(0, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1379 if (retval == ERROR_INVALID_PARAMETER)
1381 win_skip( "GetMouseMovePointsEx broken on WinME\n" );
1382 return;
1384 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1385 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1386 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1388 SetLastError(MYERROR);
1389 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1390 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1391 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1392 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1394 SetLastError(MYERROR);
1395 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)+1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1396 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1397 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1398 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1400 /* test second and third parameter
1402 SetLastError(MYERROR);
1403 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1404 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1405 ok(GetLastError() == ERROR_NOACCESS || GetLastError() == MYERROR,
1406 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1408 SetLastError(MYERROR);
1409 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1410 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1411 ok(ERROR_NOACCESS == GetLastError(),
1412 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1414 SetLastError(MYERROR);
1415 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1416 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1417 ok(ERROR_NOACCESS == GetLastError(),
1418 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1420 SetLastError(MYERROR);
1421 count = 0;
1422 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, count, GMMP_USE_DISPLAY_POINTS);
1423 if (retval == -1)
1424 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1425 else
1426 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1428 /* test fourth parameter
1429 * a value higher than 64 is expected to fail with ERROR_INVALID_PARAMETER
1431 SetLastError(MYERROR);
1432 count = -1;
1433 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1434 ok(retval == count, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1435 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1436 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1438 SetLastError(MYERROR);
1439 count = 0;
1440 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1441 if (retval == -1)
1442 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1443 else
1444 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1446 SetLastError(MYERROR);
1447 count = BUFLIM;
1448 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1449 if (retval == -1)
1450 ok(GetLastError() == ERROR_POINT_NOT_FOUND, "unexpected error %u\n", GetLastError());
1451 else
1452 ok((0 <= retval) && (retval <= count), "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1454 SetLastError(MYERROR);
1455 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1456 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1457 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1458 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1460 /* it was not possible to force an error with the fifth parameter on win2k */
1462 /* test combinations of wrong parameters to see which error wins */
1463 SetLastError(MYERROR);
1464 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1465 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1466 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1467 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1469 SetLastError(MYERROR);
1470 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1471 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1472 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1473 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1475 SetLastError(MYERROR);
1476 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1477 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1478 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1479 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1481 SetLastError(MYERROR);
1482 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1483 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1484 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == MYERROR,
1485 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1487 #undef BUFLIM
1488 #undef MYERROR
1491 static void test_GetRawInputDeviceList(void)
1493 RAWINPUTDEVICELIST devices[32];
1494 UINT ret, oret, devcount, odevcount;
1495 DWORD err;
1497 SetLastError(0xdeadbeef);
1498 ret = pGetRawInputDeviceList(NULL, NULL, 0);
1499 err = GetLastError();
1500 ok(ret == -1, "expected -1, got %d\n", ret);
1501 ok(err == ERROR_INVALID_PARAMETER, "expected 87, got %d\n", err);
1503 SetLastError(0xdeadbeef);
1504 ret = pGetRawInputDeviceList(NULL, NULL, sizeof(devices[0]));
1505 err = GetLastError();
1506 ok(ret == -1, "expected -1, got %d\n", ret);
1507 ok(err == ERROR_NOACCESS, "expected 998, got %d\n", err);
1509 devcount = 0;
1510 ret = pGetRawInputDeviceList(NULL, &devcount, sizeof(devices[0]));
1511 ok(ret == 0, "expected 0, got %d\n", ret);
1512 ok(devcount > 0, "expected non-zero\n");
1514 SetLastError(0xdeadbeef);
1515 devcount = 0;
1516 ret = pGetRawInputDeviceList(devices, &devcount, sizeof(devices[0]));
1517 err = GetLastError();
1518 ok(ret == -1, "expected -1, got %d\n", ret);
1519 ok(err == ERROR_INSUFFICIENT_BUFFER, "expected 122, got %d\n", err);
1520 ok(devcount > 0, "expected non-zero\n");
1522 /* devcount contains now the correct number of devices */
1523 ret = pGetRawInputDeviceList(devices, &devcount, sizeof(devices[0]));
1524 ok(ret > 0, "expected non-zero\n");
1526 /* check if variable changes from larger to smaller value */
1527 devcount = odevcount = sizeof(devices) / sizeof(devices[0]);
1528 oret = ret = pGetRawInputDeviceList(devices, &odevcount, sizeof(devices[0]));
1529 ok(ret > 0, "expected non-zero\n");
1530 ok(devcount == odevcount, "expected %d, got %d\n", devcount, odevcount);
1531 devcount = odevcount;
1532 odevcount = sizeof(devices) / sizeof(devices[0]);
1533 ret = pGetRawInputDeviceList(NULL, &odevcount, sizeof(devices[0]));
1534 ok(ret == 0, "expected 0, got %d\n", ret);
1535 ok(odevcount == oret, "expected %d, got %d\n", oret, odevcount);
1538 static void test_key_map(void)
1540 HKL kl = GetKeyboardLayout(0);
1541 UINT kL, kR, s, sL;
1542 int i;
1543 static const UINT numpad_collisions[][2] = {
1544 { VK_NUMPAD0, VK_INSERT },
1545 { VK_NUMPAD1, VK_END },
1546 { VK_NUMPAD2, VK_DOWN },
1547 { VK_NUMPAD3, VK_NEXT },
1548 { VK_NUMPAD4, VK_LEFT },
1549 { VK_NUMPAD6, VK_RIGHT },
1550 { VK_NUMPAD7, VK_HOME },
1551 { VK_NUMPAD8, VK_UP },
1552 { VK_NUMPAD9, VK_PRIOR },
1555 s = MapVirtualKeyExA(VK_SHIFT, MAPVK_VK_TO_VSC, kl);
1556 ok(s != 0, "MapVirtualKeyEx(VK_SHIFT) should return non-zero\n");
1557 sL = MapVirtualKeyExA(VK_LSHIFT, MAPVK_VK_TO_VSC, kl);
1558 ok(s == sL || broken(sL == 0), /* win9x */
1559 "%x != %x\n", s, sL);
1561 kL = MapVirtualKeyExA(0x2a, MAPVK_VSC_TO_VK, kl);
1562 ok(kL == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kL);
1563 kR = MapVirtualKeyExA(0x36, MAPVK_VSC_TO_VK, kl);
1564 ok(kR == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kR);
1566 kL = MapVirtualKeyExA(0x2a, MAPVK_VSC_TO_VK_EX, kl);
1567 ok(kL == VK_LSHIFT || broken(kL == 0), /* win9x */
1568 "Scan code -> vKey = %x (not VK_LSHIFT)\n", kL);
1569 kR = MapVirtualKeyExA(0x36, MAPVK_VSC_TO_VK_EX, kl);
1570 ok(kR == VK_RSHIFT || broken(kR == 0), /* win9x */
1571 "Scan code -> vKey = %x (not VK_RSHIFT)\n", kR);
1573 /* test that MAPVK_VSC_TO_VK prefers the non-numpad vkey if there's ambiguity */
1574 for (i = 0; i < sizeof(numpad_collisions)/sizeof(numpad_collisions[0]); i++)
1576 UINT numpad_scan = MapVirtualKeyExA(numpad_collisions[i][0], MAPVK_VK_TO_VSC, kl);
1577 UINT other_scan = MapVirtualKeyExA(numpad_collisions[i][1], MAPVK_VK_TO_VSC, kl);
1579 /* do they really collide for this layout? */
1580 if (numpad_scan && other_scan == numpad_scan)
1582 UINT vkey = MapVirtualKeyExA(numpad_scan, MAPVK_VSC_TO_VK, kl);
1583 ok(vkey != numpad_collisions[i][0],
1584 "Got numpad vKey %x for scan code %x when there was another choice\n",
1585 vkey, numpad_scan);
1590 #define shift 1
1591 #define ctrl 2
1593 static const struct tounicode_tests
1595 UINT vk;
1596 DWORD modifiers;
1597 WCHAR chr; /* if vk is 0, lookup vk using this char */
1598 int expect_ret;
1599 WCHAR expect_buf[4];
1600 } utests[] =
1602 { 'A', 0, 0, 1, {'a',0}},
1603 { 'A', ctrl, 0, 1, {1, 0}},
1604 { 'A', shift|ctrl, 0, 1, {1, 0}},
1605 { VK_TAB, ctrl, 0, 0, {}},
1606 { VK_TAB, shift|ctrl, 0, 0, {}},
1607 { VK_RETURN, ctrl, 0, 1, {'\n', 0}},
1608 { VK_RETURN, shift|ctrl, 0, 0, {}},
1609 { '4', ctrl, 0, 0, {}},
1610 { '4', shift|ctrl, 0, 0, {}},
1611 { 0, ctrl, '!', 0, {}},
1612 { 0, ctrl, '\"', 0, {}},
1613 { 0, ctrl, '#', 0, {}},
1614 { 0, ctrl, '$', 0, {}},
1615 { 0, ctrl, '%', 0, {}},
1616 { 0, ctrl, '\'', 0, {}},
1617 { 0, ctrl, '(', 0, {}},
1618 { 0, ctrl, ')', 0, {}},
1619 { 0, ctrl, '*', 0, {}},
1620 { 0, ctrl, '+', 0, {}},
1621 { 0, ctrl, ',', 0, {}},
1622 { 0, ctrl, '-', 0, {}},
1623 { 0, ctrl, '.', 0, {}},
1624 { 0, ctrl, '/', 0, {}},
1625 { 0, ctrl, ':', 0, {}},
1626 { 0, ctrl, ';', 0, {}},
1627 { 0, ctrl, '<', 0, {}},
1628 { 0, ctrl, '=', 0, {}},
1629 { 0, ctrl, '>', 0, {}},
1630 { 0, ctrl, '?', 0, {}},
1631 { 0, ctrl, '@', 1, {0}},
1632 { 0, ctrl, '[', 1, {0x1b}},
1633 { 0, ctrl, '\\', 1, {0x1c}},
1634 { 0, ctrl, ']', 1, {0x1d}},
1635 { 0, ctrl, '^', 1, {0x1e}},
1636 { 0, ctrl, '_', 1, {0x1f}},
1637 { 0, ctrl, '`', 0, {}},
1638 { VK_SPACE, 0, 0, 1, {' ',0}},
1639 { VK_SPACE, shift, 0, 1, {' ',0}},
1640 { VK_SPACE, ctrl, 0, 1, {' ',0}},
1643 static void test_ToUnicode(void)
1645 WCHAR wStr[4];
1646 BYTE state[256];
1647 const BYTE SC_RETURN = 0x1c, SC_TAB = 0x0f, SC_A = 0x1e;
1648 const BYTE HIGHEST_BIT = 0x80;
1649 int i, ret;
1650 BOOL us_kbd = (GetKeyboardLayout(0) == (HKL)(ULONG_PTR)0x04090409);
1652 for(i=0; i<256; i++)
1653 state[i]=0;
1655 wStr[1] = 0xAA;
1656 SetLastError(0xdeadbeef);
1657 ret = ToUnicode(VK_RETURN, SC_RETURN, state, wStr, 4, 0);
1658 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1660 win_skip("ToUnicode is not implemented\n");
1661 return;
1664 ok(ret == 1, "ToUnicode for Return key didn't return 1 (was %i)\n", ret);
1665 if(ret == 1)
1667 ok(wStr[0]=='\r', "ToUnicode for CTRL + Return was %i (expected 13)\n", wStr[0]);
1668 ok(wStr[1]==0 || broken(wStr[1]!=0) /* nt4 */,
1669 "ToUnicode didn't null-terminate the buffer when there was room.\n");
1672 for (i = 0; i < sizeof(utests) / sizeof(utests[0]); i++)
1674 UINT vk = utests[i].vk, mod = utests[i].modifiers, scan;
1676 if(!vk)
1678 short vk_ret;
1680 if (!us_kbd) continue;
1681 vk_ret = VkKeyScanW(utests[i].chr);
1682 if (vk_ret == -1) continue;
1683 vk = vk_ret & 0xff;
1684 if (vk_ret & 0x100) mod |= shift;
1685 if (vk_ret & 0x200) mod |= ctrl;
1687 scan = MapVirtualKeyW(vk, MAPVK_VK_TO_VSC);
1689 state[VK_SHIFT] = state[VK_LSHIFT] = (mod & shift) ? HIGHEST_BIT : 0;
1690 state[VK_CONTROL] = state[VK_LCONTROL] = (mod & ctrl) ? HIGHEST_BIT : 0;
1692 ret = ToUnicode(vk, scan, state, wStr, 4, 0);
1693 ok(ret == utests[i].expect_ret, "%d: got %d expected %d\n", i, ret, utests[i].expect_ret);
1694 if (ret)
1695 ok(!lstrcmpW(wStr, utests[i].expect_buf), "%d: got %s expected %s\n", i, wine_dbgstr_w(wStr),
1696 wine_dbgstr_w(utests[i].expect_buf));
1699 state[VK_SHIFT] = state[VK_LSHIFT] = 0;
1700 state[VK_CONTROL] = state[VK_LCONTROL] = 0;
1702 ret = ToUnicode(VK_TAB, SC_TAB, NULL, wStr, 4, 0);
1703 ok(ret == 0, "ToUnicode with NULL keystate didn't return 0 (was %i)\n", ret);
1704 ret = ToUnicode(VK_RETURN, SC_RETURN, NULL, wStr, 4, 0);
1705 ok(ret == 0, "ToUnicode with NULL keystate didn't return 0 (was %i)\n", ret);
1706 ret = ToUnicode('A', SC_A, NULL, wStr, 4, 0);
1707 ok(ret == 0, "ToUnicode with NULL keystate didn't return 0 (was %i)\n", ret);
1708 ret = ToUnicodeEx(VK_TAB, SC_TAB, NULL, wStr, 4, 0, GetKeyboardLayout(0));
1709 ok(ret == 0, "ToUnicodeEx with NULL keystate didn't return 0 (was %i)\n", ret);
1710 ret = ToUnicodeEx(VK_RETURN, SC_RETURN, NULL, wStr, 4, 0, GetKeyboardLayout(0));
1711 ok(ret == 0, "ToUnicodeEx with NULL keystate didn't return 0 (was %i)\n", ret);
1712 ret = ToUnicodeEx('A', SC_A, NULL, wStr, 4, 0, GetKeyboardLayout(0));
1713 ok(ret == 0, "ToUnicodeEx with NULL keystate didn't return 0 (was %i)\n", ret);
1716 static void test_ToAscii(void)
1718 WORD character;
1719 BYTE state[256];
1720 const BYTE SC_RETURN = 0x1c, SC_A = 0x1e;
1721 const BYTE HIGHEST_BIT = 0x80;
1722 int ret;
1724 memset(state, 0, sizeof(state));
1726 character = 0;
1727 ret = ToAscii(VK_RETURN, SC_RETURN, state, &character, 0);
1728 ok(ret == 1, "ToAscii for Return key didn't return 1 (was %i)\n", ret);
1729 ok(character == '\r', "ToAscii for Return was %i (expected 13)\n", character);
1731 character = 0;
1732 ret = ToAscii('A', SC_A, state, &character, 0);
1733 ok(ret == 1, "ToAscii for character 'A' didn't return 1 (was %i)\n", ret);
1734 ok(character == 'a', "ToAscii for character 'A' was %i (expected %i)\n", character, 'a');
1736 state[VK_CONTROL] |= HIGHEST_BIT;
1737 state[VK_LCONTROL] |= HIGHEST_BIT;
1738 character = 0;
1739 ret = ToAscii(VK_RETURN, SC_RETURN, state, &character, 0);
1740 ok(ret == 1, "ToAscii for CTRL + Return key didn't return 1 (was %i)\n", ret);
1741 ok(character == '\n', "ToAscii for CTRL + Return was %i (expected 10)\n", character);
1743 character = 0;
1744 ret = ToAscii('A', SC_A, state, &character, 0);
1745 ok(ret == 1, "ToAscii for CTRL + character 'A' didn't return 1 (was %i)\n", ret);
1746 ok(character == 1, "ToAscii for CTRL + character 'A' was %i (expected 1)\n", character);
1748 state[VK_SHIFT] |= HIGHEST_BIT;
1749 state[VK_LSHIFT] |= HIGHEST_BIT;
1750 ret = ToAscii(VK_RETURN, SC_RETURN, state, &character, 0);
1751 ok(ret == 0, "ToAscii for CTRL + Shift + Return key didn't return 0 (was %i)\n", ret);
1753 ret = ToAscii(VK_RETURN, SC_RETURN, NULL, &character, 0);
1754 ok(ret == 0, "ToAscii for NULL keystate didn't return 0 (was %i)\n", ret);
1755 ret = ToAscii('A', SC_A, NULL, &character, 0);
1756 ok(ret == 0, "ToAscii for NULL keystate didn't return 0 (was %i)\n", ret);
1757 ret = ToAsciiEx(VK_RETURN, SC_RETURN, NULL, &character, 0, GetKeyboardLayout(0));
1758 ok(ret == 0, "ToAsciiEx for NULL keystate didn't return 0 (was %i)\n", ret);
1759 ret = ToAsciiEx('A', SC_A, NULL, &character, 0, GetKeyboardLayout(0));
1760 ok(ret == 0, "ToAsciiEx for NULL keystate didn't return 0 (was %i)\n", ret);
1763 static void test_get_async_key_state(void)
1765 /* input value sanity checks */
1766 ok(0 == GetAsyncKeyState(1000000), "GetAsyncKeyState did not return 0\n");
1767 ok(0 == GetAsyncKeyState(-1000000), "GetAsyncKeyState did not return 0\n");
1770 static void test_keyboard_layout_name(void)
1772 BOOL ret;
1773 char klid[KL_NAMELENGTH];
1775 if (0) /* crashes on native system */
1776 ret = GetKeyboardLayoutNameA(NULL);
1778 SetLastError(0xdeadbeef);
1779 ret = GetKeyboardLayoutNameW(NULL);
1780 ok(!ret, "got %d\n", ret);
1781 ok(GetLastError() == ERROR_NOACCESS, "got %d\n", GetLastError());
1783 if (GetKeyboardLayout(0) != (HKL)(ULONG_PTR)0x04090409) return;
1785 klid[0] = 0;
1786 ret = GetKeyboardLayoutNameA(klid);
1787 ok(ret, "GetKeyboardLayoutNameA failed %u\n", GetLastError());
1788 ok(!strcmp(klid, "00000409"), "expected 00000409, got %s\n", klid);
1791 static void test_key_names(void)
1793 char buffer[40];
1794 WCHAR bufferW[40];
1795 int ret, prev;
1796 LONG lparam = 0x1d << 16;
1798 memset( buffer, 0xcc, sizeof(buffer) );
1799 ret = GetKeyNameTextA( lparam, buffer, sizeof(buffer) );
1800 ok( ret > 0, "wrong len %u for '%s'\n", ret, buffer );
1801 ok( ret == strlen(buffer), "wrong len %u for '%s'\n", ret, buffer );
1803 memset( buffer, 0xcc, sizeof(buffer) );
1804 prev = ret;
1805 ret = GetKeyNameTextA( lparam, buffer, prev );
1806 ok( ret == prev - 1, "wrong len %u for '%s'\n", ret, buffer );
1807 ok( ret == strlen(buffer), "wrong len %u for '%s'\n", ret, buffer );
1809 memset( buffer, 0xcc, sizeof(buffer) );
1810 ret = GetKeyNameTextA( lparam, buffer, 0 );
1811 ok( ret == 0, "wrong len %u for '%s'\n", ret, buffer );
1812 ok( buffer[0] == 0, "wrong string '%s'\n", buffer );
1814 memset( bufferW, 0xcc, sizeof(bufferW) );
1815 ret = GetKeyNameTextW( lparam, bufferW, sizeof(bufferW)/sizeof(WCHAR) );
1816 ok( ret > 0, "wrong len %u for %s\n", ret, wine_dbgstr_w(bufferW) );
1817 ok( ret == lstrlenW(bufferW), "wrong len %u for %s\n", ret, wine_dbgstr_w(bufferW) );
1819 memset( bufferW, 0xcc, sizeof(bufferW) );
1820 prev = ret;
1821 ret = GetKeyNameTextW( lparam, bufferW, prev );
1822 ok( ret == prev - 1, "wrong len %u for %s\n", ret, wine_dbgstr_w(bufferW) );
1823 ok( ret == lstrlenW(bufferW), "wrong len %u for %s\n", ret, wine_dbgstr_w(bufferW) );
1825 memset( bufferW, 0xcc, sizeof(bufferW) );
1826 ret = GetKeyNameTextW( lparam, bufferW, 0 );
1827 ok( ret == 0, "wrong len %u for %s\n", ret, wine_dbgstr_w(bufferW) );
1828 ok( bufferW[0] == 0xcccc, "wrong string %s\n", wine_dbgstr_w(bufferW) );
1831 static void simulate_click(BOOL left, int x, int y)
1833 INPUT input[2];
1834 UINT events_no;
1836 SetCursorPos(x, y);
1837 memset(input, 0, sizeof(input));
1838 input[0].type = INPUT_MOUSE;
1839 U(input[0]).mi.dx = x;
1840 U(input[0]).mi.dy = y;
1841 U(input[0]).mi.dwFlags = left ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN;
1842 input[1].type = INPUT_MOUSE;
1843 U(input[1]).mi.dx = x;
1844 U(input[1]).mi.dy = y;
1845 U(input[1]).mi.dwFlags = left ? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP;
1846 events_no = SendInput(2, input, sizeof(input[0]));
1847 ok(events_no == 2, "SendInput returned %d\n", events_no);
1850 static BOOL wait_for_message( MSG *msg )
1852 BOOL ret;
1854 for (;;)
1856 ret = PeekMessageA(msg, 0, 0, 0, PM_REMOVE);
1857 if (ret)
1859 if (msg->message == WM_PAINT) DispatchMessageA(msg);
1860 else break;
1862 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT) == WAIT_TIMEOUT) break;
1864 if (!ret) msg->message = 0;
1865 return ret;
1868 static BOOL wait_for_event(HANDLE event, int timeout)
1870 DWORD end_time = GetTickCount() + timeout;
1871 MSG msg;
1873 do {
1874 if(MsgWaitForMultipleObjects(1, &event, FALSE, timeout, QS_ALLINPUT) == WAIT_OBJECT_0)
1875 return TRUE;
1876 while(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
1877 DispatchMessageA(&msg);
1878 timeout = end_time - GetTickCount();
1879 }while(timeout > 0);
1881 return FALSE;
1884 static WNDPROC def_static_proc;
1885 static DWORD hittest_no;
1886 static LRESULT WINAPI static_hook_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1888 if (msg == WM_NCHITTEST)
1890 /* break infinite hittest loop */
1891 if(hittest_no > 50) return HTCLIENT;
1892 hittest_no++;
1895 return def_static_proc(hwnd, msg, wp, lp);
1898 struct thread_data
1900 HANDLE start_event;
1901 HANDLE end_event;
1902 HWND win;
1905 static DWORD WINAPI create_static_win(void *arg)
1907 struct thread_data *thread_data = arg;
1908 HWND win;
1910 win = CreateWindowA("static", "static", WS_VISIBLE | WS_POPUP,
1911 100, 100, 100, 100, 0, NULL, NULL, NULL);
1912 ok(win != 0, "CreateWindow failed\n");
1913 def_static_proc = (void*)SetWindowLongPtrA(win,
1914 GWLP_WNDPROC, (LONG_PTR)static_hook_proc);
1915 thread_data->win = win;
1917 SetEvent(thread_data->start_event);
1918 wait_for_event(thread_data->end_event, 5000);
1919 return 0;
1922 static void test_Input_mouse(void)
1924 BOOL got_button_down, got_button_up;
1925 HWND hwnd, button_win, static_win;
1926 struct thread_data thread_data;
1927 HANDLE thread;
1928 DWORD thread_id;
1929 POINT pt, pt_org;
1930 MSG msg;
1931 BOOL ret;
1933 SetLastError(0xdeadbeef);
1934 ret = GetCursorPos(NULL);
1935 ok(!ret, "GetCursorPos succeed\n");
1936 ok(GetLastError() == 0xdeadbeef || GetLastError() == ERROR_NOACCESS, "error %u\n", GetLastError());
1938 SetLastError(0xdeadbeef);
1939 ret = GetCursorPos(&pt_org);
1940 ok(ret, "GetCursorPos failed\n");
1941 ok(GetLastError() == 0xdeadbeef, "error %u\n", GetLastError());
1943 button_win = CreateWindowA("button", "button", WS_VISIBLE | WS_POPUP,
1944 100, 100, 100, 100, 0, NULL, NULL, NULL);
1945 ok(button_win != 0, "CreateWindow failed\n");
1947 pt.x = pt.y = 150;
1948 hwnd = WindowFromPoint(pt);
1949 if (hwnd != button_win)
1951 skip("there's another window covering test window\n");
1952 DestroyWindow(button_win);
1953 return;
1956 /* simple button click test */
1957 simulate_click(TRUE, 150, 150);
1958 got_button_down = got_button_up = FALSE;
1959 while (wait_for_message(&msg))
1961 DispatchMessageA(&msg);
1963 if (msg.message == WM_LBUTTONDOWN)
1965 got_button_down = TRUE;
1967 else if (msg.message == WM_LBUTTONUP)
1969 got_button_up = TRUE;
1970 break;
1973 ok(got_button_down, "expected WM_LBUTTONDOWN message\n");
1974 ok(got_button_up, "expected WM_LBUTTONUP message\n");
1976 /* click through HTTRANSPARENT child window */
1977 static_win = CreateWindowA("static", "static", WS_VISIBLE | WS_CHILD,
1978 0, 0, 100, 100, button_win, NULL, NULL, NULL);
1979 ok(static_win != 0, "CreateWindow failed\n");
1980 def_static_proc = (void*)SetWindowLongPtrA(static_win,
1981 GWLP_WNDPROC, (LONG_PTR)static_hook_proc);
1982 simulate_click(FALSE, 150, 150);
1983 hittest_no = 0;
1984 got_button_down = got_button_up = FALSE;
1985 while (wait_for_message(&msg))
1987 DispatchMessageA(&msg);
1989 if (msg.message == WM_RBUTTONDOWN)
1991 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
1992 got_button_down = TRUE;
1994 else if (msg.message == WM_RBUTTONUP)
1996 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
1997 got_button_up = TRUE;
1998 break;
2001 ok(hittest_no && hittest_no<50, "expected WM_NCHITTEST message\n");
2002 ok(got_button_down, "expected WM_RBUTTONDOWN message\n");
2003 ok(got_button_up, "expected WM_RBUTTONUP message\n");
2004 DestroyWindow(static_win);
2006 /* click through HTTRANSPARENT top-level window */
2007 static_win = CreateWindowA("static", "static", WS_VISIBLE | WS_POPUP,
2008 100, 100, 100, 100, 0, NULL, NULL, NULL);
2009 ok(static_win != 0, "CreateWindow failed\n");
2010 def_static_proc = (void*)SetWindowLongPtrA(static_win,
2011 GWLP_WNDPROC, (LONG_PTR)static_hook_proc);
2012 simulate_click(TRUE, 150, 150);
2013 hittest_no = 0;
2014 got_button_down = got_button_up = FALSE;
2015 while (wait_for_message(&msg))
2017 DispatchMessageA(&msg);
2019 if (msg.message == WM_LBUTTONDOWN)
2021 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
2022 got_button_down = TRUE;
2024 else if (msg.message == WM_LBUTTONUP)
2026 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
2027 got_button_up = TRUE;
2028 break;
2031 ok(hittest_no && hittest_no<50, "expected WM_NCHITTEST message\n");
2032 todo_wine ok(got_button_down, "expected WM_LBUTTONDOWN message\n");
2033 todo_wine ok(got_button_up, "expected WM_LBUTTONUP message\n");
2034 DestroyWindow(static_win);
2036 /* click on HTTRANSPARENT top-level window that belongs to other thread */
2037 thread_data.start_event = CreateEventA(NULL, FALSE, FALSE, NULL);
2038 ok(thread_data.start_event != NULL, "CreateEvent failed\n");
2039 thread_data.end_event = CreateEventA(NULL, FALSE, FALSE, NULL);
2040 ok(thread_data.end_event != NULL, "CreateEvent failed\n");
2041 thread = CreateThread(NULL, 0, create_static_win, &thread_data, 0, NULL);
2042 ok(thread != NULL, "CreateThread failed\n");
2043 hittest_no = 0;
2044 got_button_down = got_button_up = FALSE;
2045 WaitForSingleObject(thread_data.start_event, INFINITE);
2046 simulate_click(FALSE, 150, 150);
2047 while (wait_for_message(&msg))
2049 DispatchMessageA(&msg);
2051 if (msg.message == WM_RBUTTONDOWN)
2052 got_button_down = TRUE;
2053 else if (msg.message == WM_RBUTTONUP)
2054 got_button_up = TRUE;
2056 SetEvent(thread_data.end_event);
2057 WaitForSingleObject(thread, INFINITE);
2058 ok(hittest_no && hittest_no<50, "expected WM_NCHITTEST message\n");
2059 ok(!got_button_down, "unexpected WM_RBUTTONDOWN message\n");
2060 ok(!got_button_up, "unexpected WM_RBUTTONUP message\n");
2062 /* click on HTTRANSPARENT top-level window that belongs to other thread,
2063 * thread input queues are attached */
2064 thread = CreateThread(NULL, 0, create_static_win, &thread_data, 0, &thread_id);
2065 ok(thread != NULL, "CreateThread failed\n");
2066 hittest_no = 0;
2067 got_button_down = got_button_up = FALSE;
2068 WaitForSingleObject(thread_data.start_event, INFINITE);
2069 ok(AttachThreadInput(thread_id, GetCurrentThreadId(), TRUE),
2070 "AttachThreadInput failed\n");
2071 while (wait_for_message(&msg)) DispatchMessageA(&msg);
2072 SetWindowPos(thread_data.win, button_win, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
2073 simulate_click(TRUE, 150, 150);
2074 while (wait_for_message(&msg))
2076 DispatchMessageA(&msg);
2078 if (msg.message == WM_LBUTTONDOWN)
2079 got_button_down = TRUE;
2080 else if (msg.message == WM_LBUTTONUP)
2081 got_button_up = TRUE;
2083 SetEvent(thread_data.end_event);
2084 WaitForSingleObject(thread, INFINITE);
2085 todo_wine ok(hittest_no > 50, "expected loop with WM_NCHITTEST messages\n");
2086 ok(!got_button_down, "unexpected WM_LBUTTONDOWN message\n");
2087 ok(!got_button_up, "unexpected WM_LBUTTONUP message\n");
2089 /* click after SetCapture call */
2090 hwnd = CreateWindowA("button", "button", WS_VISIBLE | WS_POPUP,
2091 0, 0, 100, 100, 0, NULL, NULL, NULL);
2092 ok(hwnd != 0, "CreateWindow failed\n");
2093 SetCapture(button_win);
2094 got_button_down = got_button_up = FALSE;
2095 simulate_click(FALSE, 50, 50);
2096 while (wait_for_message(&msg))
2098 DispatchMessageA(&msg);
2100 if (msg.message == WM_RBUTTONDOWN)
2102 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
2103 got_button_down = TRUE;
2105 else if (msg.message == WM_RBUTTONUP)
2107 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
2108 got_button_up = TRUE;
2109 break;
2112 ok(got_button_down, "expected WM_RBUTTONDOWN message\n");
2113 ok(got_button_up, "expected WM_RBUTTONUP message\n");
2114 DestroyWindow(hwnd);
2116 /* click on child window after SetCapture call */
2117 hwnd = CreateWindowA("button", "button2", WS_VISIBLE | WS_CHILD,
2118 0, 0, 100, 100, button_win, NULL, NULL, NULL);
2119 ok(hwnd != 0, "CreateWindow failed\n");
2120 got_button_down = got_button_up = FALSE;
2121 simulate_click(TRUE, 150, 150);
2122 while (wait_for_message(&msg))
2124 DispatchMessageA(&msg);
2126 if (msg.message == WM_LBUTTONDOWN)
2128 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
2129 got_button_down = TRUE;
2131 else if (msg.message == WM_LBUTTONUP)
2133 ok(msg.hwnd == button_win, "msg.hwnd = %p\n", msg.hwnd);
2134 got_button_up = TRUE;
2135 break;
2138 ok(got_button_down, "expected WM_LBUTTONDOWN message\n");
2139 ok(got_button_up, "expected WM_LBUTTONUP message\n");
2140 DestroyWindow(hwnd);
2141 ok(ReleaseCapture(), "ReleaseCapture failed\n");
2142 SetCursorPos(pt_org.x, pt_org.y);
2144 CloseHandle(thread_data.start_event);
2145 CloseHandle(thread_data.end_event);
2146 DestroyWindow(button_win);
2150 static LRESULT WINAPI MsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
2152 if (message == WM_USER+1)
2154 HWND hwnd = (HWND)lParam;
2155 ok(GetFocus() == hwnd, "thread expected focus %p, got %p\n", hwnd, GetFocus());
2156 ok(GetActiveWindow() == hwnd, "thread expected active %p, got %p\n", hwnd, GetActiveWindow());
2158 return DefWindowProcA(hwnd, message, wParam, lParam);
2161 struct wnd_event
2163 HWND hwnd;
2164 HANDLE wait_event;
2165 HANDLE start_event;
2166 DWORD attach_from;
2167 DWORD attach_to;
2168 BOOL setWindows;
2171 static DWORD WINAPI thread_proc(void *param)
2173 MSG msg;
2174 struct wnd_event *wnd_event = param;
2175 BOOL ret;
2177 if (wnd_event->wait_event)
2179 ok(WaitForSingleObject(wnd_event->wait_event, INFINITE) == WAIT_OBJECT_0,
2180 "WaitForSingleObject failed\n");
2181 CloseHandle(wnd_event->wait_event);
2184 if (wnd_event->attach_from)
2186 ret = AttachThreadInput(wnd_event->attach_from, GetCurrentThreadId(), TRUE);
2187 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2190 if (wnd_event->attach_to)
2192 ret = AttachThreadInput(GetCurrentThreadId(), wnd_event->attach_to, TRUE);
2193 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2196 wnd_event->hwnd = CreateWindowExA(0, "TestWindowClass", "window caption text", WS_OVERLAPPEDWINDOW,
2197 100, 100, 200, 200, 0, 0, 0, NULL);
2198 ok(wnd_event->hwnd != 0, "Failed to create overlapped window\n");
2200 if (wnd_event->setWindows)
2202 SetFocus(wnd_event->hwnd);
2203 SetActiveWindow(wnd_event->hwnd);
2206 SetEvent(wnd_event->start_event);
2208 while (GetMessageA(&msg, 0, 0, 0))
2210 TranslateMessage(&msg);
2211 DispatchMessageA(&msg);
2214 return 0;
2217 static void test_attach_input(void)
2219 HANDLE hThread;
2220 HWND ourWnd, Wnd2;
2221 DWORD ret, tid;
2222 struct wnd_event wnd_event;
2223 WNDCLASSA cls;
2225 cls.style = 0;
2226 cls.lpfnWndProc = MsgCheckProcA;
2227 cls.cbClsExtra = 0;
2228 cls.cbWndExtra = 0;
2229 cls.hInstance = GetModuleHandleA(0);
2230 cls.hIcon = 0;
2231 cls.hCursor = LoadCursorW( NULL, (LPCWSTR)IDC_ARROW);
2232 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2233 cls.lpszMenuName = NULL;
2234 cls.lpszClassName = "TestWindowClass";
2235 if(!RegisterClassA(&cls)) return;
2237 wnd_event.wait_event = NULL;
2238 wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
2239 wnd_event.attach_from = 0;
2240 wnd_event.attach_to = 0;
2241 wnd_event.setWindows = FALSE;
2242 if (!wnd_event.start_event)
2244 win_skip("skipping interthread message test under win9x\n");
2245 return;
2248 hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
2249 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
2251 ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2252 CloseHandle(wnd_event.start_event);
2254 ourWnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
2255 0, 0, 0, 0, 0, 0, 0, NULL);
2256 ok(ourWnd!= 0, "failed to create ourWnd window\n");
2258 Wnd2 = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
2259 0, 0, 0, 0, 0, 0, 0, NULL);
2260 ok(Wnd2!= 0, "failed to create Wnd2 window\n");
2262 SetFocus(ourWnd);
2263 SetActiveWindow(ourWnd);
2265 ret = AttachThreadInput(GetCurrentThreadId(), tid, TRUE);
2266 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2268 ok(GetActiveWindow() == ourWnd, "expected active %p, got %p\n", ourWnd, GetActiveWindow());
2269 ok(GetFocus() == ourWnd, "expected focus %p, got %p\n", ourWnd, GetFocus());
2271 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)ourWnd);
2273 ret = AttachThreadInput(GetCurrentThreadId(), tid, FALSE);
2274 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2275 ok(GetActiveWindow() == ourWnd, "expected active %p, got %p\n", ourWnd, GetActiveWindow());
2276 ok(GetFocus() == ourWnd, "expected focus %p, got %p\n", ourWnd, GetFocus());
2278 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, 0);
2280 ret = AttachThreadInput(GetCurrentThreadId(), tid, TRUE);
2281 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2283 ok(GetActiveWindow() == ourWnd, "expected active %p, got %p\n", ourWnd, GetActiveWindow());
2284 ok(GetFocus() == ourWnd, "expected focus %p, got %p\n", ourWnd, GetFocus());
2285 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)ourWnd);
2287 SetActiveWindow(Wnd2);
2288 SetFocus(Wnd2);
2289 ok(GetActiveWindow() == Wnd2, "expected active %p, got %p\n", Wnd2, GetActiveWindow());
2290 ok(GetFocus() == Wnd2, "expected focus %p, got %p\n", Wnd2, GetFocus());
2292 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)Wnd2);
2294 ret = AttachThreadInput(GetCurrentThreadId(), tid, FALSE);
2295 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2296 ok(GetActiveWindow() == Wnd2, "expected active %p, got %p\n", Wnd2, GetActiveWindow());
2297 ok(GetFocus() == Wnd2, "expected focus %p, got %p\n", Wnd2, GetFocus());
2299 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, 0);
2301 ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
2302 ok(ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
2304 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2305 CloseHandle(hThread);
2307 wnd_event.wait_event = NULL;
2308 wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
2309 wnd_event.attach_from = 0;
2310 wnd_event.attach_to = 0;
2311 wnd_event.setWindows = TRUE;
2313 hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
2314 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
2316 ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2317 CloseHandle(wnd_event.start_event);
2319 SetFocus(ourWnd);
2320 SetActiveWindow(ourWnd);
2322 ret = AttachThreadInput(GetCurrentThreadId(), tid, TRUE);
2323 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2325 ok(GetActiveWindow() == wnd_event.hwnd, "expected active %p, got %p\n", wnd_event.hwnd, GetActiveWindow());
2326 ok(GetFocus() == wnd_event.hwnd, "expected focus %p, got %p\n", wnd_event.hwnd, GetFocus());
2328 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)wnd_event.hwnd);
2330 ret = AttachThreadInput(GetCurrentThreadId(), tid, FALSE);
2331 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2333 ok(GetActiveWindow() == 0, "expected active 0, got %p\n", GetActiveWindow());
2334 ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
2336 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)wnd_event.hwnd);
2338 ret = AttachThreadInput(GetCurrentThreadId(), tid, TRUE);
2339 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2341 ok(GetActiveWindow() == wnd_event.hwnd, "expected active %p, got %p\n", wnd_event.hwnd, GetActiveWindow());
2342 ok(GetFocus() == wnd_event.hwnd, "expected focus %p, got %p\n", wnd_event.hwnd, GetFocus());
2344 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)wnd_event.hwnd);
2346 SetFocus(Wnd2);
2347 SetActiveWindow(Wnd2);
2348 ok(GetActiveWindow() == Wnd2, "expected active %p, got %p\n", Wnd2, GetActiveWindow());
2349 ok(GetFocus() == Wnd2, "expected focus %p, got %p\n", Wnd2, GetFocus());
2351 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, (LPARAM)Wnd2);
2353 ret = AttachThreadInput(GetCurrentThreadId(), tid, FALSE);
2354 ok(ret, "AttachThreadInput error %d\n", GetLastError());
2356 ok(GetActiveWindow() == Wnd2, "expected active %p, got %p\n", Wnd2, GetActiveWindow());
2357 ok(GetFocus() == Wnd2, "expected focus %p, got %p\n", Wnd2, GetFocus());
2359 SendMessageA(wnd_event.hwnd, WM_USER+1, 0, 0);
2361 ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
2362 ok(ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
2364 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2365 CloseHandle(hThread);
2367 wnd_event.wait_event = CreateEventW(NULL, 0, 0, NULL);
2368 wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
2369 wnd_event.attach_from = 0;
2370 wnd_event.attach_to = 0;
2371 wnd_event.setWindows = TRUE;
2373 hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
2374 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
2376 SetLastError(0xdeadbeef);
2377 ret = AttachThreadInput(GetCurrentThreadId(), tid, TRUE);
2378 ok(!ret, "AttachThreadInput succeeded\n");
2379 ok(GetLastError() == ERROR_INVALID_PARAMETER || broken(GetLastError() == 0xdeadbeef) /* <= Win XP */,
2380 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2382 SetLastError(0xdeadbeef);
2383 ret = AttachThreadInput(tid, GetCurrentThreadId(), TRUE);
2384 ok(!ret, "AttachThreadInput succeeded\n");
2385 ok(GetLastError() == ERROR_INVALID_PARAMETER || broken(GetLastError() == 0xdeadbeef) /* <= Win XP */,
2386 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2388 SetEvent(wnd_event.wait_event);
2390 ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2391 CloseHandle(wnd_event.start_event);
2393 ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
2394 ok(ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
2396 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2397 CloseHandle(hThread);
2399 wnd_event.wait_event = NULL;
2400 wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
2401 wnd_event.attach_from = GetCurrentThreadId();
2402 wnd_event.attach_to = 0;
2403 wnd_event.setWindows = FALSE;
2405 SetFocus(ourWnd);
2406 SetActiveWindow(ourWnd);
2408 hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
2409 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
2411 ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2412 CloseHandle(wnd_event.start_event);
2414 ok(GetActiveWindow() == ourWnd, "expected active %p, got %p\n", ourWnd, GetActiveWindow());
2415 ok(GetFocus() == ourWnd, "expected focus %p, got %p\n", ourWnd, GetFocus());
2417 ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
2418 ok(ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
2420 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2421 CloseHandle(hThread);
2423 wnd_event.wait_event = NULL;
2424 wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
2425 wnd_event.attach_from = 0;
2426 wnd_event.attach_to = GetCurrentThreadId();
2427 wnd_event.setWindows = FALSE;
2429 SetFocus(ourWnd);
2430 SetActiveWindow(ourWnd);
2432 hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
2433 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
2435 ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2436 CloseHandle(wnd_event.start_event);
2438 ok(GetActiveWindow() == ourWnd, "expected active %p, got %p\n", ourWnd, GetActiveWindow());
2439 ok(GetFocus() == ourWnd, "expected focus %p, got %p\n", ourWnd, GetFocus());
2441 ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
2442 ok(ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
2444 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
2445 CloseHandle(hThread);
2446 DestroyWindow(ourWnd);
2447 DestroyWindow(Wnd2);
2450 static DWORD WINAPI get_key_state_thread(void *arg)
2452 HANDLE *semaphores = arg;
2453 DWORD result;
2455 ReleaseSemaphore(semaphores[0], 1, NULL);
2456 result = WaitForSingleObject(semaphores[1], 1000);
2457 ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result);
2459 result = GetKeyState('X');
2460 ok((result & 0x8000) || broken(!(result & 0x8000)), /* > Win 2003 */
2461 "expected that highest bit is set, got %x\n", result);
2463 ReleaseSemaphore(semaphores[0], 1, NULL);
2464 result = WaitForSingleObject(semaphores[1], 1000);
2465 ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result);
2467 result = GetKeyState('X');
2468 ok(!(result & 0x8000), "expected that highest bit is unset, got %x\n", result);
2470 return 0;
2473 static void test_GetKeyState(void)
2475 HANDLE semaphores[2];
2476 HANDLE thread;
2477 DWORD result;
2478 HWND hwnd;
2480 semaphores[0] = CreateSemaphoreA(NULL, 0, 1, NULL);
2481 ok(semaphores[0] != NULL, "CreateSemaphoreA failed %u\n", GetLastError());
2482 semaphores[1] = CreateSemaphoreA(NULL, 0, 1, NULL);
2483 ok(semaphores[1] != NULL, "CreateSemaphoreA failed %u\n", GetLastError());
2485 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
2486 10, 10, 200, 200, NULL, NULL, NULL, NULL);
2487 ok(hwnd != NULL, "CreateWindowA failed %u\n", GetLastError());
2489 thread = CreateThread(NULL, 0, get_key_state_thread, semaphores, 0, NULL);
2490 ok(thread != NULL, "CreateThread failed %u\n", GetLastError());
2491 result = WaitForSingleObject(semaphores[0], 1000);
2492 ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result);
2494 SetFocus(hwnd);
2495 keybd_event('X', 0, 0, 0);
2497 ReleaseSemaphore(semaphores[1], 1, NULL);
2498 result = WaitForSingleObject(semaphores[0], 1000);
2499 ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result);
2501 keybd_event('X', 0, KEYEVENTF_KEYUP, 0);
2503 ReleaseSemaphore(semaphores[1], 1, NULL);
2504 result = WaitForSingleObject(thread, 1000);
2505 ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result);
2506 CloseHandle(thread);
2508 DestroyWindow(hwnd);
2509 CloseHandle(semaphores[0]);
2510 CloseHandle(semaphores[1]);
2513 static void test_OemKeyScan(void)
2515 DWORD ret, expect, vkey, scan;
2516 WCHAR oem, wchr;
2517 char oem_char;
2519 for (oem = 0; oem < 0x200; oem++)
2521 ret = OemKeyScan( oem );
2523 oem_char = LOBYTE( oem );
2524 /* OemKeyScan returns -1 for any character that cannot be mapped,
2525 * whereas OemToCharBuff changes unmappable characters to question
2526 * marks. The ASCII characters 0-127, including the real question mark
2527 * character, are all mappable and are the same in all OEM codepages. */
2528 if (!OemToCharBuffW( &oem_char, &wchr, 1 ) || (wchr == '?' && oem_char < 0))
2529 expect = -1;
2530 else
2532 vkey = VkKeyScanW( wchr );
2533 scan = MapVirtualKeyW( LOBYTE( vkey ), MAPVK_VK_TO_VSC );
2534 if (!scan)
2535 expect = -1;
2536 else
2538 vkey &= 0xff00;
2539 vkey <<= 8;
2540 expect = vkey | scan;
2543 ok( ret == expect, "%04x: got %08x expected %08x\n", oem, ret, expect );
2547 START_TEST(input)
2549 init_function_pointers();
2551 if (pSendInput)
2553 test_Input_blackbox();
2554 test_Input_whitebox();
2555 test_Input_unicode();
2556 test_Input_mouse();
2558 else win_skip("SendInput is not available\n");
2560 test_keynames();
2561 test_mouse_ll_hook();
2562 test_key_map();
2563 test_ToUnicode();
2564 test_ToAscii();
2565 test_get_async_key_state();
2566 test_keyboard_layout_name();
2567 test_key_names();
2568 test_attach_input();
2569 test_GetKeyState();
2570 test_OemKeyScan();
2572 if(pGetMouseMovePointsEx)
2573 test_GetMouseMovePointsEx();
2574 else
2575 win_skip("GetMouseMovePointsEx is not available\n");
2577 if(pGetRawInputDeviceList)
2578 test_GetRawInputDeviceList();
2579 else
2580 win_skip("GetRawInputDeviceList is not available\n");