Skip to content

Commit 28b7f69

Browse files
committed
Try to fix arrow keys being marked as numpad on Mac.
1 parent befcc92 commit 28b7f69

11 files changed

+154
-37
lines changed

SerialPrograms/Source/ControllerInput/Keyboard/GlobalKeyboardHidTracker.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ void KeyboardHidTracker::on_key_release(const QKeyEvent& key){
9191

9292
KeyboardInputState KeyboardHidTracker::keys_to_state(const std::set<uint32_t>& pressed_native_keys) const{
9393
const QtKeyMap& qkey_map = QtKeyMap::instance();
94-
const std::map<QtKeyMap::QtKey, KeyboardKey>& hid_map = get_keyid_to_hid_map();
94+
const KeyboardInputMappings& hid_map = get_keyid_to_hid_map();
9595

9696
KeyboardInputState ret;
9797
for (uint32_t native_key : pressed_native_keys){
9898
std::set<QtKeyMap::QtKey> qkeys = qkey_map.get_QtKeys(native_key);
99-
for (QtKeyMap::QtKey qkey : qkeys){
100-
auto iter = hid_map.find(qkey);
101-
if (iter != hid_map.end()){
102-
ret.add(iter->second);
99+
for (const QtKeyMap::QtKey& qkey : qkeys){
100+
// cout << "qkey = " << qkey.key << " : " << qkey.keypad << endl;
101+
KeyboardKey key = hid_map.get(qkey);
102+
if (key != KeyboardKey::KEY_NONE){
103+
ret.add(key);
103104
}
104105
// log_qtkey(m_logger, qkey);
105106
}

SerialPrograms/Source/ControllerInput/Keyboard/GlobalKeyboardHidTracker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class QKeyEvent;
2222
namespace PokemonAutomation{
2323

2424

25-
class KeyboardHidTracker : public ControllerInputSource{
25+
class KeyboardHidTracker final : public ControllerInputSource{
2626
public:
2727
~KeyboardHidTracker();
2828
KeyboardHidTracker();

SerialPrograms/Source/ControllerInput/Keyboard/GlobalQtKeyMap.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,35 @@
88
#include "Common/Cpp/Concurrency/SpinLock.h"
99
#include "GlobalQtKeyMap.h"
1010

11+
//#include <iostream>
12+
//using std::cout;
13+
//using std::endl;
14+
1115
namespace PokemonAutomation{
1216

1317

1418

15-
void QtKeyMap::record(const QKeyEvent& event){
19+
QtKeyMap::QtKey QtKeyMap::record(const QKeyEvent& event){
1620
int qt_key = event.key();
1721
uint32_t native_key = event.nativeVirtualKey();
22+
23+
QtKeyMap::QtKey ret{Qt::Key_unknown};
24+
1825
if (native_key == 0 && (qt_key == 0 || qt_key == Qt::Key_unknown)){
1926
// both native key and Qt key event is 0 or unknown, meaning we really don't know
2027
// what key event it is. Skip
21-
return;
28+
return ret;
2229
}
23-
24-
Qt::Key qkey = (Qt::Key)qt_key;
30+
31+
ret.key = (Qt::Key)qt_key;
32+
ret.keypad = (event.modifiers() & Qt::KeypadModifier) != 0;
33+
34+
// cout << "key: " << ret.key << ", keypad = " << ret.keypad << endl;
35+
2536
WriteSpinLock lg(m_lock);
26-
m_map[native_key].insert(
27-
QtKey{
28-
qkey,
29-
(event.modifiers() & Qt::KeypadModifier) != 0
30-
}
31-
);
37+
m_map[native_key].insert(ret);
38+
39+
return ret;
3240
}
3341

3442
std::set<QtKeyMap::QtKey> QtKeyMap::get_QtKeys(uint32_t native_key) const{

SerialPrograms/Source/ControllerInput/Keyboard/GlobalQtKeyMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class QtKeyMap{
4242
return map;
4343
}
4444

45-
void record(const QKeyEvent& event);
45+
QtKeyMap::QtKey record(const QKeyEvent& event);
4646

4747
std::set<QtKey> get_QtKeys(uint32_t native_key) const;
4848

SerialPrograms/Source/ControllerInput/Keyboard/KeyBindingWidget.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,17 @@ KeyboardHidBindingCellWidget::KeyboardHidBindingCellWidget(QWidget& parent, Keyb
3838

3939

4040
void KeyboardHidBindingCellWidget::keyPressEvent(QKeyEvent* event){
41-
QtKeyMap::instance().record(*event);
41+
QtKeyMap::QtKey qkey = QtKeyMap::instance().record(*event);
4242

43-
const std::map<QtKeyMap::QtKey, KeyboardKey>& MAP = get_keyid_to_hid_map();
44-
auto iter = MAP.find(QtKeyMap::QtKey(
45-
(Qt::Key)event->key(),
46-
(event->modifiers() & Qt::KeypadModifier) != 0
47-
));
43+
const KeyboardInputMappings& MAP = get_keyid_to_hid_map();
44+
KeyboardKey key = MAP.get(qkey);
4845

49-
if (iter == MAP.end()){
46+
if (key == KeyboardKey::KEY_NONE){
5047
global_logger_tagged().log("Unable to map Qt::Key to HID ID: " + std::to_string(event->key()));
5148
return;
5249
}
5350

54-
m_value.set(iter->second);
51+
m_value.set(key);
5552
}
5653

5754

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardHidButtons.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace PokemonAutomation{
1616

1717
// Taken from: https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2
1818
enum KeyboardKey : uint8_t{
19+
KEY_NONE = 0x00,
20+
1921
KEY_A = 0x04,
2022
KEY_B = 0x05,
2123
KEY_C = 0x06,

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardInput_KeyMappings.cpp

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,78 @@
99
#include "CommonFramework/GlobalSettingsPanel.h"
1010
#include "KeyboardInput_KeyMappings.h"
1111

12+
//#include <iostream>
13+
//using std::cout;
14+
//using std::endl;
15+
1216
namespace PokemonAutomation{
1317

1418

1519

1620

17-
const std::map<QtKeyMap::QtKey, KeyboardKey>& get_keyid_to_hid_map(){
21+
KeyboardInputMappings::KeyboardInputMappings(std::initializer_list<KeyboardInputMappingEntry> list){
22+
for (auto iter = list.begin(); iter != list.end(); ++iter){
23+
add(*iter);
24+
}
25+
// for (auto& item : m_map){
26+
// cout << item.first << " : " << (int)item.second.standard << ", " << (int)item.second.keypad << endl;
27+
// }
28+
}
29+
void KeyboardInputMappings::add(const KeyboardInputMappingEntry& entry){
30+
Entry& key = m_map[entry.key.key];
31+
32+
if (entry.key.keypad){
33+
if (key.keypad != KeyboardKey::KEY_NONE){
34+
throw InternalProgramError(
35+
nullptr,
36+
PA_CURRENT_FUNCTION,
37+
"Duplicate Keyboard Mapping: " + std::to_string((int)key.keypad)
38+
);
39+
}
40+
key.keypad = entry.hid_id;
41+
}else{
42+
if (key.standard != KeyboardKey::KEY_NONE){
43+
throw InternalProgramError(
44+
nullptr,
45+
PA_CURRENT_FUNCTION,
46+
"Duplicate Keyboard Mapping: " + std::to_string((int)key.keypad)
47+
);
48+
}
49+
key.standard = entry.hid_id;
50+
}
51+
}
52+
KeyboardKey KeyboardInputMappings::get(const QtKeyMap::QtKey& key) const{
53+
// cout << key.key << " : " << key.keypad << endl;
54+
auto iter = m_map.find(key.key);
55+
if (iter == m_map.end()){
56+
return KeyboardKey::KEY_NONE;
57+
}
58+
59+
// If we don't override keypad, just return standard.
60+
if (iter->second.keypad == KeyboardKey::KEY_NONE){
61+
return iter->second.standard;
62+
}
63+
64+
bool keypad = key.keypad;
65+
if (keypad){
66+
return iter->second.keypad;
67+
}
68+
69+
return iter->second.standard;
70+
}
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
const KeyboardInputMappings& get_keyid_to_hid_map(){
1884
KeyboardLayout layout = *GlobalSettings::instance().KEYBOARD_CONTROLS_LAYOUT;
1985
switch (layout){
2086
case KeyboardLayout::QWERTY:

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardInput_KeyMappings.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,55 @@
1414
namespace PokemonAutomation{
1515

1616

17-
const std::map<QtKeyMap::QtKey, KeyboardKey>& get_keyid_to_hid_map();
17+
struct KeyboardInputMappingKey{
18+
Qt::Key key;
19+
bool keypad;
20+
21+
KeyboardInputMappingKey(Qt::Key key)
22+
: key(key)
23+
, keypad(false)
24+
{}
25+
KeyboardInputMappingKey(Qt::Key key, bool keypad)
26+
: key(key)
27+
, keypad(keypad)
28+
{}
29+
};
30+
31+
struct KeyboardInputMappingEntry{
32+
KeyboardInputMappingKey key;
33+
KeyboardKey hid_id;
34+
};
35+
36+
37+
38+
class KeyboardInputMappings{
39+
public:
40+
KeyboardInputMappings(std::initializer_list<KeyboardInputMappingEntry> list);
41+
42+
void add(const KeyboardInputMappingEntry& entry);
43+
44+
KeyboardKey get(const QtKeyMap::QtKey& key) const;
45+
46+
private:
47+
struct Entry{
48+
KeyboardKey standard = KeyboardKey::KEY_NONE;
49+
KeyboardKey keypad = KeyboardKey::KEY_NONE;
50+
};
51+
std::map<Qt::Key, Entry> m_map;
52+
};
53+
54+
55+
56+
57+
58+
59+
const KeyboardInputMappings& get_keyid_to_hid_map();
1860

1961
const std::map<Qt::Key, std::string>& QTKEY_TO_STRING();
2062
const std::map<KeyboardKey, std::string>& KEYBOARDKEY_TO_STRING();
2163

22-
const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_QWERTY();
23-
const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_AZERTY();
64+
const KeyboardInputMappings& KEYID_TO_HID_QWERTY();
65+
const KeyboardInputMappings& KEYID_TO_HID_AZERTY();
2466

2567

2668

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardInput_KeyMappings_AZERTY.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace PokemonAutomation{
1010

1111

1212

13-
const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_AZERTY(){
14-
static const std::map<QtKeyMap::QtKey, KeyboardKey> database{
13+
const KeyboardInputMappings& KEYID_TO_HID_AZERTY(){
14+
static const KeyboardInputMappings database{
1515
{Qt::Key::Key_Q, KeyboardKey::KEY_A}, // Q key -> HID A position
1616
{Qt::Key::Key_B, KeyboardKey::KEY_B},
1717
{Qt::Key::Key_C, KeyboardKey::KEY_C},

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardInput_KeyMappings_QWERTY.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace PokemonAutomation{
1010

1111

1212

13-
const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_QWERTY(){
14-
static const std::map<QtKeyMap::QtKey, KeyboardKey> database{
13+
const KeyboardInputMappings& KEYID_TO_HID_QWERTY(){
14+
static const KeyboardInputMappings database{
1515
{Qt::Key::Key_A, KeyboardKey::KEY_A},
1616
{Qt::Key::Key_B, KeyboardKey::KEY_B},
1717
{Qt::Key::Key_C, KeyboardKey::KEY_C},

0 commit comments

Comments
 (0)