Deskflow 1.24.0.365
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
OSXKeyState.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 "OSXAutoTypes.h"
11#include "deskflow/KeyState.h"
12
13#include <Carbon/Carbon.h>
14
15#include <map>
16#include <vector>
17
18class IOSXKeyResource;
19
21
24class OSXKeyState : public KeyState
25{
26public:
27 using KeyIDs = std::vector<KeyID>;
28
29 OSXKeyState(IEventQueue *events, std::vector<std::string> layouts, bool isLangSyncEnabled);
30 OSXKeyState(IEventQueue *events, deskflow::KeyMap &keyMap, std::vector<std::string> layouts, bool isLangSyncEnabled);
31 ~OSXKeyState() override = default;
32
34
35
37
41 void handleModifierKeys(void *target, KeyModifierMask oldMask, KeyModifierMask newMask);
42
44
46
48
52 KeyModifierMask mapModifiersFromOSX(uint32_t mask) const;
53
55
58 KeyModifierMask mapModifiersToCarbon(uint32_t mask) const;
59
61
68 KeyButton mapKeyFromEvent(KeyIDs &ids, KeyModifierMask *maskOut, CGEventRef event) const;
69
71
75 bool
76 mapDeskflowHotKeyToMac(KeyID key, KeyModifierMask mask, uint32_t &macVirtualKey, uint32_t &macModifierMask) const;
77
79
80 // IKeyState overrides
81 bool fakeCtrlAltDel() override;
82 bool fakeMediaKey(KeyID id) override;
83 KeyModifierMask pollActiveModifiers() const override;
84 int32_t pollActiveGroup() const override;
85 void pollPressedKeys(KeyButtonSet &pressedKeys) const override;
86
87 CGEventFlags getModifierStateAsOSXFlags() const;
88
89protected:
90 // KeyState overrides
91 void getKeyMap(deskflow::KeyMap &keyMap) override;
92 void fakeKey(const Keystroke &keystroke) override;
93
94private:
95 class KeyResource;
96
97 // Add hard coded special keys to a deskflow::KeyMap.
98 void getKeyMapForSpecialKeys(deskflow::KeyMap &keyMap, int32_t group) const;
99
100 // Convert keyboard resource to a key map
101 bool getKeyMap(deskflow::KeyMap &keyMap, int32_t group, const IOSXKeyResource &r) const;
102
103 // Get the available keyboard groups
104 bool getGroups(AutoCFArray &) const;
105
106 // Change active keyboard group to group
107 void setGroup(int32_t group);
108
109 // Send an event for the given modifier key
110 void handleModifierKey(void *target, uint32_t virtualKey, KeyID id, bool down, KeyModifierMask newMask);
111
112 // Checks if any in \p ids is a glyph key and if \p isCommand is false.
113 // If so it adds the AltGr modifier to \p mask. This allows OS X
114 // servers to use the option key both as AltGr and as a modifier. If
115 // option is acting as AltGr (i.e. it generates a glyph and there are
116 // no command modifiers active) then we don't send the super modifier
117 // to clients because they'd try to match it as a command modifier.
118 void adjustAltGrModifier(const KeyIDs &ids, KeyModifierMask *mask, bool isCommand) const;
119
120 // Maps an OS X virtual key id to a KeyButton. This simply remaps
121 // the ids so we don't use KeyButton 0.
122 static KeyButton mapVirtualKeyToKeyButton(uint32_t keyCode);
123
124 // Maps a KeyButton to an OS X key code. This is the inverse of
125 // mapVirtualKeyToKeyButton.
126 static uint32_t mapKeyButtonToVirtualKey(KeyButton keyButton);
127
128 void init();
129
130 // Post a key event to HID manager. It posts an event to HID client, a
131 // much lower level than window manager which's the target from carbon
132 // CGEventPost
133 kern_return_t postHIDVirtualKey(uint8_t virtualKeyCode, bool postDown);
134
135 // Get keyboard event flags accorfing to keyboard modifiers
136 CGEventFlags getKeyboardEventFlags() const;
137 CGEventFlags getDeviceDependedFlags() const;
138
139 void setKeyboardModifiers(CGKeyCode virtualKey, bool keyDown);
140
141 void postKeyboardKey(CGKeyCode virtualKey, bool keyDown);
142
143private:
144 // OS X uses a physical key if 0 for the 'A' key. deskflow reserves
145 // KeyButton 0 so we offset all OS X physical key ids by this much
146 // when used as a KeyButton and by minus this much to map a KeyButton
147 // to a physical button.
148 enum
149 {
150 KeyButtonOffset = 1
151 };
152
153 using GroupMap = std::map<CFDataRef, int32_t>;
154 using VirtualKeyMap = std::map<uint32_t, KeyID>;
155
156 VirtualKeyMap m_virtualKeyMap;
157 mutable uint32_t m_deadKeyState;
158 AutoCFArray m_groups{nullptr, CFRelease};
159 GroupMap m_groupMap;
160 bool m_shiftPressed;
161 bool m_controlPressed;
162 bool m_altPressed;
163 bool m_superPressed;
164 bool m_capsPressed;
165};
int key
Definition KeySequence.cpp:15
uint32_t KeyID
Key ID.
Definition KeyTypes.h:21
uint16_t KeyButton
Key Code.
Definition KeyTypes.h:34
uint32_t KeyModifierMask
Modifier key mask.
Definition KeyTypes.h:44
Event queue interface.
Definition IEventQueue.h:29
std::set< KeyButton > KeyButtonSet
Definition IKeyState.h:52
Definition IOSXKeyResource.h:15
KeyState(IEventQueue *events, std::vector< std::string > layouts, bool isLangSyncEnabled)
Definition KeyState.cpp:643
deskflow::KeyMap::Keystroke Keystroke
Definition KeyState.h:81
int32_t pollActiveGroup() const override
Get the active keyboard layout from OS.
Definition OSXKeyState.cpp:428
~OSXKeyState() override=default
OSXKeyState(IEventQueue *events, std::vector< std::string > layouts, bool isLangSyncEnabled)
Definition OSXKeyState.cpp:182
void getKeyMap(deskflow::KeyMap &keyMap) override
Get the keyboard map.
Definition OSXKeyState.cpp:457
KeyButton mapKeyFromEvent(KeyIDs &ids, KeyModifierMask *maskOut, CGEventRef event) const
Map key event to keys.
Definition OSXKeyState.cpp:263
std::vector< KeyID > KeyIDs
Definition OSXKeyState.h:27
KeyModifierMask pollActiveModifiers() const override
Get the active modifiers from OS.
Definition OSXKeyState.cpp:397
void fakeKey(const Keystroke &keystroke) override
Fake a key event.
Definition OSXKeyState.cpp:594
CGEventFlags getModifierStateAsOSXFlags() const
Definition OSXKeyState.cpp:370
bool mapDeskflowHotKeyToMac(KeyID key, KeyModifierMask mask, uint32_t &macVirtualKey, uint32_t &macModifierMask) const
Map key and mask to native values.
Definition OSXKeyState.cpp:778
KeyModifierMask mapModifiersFromOSX(uint32_t mask) const
Convert OS X modifier mask to deskflow mask.
Definition OSXKeyState.cpp:212
bool fakeMediaKey(KeyID id) override
Fake a media key.
Definition OSXKeyState.cpp:365
bool fakeCtrlAltDel() override
Fake ctrl+alt+del.
Definition OSXKeyState.cpp:359
KeyModifierMask mapModifiersToCarbon(uint32_t mask) const
Convert CG flags-style modifier mask to old-style Carbon.
Definition OSXKeyState.cpp:238
void pollPressedKeys(KeyButtonSet &pressedKeys) const override
Get the keys currently pressed from OS.
Definition OSXKeyState.cpp:443
void handleModifierKeys(void *target, KeyModifierMask oldMask, KeyModifierMask newMask)
Handle modifier key change.
Definition OSXKeyState.cpp:813
Key map.
Definition KeyMap.h:24