2 * This work is licensed under the terms of the GNU GPL, version 2 or
3 * (at your option) any later version. See the COPYING file in the
6 * The win32 keyboard hooking code was imported from project spice-gtk.
9 #include "qemu/osdep.h"
10 #include "sysemu/sysemu.h"
11 #include "ui/win32-kbd-hook.h"
13 static Notifier win32_unhook_notifier;
14 static HHOOK win32_keyboard_hook;
15 static HWND win32_window;
16 static DWORD win32_grab;
18 static LRESULT CALLBACK keyboard_hook_cb(int code, WPARAM wparam, LPARAM lparam)
20 if (win32_window && code == HC_ACTION && win32_window == GetFocus()) {
21 KBDLLHOOKSTRUCT *hooked = (KBDLLHOOKSTRUCT *)lparam;
23 if (wparam != WM_KEYUP) {
24 DWORD dwmsg = (hooked->flags << 24) |
25 ((hooked->scanCode & 0xff) << 16) | 1;
27 switch (hooked->vkCode) {
47 * When pressing AltGr, an extra VK_LCONTROL with a special
48 * scancode with bit 9 set is sent. Let's ignore the extra
49 * VK_LCONTROL, as that will make AltGr misbehave.
51 if (hooked->scanCode & 0x200) {
58 SendMessage(win32_window, wparam, hooked->vkCode, dwmsg);
65 switch (hooked->vkCode) {
67 if (hooked->scanCode & 0x200) {
75 return CallNextHookEx(NULL, code, wparam, lparam);
78 static void keyboard_hook_unhook(Notifier *n, void *data)
80 UnhookWindowsHookEx(win32_keyboard_hook);
81 win32_keyboard_hook = NULL;
84 void win32_kbd_set_window(void *hwnd)
86 if (hwnd && !win32_keyboard_hook) {
87 /* note: the installing thread must have a message loop */
88 win32_keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_hook_cb,
89 GetModuleHandle(NULL), 0);
90 if (win32_keyboard_hook) {
91 win32_unhook_notifier.notify = keyboard_hook_unhook;
92 qemu_add_exit_notifier(&win32_unhook_notifier);
99 void win32_kbd_set_grab(bool grab)