-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotKey.swift
More file actions
executable file
·38 lines (29 loc) · 1.04 KB
/
HotKey.swift
File metadata and controls
executable file
·38 lines (29 loc) · 1.04 KB
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
//
// HotKey.swift
// HotKey
//
// Created by Peter Vorwieger on 23.05.15.
// Copyright (c) 2015 Peter Vorwieger. All rights reserved.
//
class HotKey {
static let signature:FourCharCode = UTGetOSTypeFromString( "HOTK" )
static var carbonIDCounter:UInt32 = 0
var hotKeyRefPointer = UnsafeMutablePointer<EventHotKeyRef>.alloc(1)
var carbonID:UInt32
var action:() -> ()
init(shortcut:Shortcut, action:() -> ()) {
self.action = action
self.carbonID = ++HotKey.carbonIDCounter
let hotKeyID = EventHotKeyID(signature:HotKey.signature, id:self.carbonID)
let status = RegisterEventHotKey(shortcut.carbonKeyCode, shortcut.carbonFlags,
hotKeyID, GetEventDispatcherTarget(), 0, self.hotKeyRefPointer)
assert(status == noErr, "RegisterEventHotKey failed \(status)")
}
deinit {
let hotKeyRef = hotKeyRefPointer.memory
if hotKeyRef != nil {
UnregisterEventHotKey(hotKeyRef)
self.hotKeyRefPointer.dealloc(1)
}
}
}