From 5e6fd870feaa769c19ca2ce1d45dd699fa3f6f9f Mon Sep 17 00:00:00 2001 From: mjbudd77 Date: Sun, 27 Feb 2022 12:38:57 -0500 Subject: [PATCH] Added code to differentiate left vs right modifier keys on windows and linux. Cannot do this yet on mac OSX. --- src/CMakeLists.txt | 16 +++++ src/drivers/Qt/ConsoleWindow.cpp | 2 + src/drivers/Qt/input.cpp | 31 ++++++++-- src/drivers/Qt/keyscan.cpp | 100 +++++++++++++++++++++++++++---- src/drivers/Qt/keyscan.h | 4 +- 5 files changed, 134 insertions(+), 19 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4144feb8..1ec3242c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -114,6 +114,22 @@ else(WIN32) add_definitions( -D_USE_LIBAV ${LIBAV_CFLAGS} ) endif() + #find_package(X11) + pkg_check_modules( X11 x11) + + if ( ${X11_FOUND} ) + message( STATUS "Has X11 Library ${X11_VERSION}" ) + add_definitions( -D_HAS_X11 ) + include_directories( ${X11_INCLUDE_DIR} ) + endif() + + pkg_check_modules( XKB xkbcommon) + + if ( ${XKB_FOUND} ) + message( STATUS "Has XKB Library ${XKB_VERSION}" ) + add_definitions( -D_HAS_XKB ${XKB_CFLAGS} ) + endif() + #pkg_check_modules( GL gl) # Use built in find package instead for OpenGL # Check for OpenGL diff --git a/src/drivers/Qt/ConsoleWindow.cpp b/src/drivers/Qt/ConsoleWindow.cpp index 4461c4cd..eb488c4f 100644 --- a/src/drivers/Qt/ConsoleWindow.cpp +++ b/src/drivers/Qt/ConsoleWindow.cpp @@ -110,6 +110,8 @@ consoleWin_t::consoleWin_t(QWidget *parent) //QString libpath = QLibraryInfo::location(QLibraryInfo::PluginsPath); //printf("LibPath: '%s'\n", libpath.toStdString().c_str() ); + printf("Running on Platform: %s\n", QGuiApplication::platformName().toStdString().c_str() ); + QApplication::setStyle( new fceuStyle() ); initHotKeys(); diff --git a/src/drivers/Qt/input.cpp b/src/drivers/Qt/input.cpp index 665dd66e..1180d238 100644 --- a/src/drivers/Qt/input.cpp +++ b/src/drivers/Qt/input.cpp @@ -1322,12 +1322,31 @@ UpdatePhysicalInput() // event.type, event.key.keysym.sym, event.key.keysym.scancode ); keyModifier = event.key.keysym.mod; - g_keyState[SDL_SCANCODE_LSHIFT] = (event.key.keysym.mod & KMOD_LSHIFT) ? 1 : 0; - g_keyState[SDL_SCANCODE_RSHIFT] = (event.key.keysym.mod & KMOD_RSHIFT) ? 1 : 0; - g_keyState[SDL_SCANCODE_LALT] = (event.key.keysym.mod & KMOD_LALT) ? 1 : 0; - g_keyState[SDL_SCANCODE_RALT] = (event.key.keysym.mod & KMOD_RALT) ? 1 : 0; - g_keyState[SDL_SCANCODE_LCTRL] = (event.key.keysym.mod & KMOD_LCTRL) ? 1 : 0; - g_keyState[SDL_SCANCODE_RCTRL] = (event.key.keysym.mod & KMOD_RCTRL) ? 1 : 0; + + if ( (event.key.keysym.mod & KMOD_LSHIFT) == 0 ) + { + g_keyState[SDL_SCANCODE_LSHIFT] = 0; + } + if ( (event.key.keysym.mod & KMOD_RSHIFT) == 0 ) + { + g_keyState[SDL_SCANCODE_RSHIFT] = 0; + } + if ( (event.key.keysym.mod & KMOD_LALT) == 0 ) + { + g_keyState[SDL_SCANCODE_LALT] = 0; + } + if ( (event.key.keysym.mod & KMOD_RALT) == 0 ) + { + g_keyState[SDL_SCANCODE_RALT] = 0; + } + if ( (event.key.keysym.mod & KMOD_LCTRL) == 0 ) + { + g_keyState[SDL_SCANCODE_LCTRL] = 0; + } + if ( (event.key.keysym.mod & KMOD_RCTRL) == 0 ) + { + g_keyState[SDL_SCANCODE_RCTRL] = 0; + } g_keyState[event.key.keysym.scancode] = (event.type == SDL_KEYDOWN) ? 1 : 0; diff --git a/src/drivers/Qt/keyscan.cpp b/src/drivers/Qt/keyscan.cpp index 9a022d9f..64e130f5 100644 --- a/src/drivers/Qt/keyscan.cpp +++ b/src/drivers/Qt/keyscan.cpp @@ -27,7 +27,39 @@ using namespace Qt; -SDL_Scancode convQtKey2SDLScanCode(Qt::Key q) +#if defined(WIN32) + +#include +#include +static uint32_t ShiftKeyCodeR = VK_RSHIFT; +static uint32_t CtrlKeyCodeR = VK_RCONTROL; +static uint32_t AltKeyCodeR = VK_RMENU; + +#elif defined(__linux__) + + #if defined(_HAS_XKB) + #include + static uint32_t ShiftKeyCodeR = XKB_KEY_Shift_R; + static uint32_t CtrlKeyCodeR = XKB_KEY_Control_R; + static uint32_t AltKeyCodeR = XKB_KEY_Alt_R; + #elif defined(_HAS_X11) + #include + static uint32_t ShiftKeyCodeR = XK_Shift_R; + static uint32_t CtrlKeyCodeR = XK_Control_R; + static uint32_t AltKeyCodeR = XK_Alt_R; + #else + static uint32_t ShiftKeyCodeR = 0xffe2; + static uint32_t CtrlKeyCodeR = 0xffe4; + static uint32_t AltKeyCodeR = 0xffea; + #endif + +#else +static uint32_t ShiftKeyCodeR = 0xffe2; +static uint32_t CtrlKeyCodeR = 0xffe4; +static uint32_t AltKeyCodeR = 0xffea; +#endif + +SDL_Scancode convQtKey2SDLScanCode(Qt::Key q, uint32_t nativeVirtualKey) { SDL_Scancode s = SDL_SCANCODE_UNKNOWN; @@ -90,16 +122,37 @@ SDL_Scancode convQtKey2SDLScanCode(Qt::Key q) s = SDL_SCANCODE_PAGEDOWN; break; case Key_Shift: - s = SDL_SCANCODE_LSHIFT; + if ( nativeVirtualKey == ShiftKeyCodeR ) + { + s = SDL_SCANCODE_RSHIFT; + } + else + { + s = SDL_SCANCODE_LSHIFT; + } break; case Key_Control: - s = SDL_SCANCODE_LCTRL; + if ( nativeVirtualKey == CtrlKeyCodeR ) + { + s = SDL_SCANCODE_RCTRL; + } + else + { + s = SDL_SCANCODE_LCTRL; + } break; case Key_Meta: s = SDL_SCANCODE_LGUI; break; case Key_Alt: - s = SDL_SCANCODE_LALT; + if ( nativeVirtualKey == AltKeyCodeR ) + { + s = SDL_SCANCODE_RALT; + } + else + { + s = SDL_SCANCODE_LALT; + } break; case Key_CapsLock: s = SDL_SCANCODE_CAPSLOCK; @@ -497,7 +550,7 @@ SDL_Scancode convQtKey2SDLScanCode(Qt::Key q) return s; } -SDL_Keycode convQtKey2SDLKeyCode(Qt::Key q) +SDL_Keycode convQtKey2SDLKeyCode(Qt::Key q, uint32_t nativeVirtualKey) { SDL_Keycode s = SDLK_UNKNOWN; @@ -562,16 +615,37 @@ SDL_Keycode convQtKey2SDLKeyCode(Qt::Key q) s = SDLK_PAGEDOWN; break; case Key_Shift: - s = SDLK_LSHIFT; + if ( nativeVirtualKey == ShiftKeyCodeR ) + { + s = SDLK_RSHIFT; + } + else + { + s = SDLK_LSHIFT; + } break; case Key_Control: - s = SDLK_LCTRL; + if ( nativeVirtualKey == CtrlKeyCodeR ) + { + s = SDLK_RCTRL; + } + else + { + s = SDLK_LCTRL; + } break; case Key_Meta: s = SDLK_LGUI; break; case Key_Alt: - s = SDLK_LALT; + if ( nativeVirtualKey == AltKeyCodeR ) + { + s = SDLK_RALT; + } + else + { + s = SDLK_LALT; + } break; case Key_CapsLock: s = SDLK_CAPSLOCK; @@ -1064,6 +1138,7 @@ int convKeyEvent2Sequence( QKeyEvent *event ) int pushKeyEvent(QKeyEvent *event, int pressDown) { SDL_Event sdlev; + uint32_t vkey; if (pressDown) { @@ -1076,12 +1151,15 @@ int pushKeyEvent(QKeyEvent *event, int pressDown) sdlev.key.state = SDL_RELEASED; } - sdlev.key.keysym.sym = convQtKey2SDLKeyCode((Qt::Key)event->key()); + vkey = event->nativeVirtualKey(); + + sdlev.key.keysym.sym = convQtKey2SDLKeyCode((Qt::Key)event->key(), vkey); sdlev.key.keysym.scancode = SDL_GetScancodeFromKey(sdlev.key.keysym.sym); - //printf("Native ScanCode: x%08X %i \n", event->nativeScanCode(), event->nativeScanCode() ); - //printf("Scancode: 0x%08X %i \n", sdlev.key.keysym.scancode, sdlev.key.keysym.scancode ); + printf("Native ScanCode: x%08X %i \n", event->nativeScanCode(), event->nativeScanCode() ); + printf("Virtual ScanCode: x%08X %i \n", event->nativeVirtualKey(), event->nativeVirtualKey() ); + printf("Scancode: 0x%08X %i \n", sdlev.key.keysym.scancode, sdlev.key.keysym.scancode ); // SDL Docs say this code should never happen, but it does... // so force it to alternative scancode algorithm if it occurs. diff --git a/src/drivers/Qt/keyscan.h b/src/drivers/Qt/keyscan.h index 04e739dd..33305db1 100644 --- a/src/drivers/Qt/keyscan.h +++ b/src/drivers/Qt/keyscan.h @@ -48,9 +48,9 @@ #include #include -SDL_Keycode convQtKey2SDLKeyCode( Qt::Key q ); +SDL_Keycode convQtKey2SDLKeyCode( Qt::Key q, uint32_t nativeVirtualKey = 0 ); -SDL_Scancode convQtKey2SDLScanCode( Qt::Key q ); +SDL_Scancode convQtKey2SDLScanCode( Qt::Key q, uint32_t nativeVirtualKey = 0 ); SDL_Keymod convQtKey2SDLModifier( Qt::KeyboardModifiers m );