Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
KeyState.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) 2004 Chris Schoeneman
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include "deskflow/IKeyState.h"
11#include "deskflow/KeyMap.h"
12
14
18class KeyState : public IKeyState
19{
20public:
21 KeyState(IEventQueue *events, std::vector<std::string> layouts, bool isLangSyncEnabled);
22 KeyState(IEventQueue *events, deskflow::KeyMap &keyMap, std::vector<std::string> layouts, bool isLangSyncEnabled);
23 ~KeyState() override;
24
26
27
29
35 virtual void onKey(KeyButton button, bool down, KeyModifierMask newState);
36
38
43 virtual void sendKeyEvent(
44 void *target, bool press, bool isAutoRepeat, KeyID key, KeyModifierMask mask, int32_t count, KeyButton button
45 );
46
48
50
52
53 void updateKeyMap(deskflow::KeyMap *existing);
54 // IKeyState overrides
55 void updateKeyMap() override
56 {
57 this->updateKeyMap(nullptr);
58 }
59 void updateKeyState() override;
61 void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override;
62 bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override;
63 bool fakeKeyUp(KeyButton button) override;
64 void fakeAllKeysUp() override;
65 bool fakeMediaKey(KeyID id) override;
66
67 bool isKeyDown(KeyButton) const override;
68 KeyModifierMask getActiveModifiers() const override;
69 // Left abstract
70 bool fakeCtrlAltDel() override = 0;
72 int32_t pollActiveGroup() const override = 0;
73 void pollPressedKeys(KeyButtonSet &pressedKeys) const override = 0;
74
75 int32_t getKeyState(KeyButton keyButton) const
76 {
77 return m_keys[keyButton];
78 }
79
80protected:
82
84
85
87
90 virtual void getKeyMap(deskflow::KeyMap &keyMap) = 0;
91
93
96 virtual void fakeKey(const Keystroke &keystroke) = 0;
97
99
104
106
108
110
113 int32_t getEffectiveGroup(int32_t group, int32_t offset) const;
114
116
120 virtual bool isIgnoredKey(KeyID key, KeyModifierMask mask) const;
121
123
127 KeyButton getButton(KeyID id, int32_t group) const;
128
130
131private:
132 using Keystrokes = deskflow::KeyMap::Keystrokes;
133 using ModifierToKeys = deskflow::KeyMap::ModifierToKeys;
134
135public:
137 {
138 public:
139 AddActiveModifierContext(int32_t group, KeyModifierMask mask, ModifierToKeys &activeModifiers);
140
141 public:
144 ModifierToKeys &m_activeModifiers;
145
146 private:
147 // not implemented
150 };
151
152private:
153 class ButtonToKeyLess
154 {
155 public:
156 bool operator()(
157 const deskflow::KeyMap::ButtonToKeyMap::value_type &a, const deskflow::KeyMap::ButtonToKeyMap::value_type b
158 ) const
159 {
160 return (a.first < b.first);
161 }
162 };
163
164 // not implemented
165 KeyState(const KeyState &);
166 KeyState &operator=(const KeyState &);
167
168 // called by all ctors.
169 void init();
170
171 // adds alias key sequences. these are sequences that are equivalent
172 // to other sequences.
173 void addAliasEntries();
174
175 // adds non-keypad key sequences for keypad KeyIDs
176 void addKeypadEntries();
177
178 // adds key sequences for combination KeyIDs (those built using
179 // dead keys)
180 void addCombinationEntries();
181
182 // synthesize key events. synthesize auto-repeat events count times.
183 void fakeKeys(const Keystrokes &, uint32_t count);
184
185 // update key state to match changes to modifiers
186 void updateModifierKeyState(KeyButton button, const ModifierToKeys &oldModifiers, const ModifierToKeys &newModifiers);
187
188 // active modifiers collection callback
189 static void addActiveModifierCB(KeyID id, int32_t group, deskflow::KeyMap::KeyItem &keyItem, void *vcontext);
190
191private:
192 // must be declared before m_keyMap. used when this class owns the key map.
193 deskflow::KeyMap *m_keyMapPtr;
194
195 // the keyboard map
196 deskflow::KeyMap &m_keyMap;
197
198 // current modifier state
199 KeyModifierMask m_mask;
200
201 // the active modifiers and the buttons activating them
202 ModifierToKeys m_activeModifiers;
203
204 // current keyboard state (> 0 if pressed, 0 otherwise). this is
205 // initialized to the keyboard state according to the system then
206 // it tracks synthesized events.
207 int32_t m_keys[s_numButtons];
208
209 // synthetic keyboard state (> 0 if pressed, 0 otherwise). this
210 // tracks the synthesized keyboard state. if m_keys[n] > 0 but
211 // m_syntheticKeys[n] == 0 then the key was pressed locally and
212 // not synthesized yet.
213 int32_t m_syntheticKeys[s_numButtons];
214
215 // client data for each pressed key
216 uint32_t m_keyClientData[s_numButtons];
217
218 // server keyboard state. an entry is 0 if not the key isn't pressed
219 // otherwise it's the local KeyButton synthesized for the server key.
220 KeyButton m_serverKeys[s_numButtons];
221
222 IEventQueue *m_events;
223
224 bool m_isLangSyncEnabled;
225};
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
Event queue interface.
Definition IEventQueue.h:32
std::set< KeyButton > KeyButtonSet
Definition IKeyState.h:52
IKeyState(const IEventQueue *events)
Definition IKeyState.cpp:18
static const auto s_numButtons
Definition IKeyState.h:27
KeyModifierMask getActiveModifiers() const override
Get the active modifiers.
Definition KeyState.cpp:969
void fakeAllKeysUp() override
Fake key releases for all fake pressed keys.
Definition KeyState.cpp:943
void setHalfDuplexMask(KeyModifierMask) override
Set half-duplex mask.
Definition KeyState.cpp:782
virtual void onKey(KeyButton button, bool down, KeyModifierMask newState)
Handle key event.
Definition KeyState.cpp:685
KeyModifierMask pollActiveModifiers() const override=0
Get the active modifiers from OS.
KeyState(IEventQueue *events, std::vector< std::string > layouts, bool isLangSyncEnabled)
Definition KeyState.cpp:645
virtual void getKeyMap(deskflow::KeyMap &keyMap)=0
Get the keyboard map.
virtual void sendKeyEvent(void *target, bool press, bool isAutoRepeat, KeyID key, KeyModifierMask mask, int32_t count, KeyButton button)
Post a key event.
Definition KeyState.cpp:707
bool fakeKeyUp(KeyButton button) override
Fake a key release.
Definition KeyState.cpp:900
int32_t getKeyState(KeyButton keyButton) const
Definition KeyState.h:75
KeyButton getButton(KeyID id, int32_t group) const
Get button for a KeyID.
Definition KeyState.cpp:997
virtual KeyModifierMask & getActiveModifiersRValue()
Get the active modifiers.
Definition KeyState.cpp:974
deskflow::KeyMap::Keystroke Keystroke
Definition KeyState.h:81
virtual bool isIgnoredKey(KeyID key, KeyModifierMask mask) const
Check if key is ignored.
Definition KeyState.cpp:984
virtual void fakeKey(const Keystroke &keystroke)=0
Fake a key event.
void pollPressedKeys(KeyButtonSet &pressedKeys) const override=0
Get the keys currently pressed from OS.
int32_t getEffectiveGroup(int32_t group, int32_t offset) const
Compute a group number.
Definition KeyState.cpp:979
int32_t pollActiveGroup() const override=0
Get the active keyboard layout from OS.
void updateKeyMap() override
Update the keyboard map.
Definition KeyState.h:55
void updateKeyState() override
Update the key state.
Definition KeyState.cpp:748
bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override
Fake a key repeat.
Definition KeyState.cpp:843
bool fakeMediaKey(KeyID id) override
Fake a media key.
Definition KeyState.cpp:959
void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override
Fake a key press.
Definition KeyState.cpp:796
bool fakeCtrlAltDel() override=0
Fake ctrl+alt+del.
bool isKeyDown(KeyButton) const override
Test if key is pressed.
Definition KeyState.cpp:964
~KeyState() override
Definition KeyState.cpp:671
A keystroke.
Definition KeyMap.h:72
Key map.
Definition KeyMap.h:24
std::multimap< KeyModifierMask, KeyItem > ModifierToKeys
A mapping of a modifier to keys for that modifier.
Definition KeyMap.h:114
std::vector< Keystroke > Keystrokes
A sequence of keystrokes.
Definition KeyMap.h:111
int32_t m_activeGroup
Definition KeyState.h:142
ModifierToKeys & m_activeModifiers
Definition KeyState.h:144
KeyModifierMask m_mask
Definition KeyState.h:143
AddActiveModifierContext(int32_t group, KeyModifierMask mask, ModifierToKeys &activeModifiers)
Definition KeyState.cpp:1142