diff --git a/src/keypress.c b/src/keypress.c index 6187953b..dffd7bb4 100644 --- a/src/keypress.c +++ b/src/keypress.c @@ -13,9 +13,8 @@ /* Convenience wrappers around ugly APIs. */ #if defined(IS_WINDOWS) - #define WIN32_KEY_EVENT(key, flags) keybd_event(key, 0, flags, 0) #define WIN32_KEY_EVENT_WAIT(key, flags) \ - (WIN32_KEY_EVENT(key, flags), Sleep(DEADBEEF_RANDRANGE(0, 63))) + (win32KeyEvent(key, flags), Sleep(DEADBEEF_RANDRANGE(0, 63))) #elif defined(USE_X11) #define X_KEY_EVENT(display, key, is_press) \ (XTestFakeKeyEvent(display, \ @@ -27,6 +26,20 @@ microsleep(DEADBEEF_UNIFORM(0.0, 62.5))) #endif +#if defined(IS_WINDOWS) +void win32KeyEvent(int key, MMKeyFlags flags) +{ + int scan = MapVirtualKey(key & 0xff, 0); + + /* Set the scancode for keyup */ + if ( flags & KEYEVENTF_KEYUP ) { + scan = scan | 0x80; + } + + keybd_event(key, scan, flags, 0); +} +#endif + void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) { #if defined(IS_MACOSX) @@ -47,7 +60,7 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) if (flags & MOD_CONTROL) WIN32_KEY_EVENT_WAIT(K_CONTROL, dwFlags); if (flags & MOD_SHIFT) WIN32_KEY_EVENT_WAIT(K_SHIFT, dwFlags); - WIN32_KEY_EVENT(code, dwFlags); + win32KeyEvent(code, dwFlags); #elif defined(USE_X11) Display *display = XGetMainDisplay(); const Bool is_press = down ? True : False; /* Just to be safe. */ diff --git a/src/keypress.h b/src/keypress.h index 5ad43986..b5c823c0 100644 --- a/src/keypress.h +++ b/src/keypress.h @@ -51,6 +51,13 @@ extern "C" #endif +#if defined(IS_WINDOWS) + +/* Send win32 key event for given key. */ +void win32KeyEvent(int key, MMKeyFlags flags); + +#endif + /* Toggles the given key down or up. */ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags);