Skip to content

Commit 4bd8ead

Browse files
committed
Distinguish numberpad keys.
1 parent 112c73d commit 4bd8ead

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

SerialPrograms/Source/Controllers/KeyboardInput/GlobalQtKeyMap.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ void QtKeyMap::record(const QKeyEvent& event){
2323

2424
Qt::Key qkey = (Qt::Key)qt_key;
2525
WriteSpinLock lg(m_lock);
26-
m_map[native_key].insert(qkey);
26+
m_map[native_key].insert(
27+
QtKey{
28+
qkey,
29+
(event.modifiers() & Qt::KeypadModifier) != 0
30+
}
31+
);
2732
}
2833

29-
std::set<Qt::Key> QtKeyMap::get_QtKeys(uint32_t native_key) const{
34+
std::set<QtKeyMap::QtKey> QtKeyMap::get_QtKeys(uint32_t native_key) const{
3035
ReadSpinLock lg(m_lock);
3136
auto iter = m_map.find(native_key);
3237
if (iter != m_map.end()){

SerialPrograms/Source/Controllers/KeyboardInput/GlobalQtKeyMap.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ namespace PokemonAutomation{
1818

1919

2020
class QtKeyMap{
21+
public:
22+
struct QtKey{
23+
Qt::Key key;
24+
bool keypad;
25+
26+
QtKey(Qt::Key key, bool keypad = false)
27+
: key(key)
28+
, keypad(keypad)
29+
{}
30+
31+
bool operator<(const QtKey& x) const{
32+
if (keypad != x.keypad){
33+
return x.keypad;
34+
}
35+
return key < x.key;
36+
}
37+
};
38+
2139
public:
2240
static QtKeyMap& instance(){
2341
static QtKeyMap map;
@@ -26,7 +44,7 @@ class QtKeyMap{
2644

2745
void record(const QKeyEvent& event);
2846

29-
std::set<Qt::Key> get_QtKeys(uint32_t native_key) const;
47+
std::set<QtKey> get_QtKeys(uint32_t native_key) const;
3048

3149

3250
private:
@@ -36,7 +54,7 @@ class QtKeyMap{
3654
private:
3755
mutable SpinLock m_lock;
3856
// Map of native key to Qt's key ID.
39-
std::map<uint32_t, std::set<Qt::Key>> m_map;
57+
std::map<uint32_t, std::set<QtKey>> m_map;
4058
};
4159

4260

SerialPrograms/Source/Controllers/KeyboardInput/KeyboardInput.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include "Controllers/KeyboardInput/GlobalQtKeyMap.h"
1313
#include "KeyboardInput.h"
1414

15+
//#include <iostream>
16+
//using std::cout;
17+
//using std::endl;
18+
1519
namespace PokemonAutomation{
1620

1721

@@ -54,7 +58,7 @@ void KeyboardInputController::clear_state(){
5458
m_cv.notify_all();
5559
}
5660
void KeyboardInputController::on_key_press(const QKeyEvent& key){
57-
// cout << "press: " << key.key() << ", native = " << key.nativeVirtualKey() << endl;
61+
// cout << "press: " << key.key() << ", native = " << key.nativeVirtualKey() << ", scancode = " << key.nativeScanCode() << endl;
5862

5963
// QKeySequence seq((Qt::Key)key.key());
6064
// cout << "button: " << seq.toString().toStdString() << endl;

SerialPrograms/Source/Controllers/KeyboardInput/KeyboardInput.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ class KeyboardManager : public KeyboardInputController{
110110
DeltaType deltas;
111111
const QtKeyMap& qkey_map = QtKeyMap::instance();
112112
for (uint32_t native_key : pressed_keys){
113-
std::set<Qt::Key> qkeys = qkey_map.get_QtKeys(native_key);
114-
for (Qt::Key qkey : qkeys){
115-
auto iter = m_mapping.find(qkey);
113+
std::set<QtKeyMap::QtKey> qkeys = qkey_map.get_QtKeys(native_key);
114+
for (QtKeyMap::QtKey qkey : qkeys){
115+
auto iter = m_mapping.find(qkey.key);
116116
if (iter != m_mapping.end()){
117117
deltas += iter->second;
118118
break;

SerialPrograms/Source/Controllers/StandardHid/StandardHid_Keyboard.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class KeyboardState : public ControllerState{
6767

6868

6969

70-
const std::map<Qt::Key, KeyboardKey>& KEYID_TO_HID_KEY(){
71-
static const std::map<Qt::Key, KeyboardKey> database{
70+
const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_KEY(){
71+
static const std::map<QtKeyMap::QtKey, KeyboardKey> database{
7272
{Qt::Key::Key_A, KeyboardKey::KEY_A},
7373
{Qt::Key::Key_B, KeyboardKey::KEY_B},
7474
{Qt::Key::Key_C, KeyboardKey::KEY_C},
@@ -157,9 +157,22 @@ const std::map<Qt::Key, KeyboardKey>& KEYID_TO_HID_KEY(){
157157
{Qt::Key::Key_Up, KeyboardKey::KEY_UP},
158158
{Qt::Key::Key_NumLock, KeyboardKey::KEY_NUM_LOCK},
159159

160-
// {Qt::Key::Key_Plus, KeyboardKey::KEY_KP_SLASH},
161-
{Qt::Key::Key_Asterisk, KeyboardKey::KEY_KP_ASTERISK},
162-
{Qt::Key::Key_Plus, KeyboardKey::KEY_KP_PLUS},
160+
{{Qt::Key::Key_Slash, true}, KeyboardKey::KEY_KP_SLASH},
161+
{{Qt::Key::Key_Asterisk, true}, KeyboardKey::KEY_KP_ASTERISK},
162+
{{Qt::Key::Key_Minus, true}, KeyboardKey::KEY_KP_MINUS},
163+
{{Qt::Key::Key_Plus, true}, KeyboardKey::KEY_KP_PLUS},
164+
{{Qt::Key::Key_Enter, true}, KeyboardKey::KEY_KP_ENTER},
165+
{{Qt::Key::Key_1, true}, KeyboardKey::KEY_KP_1},
166+
{{Qt::Key::Key_2, true}, KeyboardKey::KEY_KP_2},
167+
{{Qt::Key::Key_3, true}, KeyboardKey::KEY_KP_3},
168+
{{Qt::Key::Key_4, true}, KeyboardKey::KEY_KP_4},
169+
{{Qt::Key::Key_5, true}, KeyboardKey::KEY_KP_5},
170+
{{Qt::Key::Key_6, true}, KeyboardKey::KEY_KP_6},
171+
{{Qt::Key::Key_7, true}, KeyboardKey::KEY_KP_7},
172+
{{Qt::Key::Key_8, true}, KeyboardKey::KEY_KP_8},
173+
{{Qt::Key::Key_9, true}, KeyboardKey::KEY_KP_9},
174+
{{Qt::Key::Key_0, true}, KeyboardKey::KEY_KP_0},
175+
{{Qt::Key::Key_Period, true}, KeyboardKey::KEY_KP_DOT},
163176

164177
{Qt::Key::Key_F13, KeyboardKey::KEY_F13},
165178
{Qt::Key::Key_F14, KeyboardKey::KEY_F14},
@@ -240,17 +253,18 @@ class Keyboard::KeyboardManager final : public PokemonAutomation::KeyboardInputC
240253
}
241254
virtual void update_state(ControllerState& state, const std::set<uint32_t>& pressed_keys) override{
242255
const QtKeyMap& qkey_map = QtKeyMap::instance();
243-
const std::map<Qt::Key, KeyboardKey>& hid_map = KEYID_TO_HID_KEY();
256+
const std::map<QtKeyMap::QtKey, KeyboardKey>& hid_map = KEYID_TO_HID_KEY();
244257

245258
KeyboardState& local_state = static_cast<KeyboardState&>(state);
246259
local_state.clear();
247260

248261
for (uint32_t native_key : pressed_keys){
249-
std::set<Qt::Key> qkeys = qkey_map.get_QtKeys(native_key);
250-
for (Qt::Key qkey : qkeys){
262+
std::set<QtKeyMap::QtKey> qkeys = qkey_map.get_QtKeys(native_key);
263+
for (QtKeyMap::QtKey qkey : qkeys){
251264
// cout << "qkey = " << qkey << endl;
252265
auto iter = hid_map.find(qkey);
253266
if (iter != hid_map.end()){
267+
// cout << "hid-key = " << (uint16_t)iter->second << endl;
254268
local_state.keys.insert(iter->second);
255269
}
256270
}

0 commit comments

Comments
 (0)