forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachotkeywrapper.mm
284 lines (234 loc) · 7.51 KB
/
machotkeywrapper.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* This file is (c) 2013 Abs62
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "hotkeywrapper.hh"
#include <ApplicationServices/ApplicationServices.h>
namespace MacKeyMapping
{
// Convert Qt key codes to Mac OS X native codes
struct ReverseMapEntry
{
UniChar character;
UInt16 keyCode;
};
static struct ReverseMapEntry * mapping;
static int mapEntries = 0;
void createMapping()
{
if( mapping == NULL )
{
TISInputSourceRef inputSourceRef = TISCopyInputSourceForLanguage( CFSTR( "en" ) );
if ( !inputSourceRef )
inputSourceRef = TISCopyCurrentKeyboardInputSource();
if ( !inputSourceRef )
return;
CFDataRef dataRef = ( CFDataRef )TISGetInputSourceProperty( inputSourceRef,
kTISPropertyUnicodeKeyLayoutData );
if( !dataRef )
return;
const UCKeyboardLayout * keyboardLayoutPtr = ( const UCKeyboardLayout * )CFDataGetBytePtr( dataRef );
if( !keyboardLayoutPtr )
return;
mapping = ( struct ReverseMapEntry * )calloc( 128 , sizeof(struct ReverseMapEntry) );
if( !mapping )
return;
mapEntries = 0;
for( int i = 0; i < 128; i++ )
{
UInt32 theDeadKeyState = 0;
UniCharCount theLength = 0;
if( UCKeyTranslate( keyboardLayoutPtr, i, kUCKeyActionDisplay, 0, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit, &theDeadKeyState, 1, &theLength,
&mapping[ mapEntries ].character ) == noErr && theLength > 0 )
{
if( isprint( mapping[ mapEntries ].character ) )
{
mapping[ mapEntries++ ].keyCode = i;
}
}
}
}
}
quint32 qtKeyToNativeKey( quint32 key )
{
createMapping();
if( mapping == NULL )
return 0;
for( int i = 0; i < mapEntries; i++ )
{
if( mapping[ i ].character == key )
return mapping[ i ].keyCode;
}
return 0;
}
} // namespace MacKeyMapping
static pascal OSStatus hotKeyHandler( EventHandlerCallRef /* nextHandler */, EventRef theEvent, void * userData )
{
EventHotKeyID hkID;
GetEventParameter( theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(EventHotKeyID), NULL, &hkID );
static_cast< HotkeyWrapper * >( userData )->activated( hkID.id );
return noErr;
}
HotkeyWrapper::HotkeyWrapper( QObject *parent )
{
(void) parent;
hotKeyFunction = NewEventHandlerUPP( hotKeyHandler );
EventTypeSpec type;
type.eventClass = kEventClassKeyboard;
type.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler( hotKeyFunction, 1, &type, this, &handlerRef );
keyC = nativeKey( 'c' );
}
HotkeyWrapper::~HotkeyWrapper()
{
unregister();
RemoveEventHandler( handlerRef );
}
void HotkeyWrapper::waitKey2()
{
state2 = false;
}
void HotkeyWrapper::activated( int hkId )
{
if ( state2 )
{ // wait for 2nd key
waitKey2(); // Cancel the 2nd-key wait stage
if ( hkId == state2waiter.id + 1 ||
( hkId == state2waiter.id && state2waiter.key == state2waiter.key2 ) )
{
emit hotkeyActivated( state2waiter.handle );
return;
}
}
for ( int i = 0; i < hotkeys.count(); i++ )
{
HotkeyStruct &hs = hotkeys[ i ];
if( hkId == hs.id )
{
if( hs.key == keyC && hs.modifier == cmdKey )
{
// If that was a copy-to-clipboard shortcut, re-emit it back so it could
// reach its original destination so it could be acted upon.
UnregisterEventHotKey( hs.hkRef );
sendCmdC();
EventHotKeyID hotKeyID;
hotKeyID.signature = 'GDHK';
hotKeyID.id = hs.id;
RegisterEventHotKey( hs.key, hs.modifier, hotKeyID, GetApplicationEventTarget(), 0, &hs.hkRef );
}
if ( hs.key2 == 0 ) {
emit hotkeyActivated( hs.handle );
return;
}
state2 = true;
state2waiter = hs;
QTimer::singleShot( 500, this, SLOT( waitKey2() ) );
return;
}
}
state2 = false;
return;
}
void HotkeyWrapper::unregister()
{
for ( int i = 0; i < hotkeys.count(); i++ )
{
HotkeyStruct const & hk = hotkeys.at( i );
UnregisterEventHotKey( hk.hkRef );
if ( hk.key2 && hk.key2 != hk.key )
UnregisterEventHotKey( hk.hkRef2 );
}
(static_cast< QHotkeyApplication * >( qApp ))->unregisterWrapper( this );
}
bool HotkeyWrapper::setGlobalKey( int key, int key2, Qt::KeyboardModifiers modifier, int handle )
{
if ( !key )
return false; // We don't monitor empty combinations
quint32 vk = nativeKey( key );
if( vk == 0 )
return false;
quint32 vk2 = key2 ? nativeKey( key2 ) : 0;
static int nextId = 1;
if( nextId > 0xBFFF - 1 )
nextId = 1;
quint32 mod = 0;
if( modifier & Qt::CTRL )
mod |= cmdKey;
if( modifier & Qt::ALT )
mod |= optionKey;
if( modifier & Qt::SHIFT )
mod |= shiftKey;
if( modifier & Qt::META )
mod |= controlKey;
hotkeys.append( HotkeyStruct( vk, vk2, mod, handle, nextId ) );
HotkeyStruct &hk = hotkeys.last();
EventHotKeyID hotKeyID;
hotKeyID.signature = 'GDHK';
hotKeyID.id = nextId;
OSStatus ret = RegisterEventHotKey( vk, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef );
if ( ret != 0 )
return false;
if ( vk2 && vk2 != vk )
{
hotKeyID.id = nextId + 1;
ret = RegisterEventHotKey( vk2, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef2 );
}
nextId += 2;
return ret == 0;
}
quint32 HotkeyWrapper::nativeKey( int key )
{
switch( key ) {
case Qt::Key_Escape: return 0x35;
case Qt::Key_Tab: return 0x30;
case Qt::Key_Backspace: return 0x33;
case Qt::Key_Return: return 0x24;
case Qt::Key_Enter: return 0x4c;
case Qt::Key_Delete: return 0x75;
case Qt::Key_Clear: return 0x47;
case Qt::Key_Home: return 0x73;
case Qt::Key_End: return 0x77;
case Qt::Key_Left: return 0x7b;
case Qt::Key_Up: return 0x7e;
case Qt::Key_Right: return 0x7c;
case Qt::Key_Down: return 0x7d;
case Qt::Key_PageUp: return 0x74;
case Qt::Key_PageDown: return 0x79;
case Qt::Key_CapsLock: return 0x57;
case Qt::Key_F1: return 0x7a;
case Qt::Key_F2: return 0x78;
case Qt::Key_F3: return 0x63;
case Qt::Key_F4: return 0x76;
case Qt::Key_F5: return 0x60;
case Qt::Key_F6: return 0x61;
case Qt::Key_F7: return 0x62;
case Qt::Key_F8: return 0x64;
case Qt::Key_F9: return 0x65;
case Qt::Key_F10: return 0x6d;
case Qt::Key_F11: return 0x67;
case Qt::Key_F12: return 0x6f;
case Qt::Key_F13: return 0x69;
case Qt::Key_F14: return 0x6b;
case Qt::Key_F15: return 0x71;
case Qt::Key_Help: return 0x72;
default:;
}
return MacKeyMapping::qtKeyToNativeKey( QChar( key ).toLower().toLatin1() );
}
void HotkeyWrapper::sendCmdC()
{
CGEventFlags flags = kCGEventFlagMaskCommand;
CGEventRef ev;
CGEventSourceRef source = CGEventSourceCreate( kCGEventSourceStateCombinedSessionState );
//press down
ev = CGEventCreateKeyboardEvent( source, keyC, true );
CGEventSetFlags( ev, CGEventFlags( flags | CGEventGetFlags( ev ) ) ); //combine flags
CGEventPost( kCGAnnotatedSessionEventTap, ev );
CFRelease( ev );
//press up
ev = CGEventCreateKeyboardEvent( source, keyC, false );
CGEventSetFlags( ev, CGEventFlags( flags | CGEventGetFlags( ev ) ) ); //combine flags
CGEventPost( kCGAnnotatedSessionEventTap, ev );
CFRelease( ev );
CFRelease( source );
}
EventHandlerUPP HotkeyWrapper::hotKeyFunction = NULL;