Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map virtual key before triggering keybd_event #151

Merged
merged 1 commit into from
Feb 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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