From eca9c452d82731fe12b321132c735a50175bbb6b Mon Sep 17 00:00:00 2001 From: Tony Jansson Date: Mon, 9 Nov 2020 15:19:41 +0200 Subject: [PATCH] (DINPUT) Simultaneous shift sticky fix --- gfx/common/win32_common.c | 7 ++++++ input/drivers/dinput.c | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/gfx/common/win32_common.c b/gfx/common/win32_common.c index 3209e5be6b..c50dbe7809 100644 --- a/gfx/common/win32_common.c +++ b/gfx/common/win32_common.c @@ -1054,6 +1054,13 @@ static LRESULT CALLBACK wnd_proc_common_dinput_internal(HWND hwnd, keysym |= 0x80; keycode = input_keymaps_translate_keysym_to_rk(keysym); + switch (keycode) + { + /* L+R Shift handling done in dinput_poll */ + case RETROK_LSHIFT: + case RETROK_RSHIFT: + return 0; + } input_keyboard_event(keydown, keycode, 0, mod, RETRO_DEVICE_KEYBOARD); diff --git a/input/drivers/dinput.c b/input/drivers/dinput.c index 3a54379291..25ea58d6a7 100644 --- a/input/drivers/dinput.c +++ b/input/drivers/dinput.c @@ -81,6 +81,7 @@ struct dinput_input int mouse_x; int mouse_y; uint8_t state[256]; + bool shift_l, shift_r, alt_l; bool doubleclick_on_titlebar; bool mouse_l, mouse_r, mouse_m, mouse_b4, mouse_b5, mouse_wu, mouse_wd, mouse_hwu, mouse_hwd; }; @@ -181,6 +182,50 @@ static void *dinput_init(const char *joypad_driver) return di; } +static void dinput_keyboard_mods(struct dinput_input *di, int mod) +{ + switch (mod) + { + case RETROKMOD_SHIFT: + { + unsigned vk_shift_l = GetAsyncKeyState(VK_LSHIFT) >> 1; + unsigned vk_shift_r = GetAsyncKeyState(VK_RSHIFT) >> 1; + + if ( (vk_shift_l && !di->shift_l) || + (!vk_shift_l && di->shift_l)) + { + input_keyboard_event(vk_shift_l, RETROK_LSHIFT, + 0, RETROKMOD_SHIFT, RETRO_DEVICE_KEYBOARD); + di->shift_l = !di->shift_l; + } + + if ( (vk_shift_r && !di->shift_r) || + (!vk_shift_r && di->shift_r)) + { + input_keyboard_event(vk_shift_r, RETROK_RSHIFT, + 0, RETROKMOD_SHIFT, RETRO_DEVICE_KEYBOARD); + di->shift_r = !di->shift_r; + } + } + break; + + case RETROKMOD_ALT: + { + unsigned vk_alt_l = GetAsyncKeyState(VK_LMENU) >> 1; + + if (vk_alt_l && !di->alt_l) + di->alt_l = !di->alt_l; + else if (!vk_alt_l && di->alt_l) + { + input_keyboard_event(vk_alt_l, RETROK_LALT, + 0, RETROKMOD_ALT, RETRO_DEVICE_KEYBOARD); + di->alt_l = !di->alt_l; + } + } + break; + } +} + static void dinput_poll(void *data) { struct dinput_input *di = (struct dinput_input*)data; @@ -211,6 +256,12 @@ static void dinput_poll(void *data) *kb_state = 0; } } + else + /* Shifts only when window focused */ + dinput_keyboard_mods(di, RETROKMOD_SHIFT); + + /* Left alt keyup when unfocused, to prevent alt-tab sticky */ + dinput_keyboard_mods(di, RETROKMOD_ALT); } if (di->mouse)