7
7
8
8
import Cocoa
9
9
10
+
10
11
@NSApplicationMain
11
12
class AppDelegate : NSObject , NSApplicationDelegate {
12
- let REPLACE_MAP : Dictionary < String , String > = [
13
- " \r " : " ↵ \n " ,
14
- " \u{1B} " : " ⎋ " ,
15
- " \t " : " ⇥ " ,
16
- " \u{19} " : " ⇤ " ,
17
- " " : " ␣ " ,
18
- " \u{7f} " : " ⌫ " ,
19
- " \u{03} " : " ⌤ " ,
20
- " \u{10} " : " ⏏ " ,
21
- " \u{F728} " : " ⌦ " ,
22
- " \u{F739} " : " ⌧ " ,
23
- " \u{F704} " : " [F1] " ,
24
- " \u{F705} " : " [F2] " ,
25
- " \u{F706} " : " [F3] " ,
26
- " \u{F707} " : " [F4] " ,
27
- " \u{F708} " : " [F5] " ,
28
- " \u{F709} " : " [F6] " ,
29
- " \u{F70A} " : " [F7] " ,
30
- " \u{F70B} " : " [F8] " ,
31
- " \u{F70C} " : " [F9] " ,
32
- " \u{F70D} " : " [F10] " ,
33
- " \u{F70E} " : " [F11] " ,
34
- " \u{F70F} " : " [F12] " ,
35
-
36
- " \u{F700} " : " ↑ " ,
37
- " \u{F701} " : " ↓ " ,
38
- " \u{F702} " : " ← " ,
39
- " \u{F703} " : " → " ,
40
- " \u{F72C} " : " ⇞ " ,
41
- " \u{F72D} " : " ⇟ " ,
42
- " \u{F729} " : " ↖ " ,
43
- " \u{F72B} " : " ↘ " ,
44
- ]
45
-
46
13
let statusItem = NSStatusBar . systemStatusBar ( ) . statusItemWithLength ( - 1 )
47
- var enabled : Bool = true
14
+ var enabled : Bool = true {
15
+ didSet {
16
+ menuEnabled. state = enabled ? 1 : 0
17
+ updateMenuTitle ( )
18
+ toast. toast ( enabled ? " KeyCast is enabled " : " KeyCast is disabled " )
19
+ }
20
+ }
48
21
var window : NSWindow ! = nil
49
22
var view : MainView ! = nil
50
23
var prevKeyed : NSDate = NSDate ( )
51
24
52
25
@IBOutlet weak var menu : NSMenu !
26
+ @IBOutlet weak var menuEnabled : NSMenuItem !
53
27
@IBOutlet weak var preferences : PreferencesWindow !
28
+ @IBOutlet weak var toast : ToastWindow !
54
29
55
30
func applicationDidFinishLaunching( aNotification: NSNotification ) {
56
31
@@ -65,27 +40,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
65
40
}
66
41
// println(e)
67
42
68
- var mod = " "
69
- if e. modifierFlags. rawValue & NSEventModifierFlags . ControlKeyMask. rawValue != 0 {
70
- mod += " ⌃ "
71
- }
72
- if e. modifierFlags. rawValue & NSEventModifierFlags . AlternateKeyMask. rawValue != 0 {
73
- mod += " ⌥ "
74
- }
75
- if e. modifierFlags. rawValue & NSEventModifierFlags . ShiftKeyMask. rawValue != 0 {
76
- mod += " ⇧ "
77
- }
78
- if e. modifierFlags. rawValue & NSEventModifierFlags . CommandKeyMask. rawValue != 0 {
79
- mod += " ⌘ "
80
- }
81
43
82
44
83
45
for c in e. charactersIgnoringModifiers!. uppercaseString. unicodeScalars {
84
46
println ( NSString ( format: " %08X " , c. value) ) ;
85
47
}
86
48
49
+ let ( mod, char) = Utils . keyStringFromEvent ( e)
87
50
88
- let char = self . keyToReadableString ( e. charactersIgnoringModifiers!. uppercaseString)
89
51
if mod. isEmpty {
90
52
let interval = NSDate ( ) . timeIntervalSinceDate ( self . prevKeyed)
91
53
if interval > 1 {
@@ -101,8 +63,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
101
63
self . prevKeyed = NSDate ( )
102
64
}
103
65
104
- preferences. initControls ( )
105
-
106
66
let rect = NSRect ( x: 0 , y: 0 , width: 800 , height: 500 )
107
67
window = NSWindow ( contentRect: rect, styleMask: NSBorderlessWindowMask, backing: NSBackingStoreType . Buffered, defer: false )
108
68
window. opaque = false
@@ -130,7 +90,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
130
90
name: NSUserDefaultsDidChangeNotification,
131
91
object: nil
132
92
)
133
- self . userDefaultsDidChange ( NSNotification ( ) )
93
+ userDefaultsDidChange ( nil )
134
94
135
95
136
96
/*
@@ -143,11 +103,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
143
103
*/
144
104
145
105
enableGlobalAccessibilityFeatures ( )
106
+ toast. toast ( " Initialized " )
146
107
}
147
108
148
109
// VoiceOver が起動していない限りアクセシビリティオブジェクトを作らない一部アプリケーション用 (eg. Google Chrome) に
149
110
// VoiceOver がセットする属性をセットする。VoiceOver 判定のため自プロセスには設定しない
150
111
func enableGlobalAccessibilityFeatures( ) {
112
+ println ( " enableGlobalAccessibilityFeatures " )
151
113
NSWorkspace . sharedWorkspace ( ) . notificationCenter. addObserver (
152
114
self ,
153
115
selector: " enableAccessibilityForNewApplication: " ,
@@ -162,7 +124,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
162
124
continue
163
125
}
164
126
let app = AXUIElementCreateApplication ( application. processIdentifier) . takeRetainedValue ( )
165
-
127
+ println ( " enableGlobalAccessibilityFeatures: \( app ) " )
166
128
AXUIElementCopyAttributeValue ( app, " AXEnhancedUserInterface " , & ptr)
167
129
AXUIElementSetAttributeValue ( app, " AXEnhancedUserInterface " , 1 )
168
130
}
@@ -216,7 +178,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
216
178
return false
217
179
}
218
180
219
- func userDefaultsDidChange( aNotification: NSNotification ) {
181
+ func userDefaultsDidChange( aNotification: NSNotification ! ) {
220
182
window. alphaValue = CGFloat ( preferences. opacity) / 100.0
221
183
self . resize ( preferences. width, height: preferences. height)
222
184
view. shadowCount = preferences. shadow
@@ -235,24 +197,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
235
197
view. setFrameSize ( NSSize ( width: rect. width, height: rect. height) )
236
198
}
237
199
238
- func keyToReadableString ( string: String ) -> String {
239
- var str = string
240
- for (k, v) in self . REPLACE_MAP {
241
- str = str. stringByReplacingOccurrencesOfString ( k, withString: v)
242
- }
243
- return str
244
- }
245
-
246
-
247
200
func updateMenuTitle( ) {
248
201
statusItem. title = ( enabled ? " \u{2713} " : " " ) + NSRunningApplication. currentApplication ( ) . localizedName!
249
202
}
250
203
251
204
func canShowInput( ) -> Bool {
252
- return enabled && canShowInputByFocusedUIElement ( )
205
+ return enabled && canShowInputByRunningProcesses ( ) && canShowInputByFocusedUIElement ( )
206
+ }
207
+
208
+ func canShowInputByRunningProcesses( ) -> Bool {
209
+ if !preferences. hideSudoInProcessList {
210
+ return true
211
+ }
212
+
213
+ // sudo 実行中は入力を表示しない
214
+ return !IsInBSDProcessList( " sudo " )
253
215
}
254
216
255
217
func canShowInputByFocusedUIElement( ) -> Bool {
218
+ if !preferences. hideNativePasswordInput {
219
+ return true
220
+ }
221
+
256
222
var ptr : Unmanaged < AnyObject > ?
257
223
258
224
let system = AXUIElementCreateSystemWide ( ) . takeRetainedValue ( )
@@ -298,6 +264,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
298
264
}
299
265
*/
300
266
267
+ /*
301
268
if bundleId == "com.apple.Terminal" {
302
269
AXUIElementCopyAttributeValue(ui, "AXValue", &ptr)
303
270
if ptr != nil {
@@ -312,6 +279,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
312
279
}
313
280
}
314
281
}
282
+ */
315
283
316
284
AXUIElementCopyAttributeValue ( ui, " AXSubrole " , & ptr)
317
285
if ptr != nil {
@@ -328,15 +296,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
328
296
329
297
@IBAction func toggleState( sender: NSMenuItem ) {
330
298
enabled = !enabled
331
- sender. state = enabled ? 1 : 0
332
- updateMenuTitle ( )
333
299
}
300
+
334
301
@IBAction func clearLog( sender: AnyObject ) {
335
302
view. clear ( )
336
303
}
337
304
338
305
@IBAction func openPreferencesWindow( sender: AnyObject ) {
339
- preferences. orderFrontRegardless ( )
306
+ preferences. makeKeyAndOrderFront ( nil )
340
307
}
341
308
342
309
@IBAction func chooseFont( sender: AnyObject ) {
@@ -389,5 +356,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
389
356
println(result)
390
357
rkkkgg}
391
358
*/
359
+
360
+ // osascript -e 'tell application "KeyCast"' -e 'set enabled to true' -e 'end tell'
361
+ // osascript -e 'tell application "KeyCast"' -e 'set enabled to false' -e 'end tell'
362
+ override func application( sender: NSApplication , delegateHandlesKey key: String ) -> Bool {
363
+ if key == " enabled " {
364
+ return true
365
+ }
366
+ return false
367
+ }
392
368
}
393
369
0 commit comments