Deskflow 1.22.0.197
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 <set>
17#include <vector>
18
19class IOSXKeyResource;
20
22
25class OSXKeyState : public KeyState
26{
27public:
28 using KeyIDs = std::vector<KeyID>;
29
30 OSXKeyState(IEventQueue *events, std::vector<std::string> layouts, bool isLangSyncEnabled);
31 OSXKeyState(IEventQueue *events, deskflow::KeyMap &keyMap, std::vector<std::string> layouts, bool isLangSyncEnabled);
32 ~OSXKeyState() override = default;
33
35
36
38
42 void handleModifierKeys(void *target, KeyModifierMask oldMask, KeyModifierMask newMask);
43
45
47
49
53 KeyModifierMask mapModifiersFromOSX(uint32_t mask) const;
54
56
59 KeyModifierMask mapModifiersToCarbon(uint32_t mask) const;
60
62
69 KeyButton mapKeyFromEvent(KeyIDs &ids, KeyModifierMask *maskOut, CGEventRef event) const;
70
72
76 bool
77 mapDeskflowHotKeyToMac(KeyID key, KeyModifierMask mask, uint32_t &macVirtualKey, uint32_t &macModifierMask) const;
78
80
81 // IKeyState overrides
82 bool fakeCtrlAltDel() override;
83 bool fakeMediaKey(KeyID id) override;
84 KeyModifierMask pollActiveModifiers() const override;
85 int32_t pollActiveGroup() const override;
86 void pollPressedKeys(KeyButtonSet &pressedKeys) const override;
87
88 CGEventFlags getModifierStateAsOSXFlags() const;
89
90protected:
91 // KeyState overrides
92 void getKeyMap(deskflow::KeyMap &keyMap) override;
93 void fakeKey(const Keystroke &keystroke) override;
94
95private:
96 class KeyResource;
97
98 // Add hard coded special keys to a deskflow::KeyMap.
99 void getKeyMapForSpecialKeys(deskflow::KeyMap &keyMap, int32_t group) const;
100
101 // Convert keyboard resource to a key map
102 bool getKeyMap(deskflow::KeyMap &keyMap, int32_t group, const IOSXKeyResource &r) const;
103
104 // Get the available keyboard groups
105 bool getGroups(AutoCFArray &) const;
106
107 // Change active keyboard group to group
108 void setGroup(int32_t group);
109
110 // Send an event for the given modifier key
111 void handleModifierKey(void *target, uint32_t virtualKey, KeyID id, bool down, KeyModifierMask newMask);
112
113 // Checks if any in \p ids is a glyph key and if \p isCommand is false.
114 // If so it adds the AltGr modifier to \p mask. This allows OS X
115 // servers to use the option key both as AltGr and as a modifier. If
116 // option is acting as AltGr (i.e. it generates a glyph and there are
117 // no command modifiers active) then we don't send the super modifier
118 // to clients because they'd try to match it as a command modifier.
119 void adjustAltGrModifier(const KeyIDs &ids, KeyModifierMask *mask, bool isCommand) const;
120
121 // Maps an OS X virtual key id to a KeyButton. This simply remaps
122 // the ids so we don't use KeyButton 0.
123 static KeyButton mapVirtualKeyToKeyButton(uint32_t keyCode);
124
125 // Maps a KeyButton to an OS X key code. This is the inverse of
126 // mapVirtualKeyToKeyButton.
127 static uint32_t mapKeyButtonToVirtualKey(KeyButton keyButton);
128
129 void init();
130
131 // Post a key event to HID manager. It posts an event to HID client, a
132 // much lower level than window manager which's the target from carbon
133 // CGEventPost
134 kern_return_t postHIDVirtualKey(uint8_t virtualKeyCode, bool postDown);
135
136 // Get keyboard event flags accorfing to keyboard modifiers
137 CGEventFlags getKeyboardEventFlags() const;
138 CGEventFlags getDeviceDependedFlags() const;
139
140 void setKeyboardModifiers(CGKeyCode virtualKey, bool keyDown);
141
142 void postKeyboardKey(CGKeyCode virtualKey, bool keyDown);
143
144private:
145 // OS X uses a physical key if 0 for the 'A' key. deskflow reserves
146 // KeyButton 0 so we offset all OS X physical key ids by this much
147 // when used as a KeyButton and by minus this much to map a KeyButton
148 // to a physical button.
149 enum
150 {
151 KeyButtonOffset = 1
152 };
153
154 using GroupMap = std::map<CFDataRef, int32_t>;
155 using VirtualKeyMap = std::map<uint32_t, KeyID>;
156
157 VirtualKeyMap m_virtualKeyMap;
158 mutable uint32_t m_deadKeyState;
159 AutoCFArray m_groups{nullptr, CFRelease};
160 GroupMap m_groupMap;
161 bool m_shiftPressed;
162 bool m_controlPressed;
163 bool m_altPressed;
164 bool m_superPressed;
165 bool m_capsPressed;
166};
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
Definition IOSXKeyResource.h:12
KeyState(IEventQueue *events, std::vector< std::string > layouts, bool isLangSyncEnabled)
Definition KeyState.cpp:645
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:28
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