Skip to content

Commit

Permalink
Map virtual key before triggering keybd_event
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Jan 18, 2016
1 parent 2ad647b commit c47f8b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/keypress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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)
Expand All @@ -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. */
Expand Down
5 changes: 5 additions & 0 deletions src/keypress.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ 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);

Expand Down

0 comments on commit c47f8b0

Please sign in to comment.