6using namespace swift::misc::input;
10 static const auto &keyMapping()
12 static const QHash<int, KeyCode> hash {
49 { VK_LSHIFT, Key_ShiftLeft },
50 { VK_RSHIFT, Key_ShiftRight },
51 { VK_LCONTROL, Key_ControlLeft },
52 { VK_RCONTROL, Key_ControlRight },
53 { VK_LMENU, Key_AltLeft },
54 { VK_RMENU, Key_AltRight },
60 { VK_DIVIDE, Key_Divide },
63 { VK_ESCAPE, Key_Esc },
73 { VK_MULTIPLY, Key_Multiply },
74 { VK_SUBTRACT, Key_NumpadMinus },
75 { VK_ADD, Key_NumpadPlus },
76 { VK_DECIMAL, Key_NumpadDelete },
77 { VK_NUMPAD0, Key_Numpad0 },
78 { VK_NUMPAD1, Key_Numpad1 },
79 { VK_NUMPAD2, Key_Numpad2 },
80 { VK_NUMPAD3, Key_Numpad3 },
81 { VK_NUMPAD4, Key_Numpad4 },
82 { VK_NUMPAD5, Key_Numpad5 },
83 { VK_NUMPAD6, Key_Numpad6 },
84 { VK_NUMPAD7, Key_Numpad7 },
85 { VK_NUMPAD8, Key_Numpad8 },
86 { VK_NUMPAD9, Key_Numpad9 },
87 { VK_OEM_NEC_EQUAL, Key_NumpadEqual },
89 { VK_OEM_1, Key_OEM1 },
90 { VK_OEM_2, Key_OEM2 },
91 { VK_OEM_3, Key_OEM3 },
92 { VK_OEM_4, Key_OEM4 },
93 { VK_OEM_5, Key_OEM5 },
94 { VK_OEM_6, Key_OEM6 },
95 { VK_OEM_7, Key_OEM7 },
96 { VK_OEM_8, Key_OEM8 },
97 { VK_OEM_102, Key_OEM102 },
98 { VK_F1, Key_Function1 },
99 { VK_F2, Key_Function2 },
100 { VK_F3, Key_Function3 },
101 { VK_F4, Key_Function4 },
102 { VK_F5, Key_Function5 },
103 { VK_F6, Key_Function6 },
104 { VK_F7, Key_Function7 },
105 { VK_F8, Key_Function8 },
106 { VK_F9, Key_Function9 },
107 { VK_F10, Key_Function10 },
108 { VK_F11, Key_Function11 },
109 { VK_F12, Key_Function12 },
110 { VK_F13, Key_Function13 },
111 { VK_F14, Key_Function14 },
112 { VK_F15, Key_Function15 },
113 { VK_F16, Key_Function16 },
114 { VK_F17, Key_Function17 },
115 { VK_F18, Key_Function18 },
116 { VK_F19, Key_Function19 },
117 { VK_F20, Key_Function20 },
118 { VK_F21, Key_Function21 },
119 { VK_F22, Key_Function22 },
120 { VK_F23, Key_Function23 },
121 { VK_F24, Key_Function24 },
137 UnhookWindowsHookEx(m_keyboardHook);
145 Q_ASSERT_X(g_keyboardWindows ==
nullptr,
"CKeyboardWindows::init",
146 "Windows supports only one keyboard instance. Cannot initialize a second one!");
147 g_keyboardWindows =
this;
149 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
150 reinterpret_cast<LPCTSTR
>(&CKeyboardWindows::keyboardProc), &module);
151 m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, CKeyboardWindows::keyboardProc, module, 0);
154 m_pollTimer.start(50);
159 void CKeyboardWindows::processKeyEvent(
DWORD vkcode, WPARAM event)
162 if ((event == WM_KEYDOWN) || (event == WM_SYSKEYDOWN))
164 auto key = keyMapping().value(
static_cast<int>(vkcode));
165 if (key == Key_Unknown)
171 else if ((event == WM_KEYUP) || (event == WM_SYSKEYUP))
173 auto key = keyMapping().value(
static_cast<int>(vkcode));
174 if (key == Key_Unknown)
181 if (oldCombination != m_keyCombination)
183 emit keyCombinationChanged(m_keyCombination);
187 void CKeyboardWindows::pollKeyboardState()
191 for (
int vkey : vkeys)
193 if ((GetKeyState(vkey) & 0x8000) && !m_pressedKeys.contains(vkey))
196 auto key = keyMapping().value(vkey);
197 if (key == Key_Unknown)
201 m_pressedKeys.push_back(vkey);
204 else if (!(GetKeyState(vkey) & 0x8000) && m_pressedKeys.contains(vkey))
207 auto key = keyMapping().value(vkey);
208 if (key == Key_Unknown)
212 m_pressedKeys.removeAll(vkey);
217 if (oldCombination != m_keyCombination)
219 emit keyCombinationChanged(m_keyCombination);
223 LRESULT CALLBACK CKeyboardWindows::keyboardProc(
int nCode, WPARAM wParam, LPARAM lParam)
225 if (nCode == HC_ACTION)
227 const KBDLLHOOKSTRUCT *keyboardEvent =
reinterpret_cast<KBDLLHOOKSTRUCT *
>(lParam);
228 DWORD vkCode = keyboardEvent->vkCode;
229 g_keyboardWindows->processKeyEvent(vkCode, wParam);
231 return CallNextHookEx(g_keyboardWindows->m_keyboardHook, nCode, wParam, lParam);
QFuture< QtFuture::ArgsType< Signal > > connect(Sender *sender, Signal signal)
unsigned long DWORD
Fake Windows DWORD.