From 5a1fa6008714b96b1506a3f265104a0cfa830b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 6 Jan 2019 23:46:43 +0100 Subject: [PATCH 1/2] Try to support Qt keyboard input directly. Partially fixes #11653 (nickname crash on mac) --- Qt/QtMain.cpp | 26 +++++++++++++++++++++++++- UI/GameSettingsScreen.cpp | 9 +++++---- ext/native/base/NKCodeFromQt.h | 3 ++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index ada30411ad61..f7937d854783 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -31,6 +31,7 @@ #include "gfx_es2/gpu_features.h" #include "math/math_util.h" #include "thread/threadutil.h" +#include "util/text/utf8.h" #include "Core/Config.h" #include "Core/ConfigValues.h" @@ -354,7 +355,30 @@ bool MainUI::event(QEvent *e) NativeKey(KeyInput(DEVICE_ID_MOUSE, ((QWheelEvent*)e)->delta()<0 ? NKCODE_EXT_MOUSEWHEEL_DOWN : NKCODE_EXT_MOUSEWHEEL_UP, KEY_DOWN)); break; case QEvent::KeyPress: - NativeKey(KeyInput(DEVICE_ID_KEYBOARD, KeyMapRawQttoNative.find(((QKeyEvent*)e)->key())->second, KEY_DOWN)); + { + auto qtKeycode = ((QKeyEvent*)e)->key(); + int nativeKeycode = KeyMapRawQttoNative.find(qtKeycode)->second; + NativeKey(KeyInput(DEVICE_ID_KEYBOARD, nativeKeycode, KEY_DOWN)); + // Also get the unicode value. + QString text = ((QKeyEvent*)e)->text(); + std::string str = text.toStdString(); + + // Now, we don't want CHAR events for non-printable characters. Not quite sure how we'll best + // do that, but here's one attempt.... + switch (nativeKeycode) { + case NKCODE_DEL: + case NKCODE_FORWARD_DEL: + case NKCODE_TAB: + break; + default: + if (str.size()) { + int pos = 0; + int code = u8_nextchar(str.c_str(), &pos); + NativeKey(KeyInput(DEVICE_ID_KEYBOARD, code, KEY_CHAR)); + } + break; + } + } break; case QEvent::KeyRelease: NativeKey(KeyInput(DEVICE_ID_KEYBOARD, KeyMapRawQttoNative.find(((QKeyEvent*)e)->key())->second, KEY_UP)); diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 37f5451f2f0a..6f4c284f9422 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -643,7 +643,7 @@ void GameSettingsScreen::CreateViews() { networkingSettings->Add(new CheckBox(&g_Config.bEnableWlan, n->T("Enable networking", "Enable networking/wlan (beta)"))); networkingSettings->Add(new CheckBox(&g_Config.bDiscordPresence, n->T("Send Discord Presence information"))); -#if !defined(MOBILE_DEVICE) && !defined(USING_QT_UI) +#if !defined(MOBILE_DEVICE) networkingSettings->Add(new PopupTextInputChoice(&g_Config.proAdhocServer, n->T("Change proAdhocServer Address"), "", 255, screenManager())); #elif defined(__ANDROID__) networkingSettings->Add(new ChoiceWithValueDisplay(&g_Config.proAdhocServer, n->T("Change proAdhocServer Address"), (const char *)nullptr))->OnClick.Handle(this, &GameSettingsScreen::OnChangeproAdhocServerAddress); @@ -801,13 +801,14 @@ void GameSettingsScreen::CreateViews() { systemSettings->Add(new PopupMultiChoice(&g_Config.iPSPModel, sy->T("PSP Model"), models, 0, ARRAY_SIZE(models), sy->GetName(), screenManager()))->SetEnabled(!PSP_IsInited()); // TODO: Come up with a way to display a keyboard for mobile users, // so until then, this is Windows/Desktop only. -#if !defined(MOBILE_DEVICE) && !defined(USING_QT_UI) // TODO: Add all platforms where KEY_CHAR support is added +#if !defined(MOBILE_DEVICE) // TODO: Add all platforms where KEY_CHAR support is added systemSettings->Add(new PopupTextInputChoice(&g_Config.sNickName, sy->T("Change Nickname"), "", 32, screenManager())); -#elif defined(USING_QT_UI) - systemSettings->Add(new Choice(sy->T("Change Nickname")))->OnClick.Handle(this, &GameSettingsScreen::OnChangeNickname); +// #elif defined(USING_QT_UI) +// systemSettings->Add(new Choice(sy->T("Change Nickname")))->OnClick.Handle(this, &GameSettingsScreen::OnChangeNickname); #elif defined(__ANDROID__) systemSettings->Add(new ChoiceWithValueDisplay(&g_Config.sNickName, sy->T("Change Nickname"), (const char *)nullptr))->OnClick.Handle(this, &GameSettingsScreen::OnChangeNickname); #endif + #if defined(_WIN32) || (defined(USING_QT_UI) && !defined(MOBILE_DEVICE)) // Screenshot functionality is not yet available on non-Windows/non-Qt systemSettings->Add(new CheckBox(&g_Config.bScreenshotsAsPNG, sy->T("Screenshots as PNG"))); diff --git a/ext/native/base/NKCodeFromQt.h b/ext/native/base/NKCodeFromQt.h index 1d2dd2cb6573..ee09d6341700 100644 --- a/ext/native/base/NKCodeFromQt.h +++ b/ext/native/base/NKCodeFromQt.h @@ -98,5 +98,6 @@ static const std::map KeyMapRawQttoNative = InitConstMap (Qt::Key_Up, NKCODE_DPAD_UP) (Qt::Key_Right, NKCODE_DPAD_RIGHT) (Qt::Key_Down, NKCODE_DPAD_DOWN) - (Qt::Key_Back, NKCODE_BACK); + (Qt::Key_Back, NKCODE_BACK) + (Qt::Key_Backspace, NKCODE_DEL); From 5362e675c414f75cfadbc77279b33cd89448e989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 7 Jan 2019 00:28:57 +0100 Subject: [PATCH 2/2] Address feedback --- Qt/QtMain.cpp | 10 +++++++--- UI/GameSettingsScreen.cpp | 2 -- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index f7937d854783..178f3f82c434 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -357,12 +357,16 @@ bool MainUI::event(QEvent *e) case QEvent::KeyPress: { auto qtKeycode = ((QKeyEvent*)e)->key(); - int nativeKeycode = KeyMapRawQttoNative.find(qtKeycode)->second; - NativeKey(KeyInput(DEVICE_ID_KEYBOARD, nativeKeycode, KEY_DOWN)); + auto iter = KeyMapRawQttoNative.find(qtKeycode); + int nativeKeycode = 0; + if (iter != KeyMapRawQttoNative.end()) { + nativeKeycode = iter->second; + NativeKey(KeyInput(DEVICE_ID_KEYBOARD, nativeKeycode, KEY_DOWN)); + } + // Also get the unicode value. QString text = ((QKeyEvent*)e)->text(); std::string str = text.toStdString(); - // Now, we don't want CHAR events for non-printable characters. Not quite sure how we'll best // do that, but here's one attempt.... switch (nativeKeycode) { diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 6f4c284f9422..0f34bdc50c08 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -803,8 +803,6 @@ void GameSettingsScreen::CreateViews() { // so until then, this is Windows/Desktop only. #if !defined(MOBILE_DEVICE) // TODO: Add all platforms where KEY_CHAR support is added systemSettings->Add(new PopupTextInputChoice(&g_Config.sNickName, sy->T("Change Nickname"), "", 32, screenManager())); -// #elif defined(USING_QT_UI) -// systemSettings->Add(new Choice(sy->T("Change Nickname")))->OnClick.Handle(this, &GameSettingsScreen::OnChangeNickname); #elif defined(__ANDROID__) systemSettings->Add(new ChoiceWithValueDisplay(&g_Config.sNickName, sy->T("Change Nickname"), (const char *)nullptr))->OnClick.Handle(this, &GameSettingsScreen::OnChangeNickname); #endif