77
88import Cocoa
99
10+
1011@NSApplicationMain
1112class 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-
4613 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+ }
4821 var window : NSWindow ! = nil
4922 var view : MainView ! = nil
5023 var prevKeyed : NSDate = NSDate ( )
5124
5225 @IBOutlet weak var menu : NSMenu !
26+ @IBOutlet weak var menuEnabled : NSMenuItem !
5327 @IBOutlet weak var preferences : PreferencesWindow !
28+ @IBOutlet weak var toast : ToastWindow !
5429
5530 func applicationDidFinishLaunching( aNotification: NSNotification ) {
5631
@@ -65,27 +40,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6540 }
6641 // println(e)
6742
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- }
8143
8244
8345 for c in e. charactersIgnoringModifiers!. uppercaseString. unicodeScalars {
8446 println ( NSString ( format: " %08X " , c. value) ) ;
8547 }
8648
49+ let ( mod, char) = Utils . keyStringFromEvent ( e)
8750
88- let char = self . keyToReadableString ( e. charactersIgnoringModifiers!. uppercaseString)
8951 if mod. isEmpty {
9052 let interval = NSDate ( ) . timeIntervalSinceDate ( self . prevKeyed)
9153 if interval > 1 {
@@ -101,8 +63,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
10163 self . prevKeyed = NSDate ( )
10264 }
10365
104- preferences. initControls ( )
105-
10666 let rect = NSRect ( x: 0 , y: 0 , width: 800 , height: 500 )
10767 window = NSWindow ( contentRect: rect, styleMask: NSBorderlessWindowMask, backing: NSBackingStoreType . Buffered, defer: false )
10868 window. opaque = false
@@ -130,7 +90,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
13090 name: NSUserDefaultsDidChangeNotification,
13191 object: nil
13292 )
133- self . userDefaultsDidChange ( NSNotification ( ) )
93+ userDefaultsDidChange ( nil )
13494
13595
13696 /*
@@ -143,11 +103,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
143103*/
144104
145105 enableGlobalAccessibilityFeatures ( )
106+ toast. toast ( " Initialized " )
146107 }
147108
148109 // VoiceOver が起動していない限りアクセシビリティオブジェクトを作らない一部アプリケーション用 (eg. Google Chrome) に
149110 // VoiceOver がセットする属性をセットする。VoiceOver 判定のため自プロセスには設定しない
150111 func enableGlobalAccessibilityFeatures( ) {
112+ println ( " enableGlobalAccessibilityFeatures " )
151113 NSWorkspace . sharedWorkspace ( ) . notificationCenter. addObserver (
152114 self ,
153115 selector: " enableAccessibilityForNewApplication: " ,
@@ -162,7 +124,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
162124 continue
163125 }
164126 let app = AXUIElementCreateApplication ( application. processIdentifier) . takeRetainedValue ( )
165-
127+ println ( " enableGlobalAccessibilityFeatures: \( app ) " )
166128 AXUIElementCopyAttributeValue ( app, " AXEnhancedUserInterface " , & ptr)
167129 AXUIElementSetAttributeValue ( app, " AXEnhancedUserInterface " , 1 )
168130 }
@@ -216,7 +178,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
216178 return false
217179 }
218180
219- func userDefaultsDidChange( aNotification: NSNotification ) {
181+ func userDefaultsDidChange( aNotification: NSNotification ! ) {
220182 window. alphaValue = CGFloat ( preferences. opacity) / 100.0
221183 self . resize ( preferences. width, height: preferences. height)
222184 view. shadowCount = preferences. shadow
@@ -235,24 +197,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
235197 view. setFrameSize ( NSSize ( width: rect. width, height: rect. height) )
236198 }
237199
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-
247200 func updateMenuTitle( ) {
248201 statusItem. title = ( enabled ? " \u{2713} " : " " ) + NSRunningApplication. currentApplication ( ) . localizedName!
249202 }
250203
251204 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 " )
253215 }
254216
255217 func canShowInputByFocusedUIElement( ) -> Bool {
218+ if !preferences. hideNativePasswordInput {
219+ return true
220+ }
221+
256222 var ptr : Unmanaged < AnyObject > ?
257223
258224 let system = AXUIElementCreateSystemWide ( ) . takeRetainedValue ( )
@@ -298,6 +264,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
298264 }
299265 */
300266
267+ /*
301268 if bundleId == "com.apple.Terminal" {
302269 AXUIElementCopyAttributeValue(ui, "AXValue", &ptr)
303270 if ptr != nil {
@@ -312,6 +279,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
312279 }
313280 }
314281 }
282+ */
315283
316284 AXUIElementCopyAttributeValue ( ui, " AXSubrole " , & ptr)
317285 if ptr != nil {
@@ -328,15 +296,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
328296
329297 @IBAction func toggleState( sender: NSMenuItem ) {
330298 enabled = !enabled
331- sender. state = enabled ? 1 : 0
332- updateMenuTitle ( )
333299 }
300+
334301 @IBAction func clearLog( sender: AnyObject ) {
335302 view. clear ( )
336303 }
337304
338305 @IBAction func openPreferencesWindow( sender: AnyObject ) {
339- preferences. orderFrontRegardless ( )
306+ preferences. makeKeyAndOrderFront ( nil )
340307 }
341308
342309 @IBAction func chooseFont( sender: AnyObject ) {
@@ -389,5 +356,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
389356 println(result)
390357 rkkkgg}
391358 */
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+ }
392368}
393369
0 commit comments