bootmanager: Use cross-platform keyboard input

This commit is contained in:
german77 2021-11-19 10:56:52 -06:00 committed by Narr the Reg
parent 42949738f2
commit 922aa9410a
3 changed files with 58 additions and 39 deletions

View File

@ -52,7 +52,7 @@ public:
void UnloadInputDevices(); void UnloadInputDevices();
/// Number of emulated controllers /// Number of emulated controllers
const std::size_t available_controllers{10}; static constexpr std::size_t available_controllers{10};
private: private:
std::unique_ptr<EmulatedController> player_1; std::unique_ptr<EmulatedController> player_1;

View File

@ -613,69 +613,88 @@ int GRenderWindow::QtKeyToSwitchKey(Qt::Key qt_key) {
} }
} }
int GRenderWindow::QtModifierToSwitchModifier(quint32 qt_modifiers) { int GRenderWindow::QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers) {
int modifier = 0; int modifier = 0;
// The values are obtained through testing, Qt doesn't seem to provide a proper enum
if ((qt_modifiers & 0x1) != 0) { if ((qt_modifiers & Qt::KeyboardModifier::ShiftModifier) != 0) {
modifier |= 1 << Settings::NativeKeyboard::LeftShift; modifier |= 1 << Settings::NativeKeyboard::LeftShift;
} }
if ((qt_modifiers & 0x2) != 0) { if ((qt_modifiers & Qt::KeyboardModifier::ControlModifier) != 0) {
modifier |= 1 << Settings::NativeKeyboard::LeftControl; modifier |= 1 << Settings::NativeKeyboard::LeftControl;
} }
if ((qt_modifiers & 0x4) != 0) { if ((qt_modifiers & Qt::KeyboardModifier::AltModifier) != 0) {
modifier |= 1 << Settings::NativeKeyboard::LeftAlt; modifier |= 1 << Settings::NativeKeyboard::LeftAlt;
} }
if ((qt_modifiers & 0x08) != 0) { if ((qt_modifiers & Qt::KeyboardModifier::MetaModifier) != 0) {
modifier |= 1 << Settings::NativeKeyboard::LeftMeta; modifier |= 1 << Settings::NativeKeyboard::LeftMeta;
} }
if ((qt_modifiers & 0x10) != 0) {
modifier |= 1 << Settings::NativeKeyboard::RightShift; // TODO: These keys can't be obtained with Qt::KeyboardModifier
}
if ((qt_modifiers & 0x20) != 0) { // if ((qt_modifiers & 0x10) != 0) {
modifier |= 1 << Settings::NativeKeyboard::RightControl; // modifier |= 1 << Settings::NativeKeyboard::RightShift;
} // }
if ((qt_modifiers & 0x40) != 0) { // if ((qt_modifiers & 0x20) != 0) {
modifier |= 1 << Settings::NativeKeyboard::RightAlt; // modifier |= 1 << Settings::NativeKeyboard::RightControl;
} // }
if ((qt_modifiers & 0x80) != 0) { // if ((qt_modifiers & 0x40) != 0) {
modifier |= 1 << Settings::NativeKeyboard::RightMeta; // modifier |= 1 << Settings::NativeKeyboard::RightAlt;
} // }
if ((qt_modifiers & 0x100) != 0) { // if ((qt_modifiers & 0x80) != 0) {
modifier |= 1 << Settings::NativeKeyboard::CapsLock; // modifier |= 1 << Settings::NativeKeyboard::RightMeta;
} // }
if ((qt_modifiers & 0x200) != 0) { // if ((qt_modifiers & 0x100) != 0) {
modifier |= 1 << Settings::NativeKeyboard::NumLock; // modifier |= 1 << Settings::NativeKeyboard::CapsLock;
} // }
// Verify the last two keys // if ((qt_modifiers & 0x200) != 0) {
if ((qt_modifiers & 0x400) != 0) { // modifier |= 1 << Settings::NativeKeyboard::NumLock;
modifier |= 1 << Settings::NativeKeyboard::Katakana; // }
} // if ((qt_modifiers & ???) != 0) {
if ((qt_modifiers & 0x800) != 0) { // modifier |= 1 << Settings::NativeKeyboard::ScrollLock;
modifier |= 1 << Settings::NativeKeyboard::Hiragana; // }
} // if ((qt_modifiers & ???) != 0) {
// modifier |= 1 << Settings::NativeKeyboard::Katakana;
// }
// if ((qt_modifiers & ???) != 0) {
// modifier |= 1 << Settings::NativeKeyboard::Hiragana;
// }
return modifier; return modifier;
} }
void GRenderWindow::keyPressEvent(QKeyEvent* event) { void GRenderWindow::keyPressEvent(QKeyEvent* event) {
/**
* This feature can be enhanced with the following functions, but they do not provide
* cross-platform behavior.
*
* event->nativeVirtualKey() can distinguish between keys on the numpad.
* event->nativeModifiers() can distinguish between left and right keys and numlock,
* capslock, scroll lock.
*/
if (!event->isAutoRepeat()) { if (!event->isAutoRepeat()) {
const auto modifier = QtModifierToSwitchModifier(event->nativeModifiers()); const auto modifier = QtModifierToSwitchModifier(event->modifiers());
// Replace event->key() with event->nativeVirtualKey() since the second one provides raw key
// buttons
const auto key = QtKeyToSwitchKey(Qt::Key(event->key())); const auto key = QtKeyToSwitchKey(Qt::Key(event->key()));
input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier); input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier);
input_subsystem->GetKeyboard()->PressKeyboardKey(key); input_subsystem->GetKeyboard()->PressKeyboardKey(key);
// This is used for gamepads // This is used for gamepads that can have any key mapped
input_subsystem->GetKeyboard()->PressKey(event->key()); input_subsystem->GetKeyboard()->PressKey(event->key());
} }
} }
void GRenderWindow::keyReleaseEvent(QKeyEvent* event) { void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
/**
* This feature can be enhanced with the following functions, but they do not provide
* cross-platform behavior.
*
* event->nativeVirtualKey() can distinguish between keys on the numpad.
* event->nativeModifiers() can distinguish between left and right buttons and numlock,
* capslock, scroll lock.
*/
if (!event->isAutoRepeat()) { if (!event->isAutoRepeat()) {
const auto modifier = QtModifierToSwitchModifier(event->nativeModifiers()); const auto modifier = QtModifierToSwitchModifier(event->modifiers());
const auto key = QtKeyToSwitchKey(Qt::Key(event->key())); const auto key = QtKeyToSwitchKey(Qt::Key(event->key()));
input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier); input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier);
input_subsystem->GetKeyboard()->ReleaseKeyboardKey(key); input_subsystem->GetKeyboard()->ReleaseKeyboardKey(key);
// This is used for gamepads // This is used for gamepads that can have any key mapped
input_subsystem->GetKeyboard()->ReleaseKey(event->key()); input_subsystem->GetKeyboard()->ReleaseKey(event->key());
} }
} }

View File

@ -162,7 +162,7 @@ public:
static int QtKeyToSwitchKey(Qt::Key qt_keys); static int QtKeyToSwitchKey(Qt::Key qt_keys);
/// Converts a Qt modifier keys into NativeKeyboard modifier keys /// Converts a Qt modifier keys into NativeKeyboard modifier keys
static int QtModifierToSwitchModifier(quint32 qt_modifiers); static int QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers);
void keyPressEvent(QKeyEvent* event) override; void keyPressEvent(QKeyEvent* event) override;
void keyReleaseEvent(QKeyEvent* event) override; void keyReleaseEvent(QKeyEvent* event) override;