Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
MSWindowsKeyState.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
4 * SPDX-FileCopyrightText: (C) 2003 Chris Schoeneman
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include "deskflow/KeyState.h"
11
12#include <string>
13#include <vector>
14
15#define WIN32_LEAN_AND_MEAN
16#include <Windows.h>
17
18class Event;
19class EventQueueTimer;
20class MSWindowsDesks;
21class IEventQueue;
22
24
28{
29public:
31 MSWindowsDesks *desks, void *eventTarget, IEventQueue *events, std::vector<std::string> layouts,
32 bool isLangSyncEnabled
33 );
35 MSWindowsDesks *desks, void *eventTarget, IEventQueue *events, deskflow::KeyMap &keyMap,
36 std::vector<std::string> layouts, bool isLangSyncEnabled
37 );
38 virtual ~MSWindowsKeyState();
39
41
42
44
48 void disable();
49
51
54 void setKeyLayout(HKL keyLayout);
55
57
61 bool testAutoRepeat(bool press, bool isRepeat, KeyButton);
62
64
67 void saveModifiers();
68
70
82 void useSavedModifiers(bool enable);
83
85
87
89
92 KeyButton virtualKeyToButton(UINT virtualKey) const;
93
95
99 KeyID mapKeyFromEvent(WPARAM charAndVirtKey, LPARAM info, KeyModifierMask *maskOut) const;
100
102
106 bool didGroupsChange() const;
107
109
113 UINT mapKeyToVirtualKey(KeyID key) const;
114
116
121 KeyID getKeyID(UINT virtualKey, KeyButton button) const;
122
124
129 UINT mapButtonToVirtualKey(KeyButton button) const;
130
132
133 // IKeyState overrides
134 void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override;
135 bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override;
136 bool fakeCtrlAltDel() override;
137 KeyModifierMask pollActiveModifiers() const override;
138 int32_t pollActiveGroup() const override;
139 void pollPressedKeys(KeyButtonSet &pressedKeys) const override;
140
141 // KeyState overrides
142 void onKey(KeyButton button, bool down, KeyModifierMask newState) override;
143 void sendKeyEvent(
144 void *target, bool press, bool isAutoRepeat, KeyID key, KeyModifierMask mask, int32_t count, KeyButton button
145 ) override;
146
147 // Unit test accessors
149 {
150 return m_lastDown;
151 }
153 {
154 m_lastDown = value;
155 }
157 {
158 return m_savedModifiers;
159 }
161 {
162 m_savedModifiers = value;
163 }
164
165protected:
166 // KeyState overrides
167 void getKeyMap(deskflow::KeyMap &keyMap) override;
168 void fakeKey(const Keystroke &keystroke) override;
170
171private:
172 using GroupList = std::vector<HKL>;
173
174 bool getGroups(GroupList &) const;
175 void setWindowGroup(int32_t group);
176
177 KeyID getIDForKey(deskflow::KeyMap::KeyItem &item, KeyButton button, UINT virtualKey, PBYTE keyState, HKL hkl) const;
178
179 void addKeyEntry(deskflow::KeyMap &keyMap, deskflow::KeyMap::KeyItem &item);
180
181 void init();
182
183private:
184 // not implemented
186 MSWindowsKeyState &operator=(const MSWindowsKeyState &);
187
188private:
189 using GroupMap = std::map<HKL, int32_t>;
190 using KeyToVKMap = std::map<KeyID, UINT>;
191
192 void *m_eventTarget;
193 MSWindowsDesks *m_desks;
194 HKL m_keyLayout;
195 UINT m_buttonToVK[512];
196 UINT m_buttonToNumpadVK[512];
197 KeyButton m_virtualKeyToButton[256];
198 KeyToVKMap m_keyToVKMap;
199 IEventQueue *m_events;
200
201 // the timer used to check for fixing key state
202 EventQueueTimer *m_fixTimer;
203
204 // the groups (keyboard layouts)
205 GroupList m_groups;
206 GroupMap m_groupMap;
207
208 // the last button that we generated a key down event for. this
209 // is zero if the last key event was a key up. we use this to
210 // synthesize key repeats since the low level keyboard hook can't
211 // tell us if an event is a key repeat.
212 KeyButton m_lastDown;
213
214 // modifier tracking
215 bool m_useSavedModifiers;
216 KeyModifierMask m_savedModifiers;
217 KeyModifierMask m_originalSavedModifiers;
218
219 // pointer to ToUnicodeEx. on win95 family this will be nullptr.
220 typedef int(WINAPI *ToUnicodeEx_t)(
221 UINT wVirtKey, UINT wScanCode, PBYTE lpKeyState, LPWSTR pwszBuff, int cchBuff, UINT wFlags, HKL dwhkl
222 );
223 ToUnicodeEx_t m_ToUnicodeEx;
224
225 static const KeyID s_virtualKey[];
226};
static int(PASCAL FAR *bind_winsock)(SOCKET s
int key
Definition KeySequence.cpp:15
uint32_t KeyID
Key ID.
Definition KeyTypes.h:22
uint16_t KeyButton
Key Code.
Definition KeyTypes.h:35
uint32_t KeyModifierMask
Modifier key mask.
Definition KeyTypes.h:45
Definition SimpleEventQueueBuffer.cpp:13
Event.
Definition Event.h:27
Event queue interface.
Definition IEventQueue.h:32
std::set< KeyButton > KeyButtonSet
Definition IKeyState.h:52
KeyState(IEventQueue *events, std::vector< std::string > layouts, bool isLangSyncEnabled)
Definition KeyState.cpp:645
deskflow::KeyMap::Keystroke Keystroke
Definition KeyState.h:81
Microsoft Windows desk handling.
Definition MSWindowsDesks.h:47
Microsoft Windows key mapper.
Definition MSWindowsKeyState.h:28
void useSavedModifiers(bool enable)
Set effective modifier state.
Definition MSWindowsKeyState.cpp:645
KeyModifierMask pollActiveModifiers() const override
Get the active modifiers from OS.
Definition MSWindowsKeyState.cpp:791
void sendKeyEvent(void *target, bool press, bool isAutoRepeat, KeyID key, KeyModifierMask mask, int32_t count, KeyButton button) override
Post a key event.
Definition MSWindowsKeyState.cpp:719
void fakeKey(const Keystroke &keystroke) override
Fake a key event.
Definition MSWindowsKeyState.cpp:1163
bool fakeCtrlAltDel() override
Fake ctrl+alt+del.
Definition MSWindowsKeyState.cpp:765
bool didGroupsChange() const
Check if keyboard groups have changed.
Definition MSWindowsKeyState.cpp:695
virtual ~MSWindowsKeyState()
Definition MSWindowsKeyState.cpp:594
MSWindowsKeyState(MSWindowsDesks *desks, void *eventTarget, IEventQueue *events, std::vector< std::string > layouts, bool isLangSyncEnabled)
Definition MSWindowsKeyState.cpp:558
void saveModifiers()
Remember modifier state.
Definition MSWindowsKeyState.cpp:639
bool testAutoRepeat(bool press, bool isRepeat, KeyButton)
Test and set autorepeat state.
Definition MSWindowsKeyState.cpp:626
void pollPressedKeys(KeyButtonSet &pressedKeys) const override
Get the keys currently pressed from OS.
Definition MSWindowsKeyState.cpp:844
KeyButton getLastDown() const
Definition MSWindowsKeyState.h:148
UINT mapKeyToVirtualKey(KeyID key) const
Map key to virtual key.
Definition MSWindowsKeyState.cpp:701
void getKeyMap(deskflow::KeyMap &keyMap) override
Get the keyboard map.
Definition MSWindowsKeyState.cpp:862
KeyButton virtualKeyToButton(UINT virtualKey) const
Map a virtual key to a button.
Definition MSWindowsKeyState.cpp:616
void setKeyLayout(HKL keyLayout)
Set the active keyboard layout.
Definition MSWindowsKeyState.cpp:621
UINT mapButtonToVirtualKey(KeyButton button) const
Map button to virtual key.
Definition MSWindowsKeyState.cpp:1291
void onKey(KeyButton button, bool down, KeyModifierMask newState) override
Handle key event.
Definition MSWindowsKeyState.cpp:714
bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override
Fake a key repeat.
Definition MSWindowsKeyState.cpp:745
void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override
Fake a key press.
Definition MSWindowsKeyState.cpp:740
KeyID getKeyID(UINT virtualKey, KeyButton button) const
Map virtual key and button to KeyID.
Definition MSWindowsKeyState.cpp:1271
KeyID mapKeyFromEvent(WPARAM charAndVirtKey, LPARAM info, KeyModifierMask *maskOut) const
Map key event to a key.
Definition MSWindowsKeyState.cpp:657
KeyModifierMask & getActiveModifiersRValue() override
Get the active modifiers.
Definition MSWindowsKeyState.cpp:1221
void setSavedModifiers(KeyModifierMask value)
Definition MSWindowsKeyState.h:160
int32_t pollActiveGroup() const override
Get the active keyboard layout from OS.
Definition MSWindowsKeyState.cpp:817
KeyModifierMask getSavedModifiers() const
Definition MSWindowsKeyState.h:156
void setLastDown(KeyButton value)
Definition MSWindowsKeyState.h:152
void disable()
Handle screen disabling.
Definition MSWindowsKeyState.cpp:606
Key map.
Definition KeyMap.h:24
KeyID synthesis info.
Definition KeyMap.h:42