[NETAPI32]
[reactos.git] / rostests / apitests / user32 / GetKeyState.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for GetKeyState
5 * PROGRAMMERS: Giannis Adamopoulos
6 */
7
8 #include <apitest.h>
9
10 #include <winuser.h>
11 #include <assert.h>
12
13 HHOOK hKbdHook, hKbdLLHook;
14
15 LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
16 {
17 BOOL pressed = !(lParam & (1<<31));
18 BOOL altPressed = lParam & (1<<29);
19
20 if(pressed)
21 {
22 ok(altPressed,"\n");
23 ok((GetKeyState(VK_MENU) & 0x8000), "Alt should not be pressed\n");\
24 ok((GetKeyState(VK_LMENU) & 0x8000), "Left alt should not be pressed\n");\
25 }
26 else
27 {
28 ok(!altPressed,"\n");
29 ok(!(GetKeyState(VK_MENU) & 0x8000), "Alt should be pressed\n");
30 ok(!(GetKeyState(VK_LMENU) & 0x8000), "Left alt should be pressed\n");
31 }
32
33 return CallNextHookEx(hKbdHook, code, wParam, lParam);
34 }
35
36 LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
37 {
38 PKBDLLHOOKSTRUCT pLLHook = (PKBDLLHOOKSTRUCT)lParam;
39
40 if(wParam == WM_SYSKEYDOWN)
41 {
42 ok(pLLHook->flags & LLKHF_ALTDOWN,"Didn't get LLKHF_ALTDOWN flag\n");
43 ok((GetAsyncKeyState (VK_MENU) & 0x8000), "Alt should not be pressed in global kbd status\n");
44 ok(!(GetKeyState(VK_MENU) & 0x8000), "Alt should not be pressed in queue state\n");
45 ok(!(GetAsyncKeyState (VK_LMENU) & 0x8000), "Left alt should not be pressed in global kbd status\n");
46 ok(!(GetKeyState(VK_LMENU) & 0x8000), "Left alt should not be pressed in queue state\n");
47 }
48 else if(wParam == WM_SYSKEYUP)
49 {
50 ok(!(pLLHook->flags & LLKHF_ALTDOWN),"got LLKHF_ALTDOWN flag\n");
51 ok(!(GetAsyncKeyState (VK_MENU) & 0x8000), "Alt should not be pressed in global kbd status\n");
52 ok((GetKeyState(VK_MENU) & 0x8000), "Alt should be pressed in queue state\n");
53 ok(!(GetAsyncKeyState (VK_LMENU) & 0x8000), "Left alt should not be pressed in global kbd status\n");
54 ok((GetKeyState(VK_LMENU) & 0x8000), "Left alt should be pressed in queue state\n");
55 }
56
57 return CallNextHookEx(hKbdLLHook, nCode, wParam, lParam);
58 }
59
60 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
61 {
62 if(msg == WM_SYSKEYDOWN)
63 {
64 ok(wParam == VK_MENU, "Got wrong wParam in WM_SYSKEYDOWN (%d instead of %d)\n", wParam, VK_MENU );
65 }
66 return DefWindowProcA( hWnd, msg, wParam, lParam );
67 }
68
69 static HWND CreateTestWindow()
70 {
71 MSG msg;
72 WNDCLASSA wclass;
73 HANDLE hInstance = GetModuleHandleA( NULL );
74 HWND hWndTest;
75
76 wclass.lpszClassName = "InputSysKeyTestClass";
77 wclass.style = CS_HREDRAW | CS_VREDRAW;
78 wclass.lpfnWndProc = WndProc;
79 wclass.hInstance = hInstance;
80 wclass.hIcon = LoadIconA( 0, IDI_APPLICATION );
81 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW );
82 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
83 wclass.lpszMenuName = 0;
84 wclass.cbClsExtra = 0;
85 wclass.cbWndExtra = 0;
86 RegisterClassA( &wclass );
87 /* create the test window that will receive the keystrokes */
88 hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
89 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
90 NULL, NULL, hInstance, NULL);
91 assert( hWndTest );
92 ShowWindow( hWndTest, SW_SHOW);
93 SetWindowPos( hWndTest, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
94 SetForegroundWindow( hWndTest );
95 UpdateWindow( hWndTest);
96
97 /* flush pending messages */
98 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
99
100 return hWndTest;
101 }
102
103 void Test_GetKeyState()
104 {
105 HWND hwnd;
106 MSG msg;
107
108 hwnd = CreateTestWindow();
109
110 hKbdHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, GetModuleHandleA( NULL ), 0);
111 hKbdLLHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandleA( NULL ), 0);
112
113 ok(hKbdHook!=NULL," \n");
114 ok(hKbdLLHook!=NULL," \n");
115
116 keybd_event(VK_LMENU, 0, 0,0);
117
118 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
119
120 keybd_event(VK_LMENU, 0, KEYEVENTF_KEYUP,0);
121
122 //fixme this hangs the test
123 //while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE|PM_NOYIELD )) DispatchMessageA( &msg );
124
125 DestroyWindow(hwnd);
126
127 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
128
129 UnhookWindowsHookEx (hKbdHook);
130 UnhookWindowsHookEx (hKbdLLHook);
131 }
132
133 START_TEST(GetKeyState)
134 {
135 Test_GetKeyState();
136 }