@@ -12,11 +12,15 @@ import Cocoa
1212
1313/**
1414 Class WebShellMediaKeysSupport
15-
16- This class will support the WebShell media keys.
15+
16+ This class will support the WebShell media keys. \
17+ \
18+ !important note, this class can not communicate with the ViewController.\
19+ The communication goes via NSUserDefaults.
1720 */
1821class WebShellMediaKeysSupport : NSApplication {
19-
22+ let MediaKeysSettings = WebShell ( ) . Settings [ " MediaKeys " ] as! [ String : Bool ]
23+
2024 override func sendEvent( theEvent: NSEvent ) {
2125 if theEvent. type == . SystemDefined && theEvent. subtype. rawValue == 8 {
2226 let keyCode = ( ( theEvent. data1 & 0xFFFF0000 ) >> 16 )
@@ -26,37 +30,115 @@ class WebShellMediaKeysSupport: NSApplication {
2630 let keyRepeat = ( keyFlags & 0x1 )
2731 mediaKeyEvent ( Int32 ( keyCode) , state: keyState, keyRepeat: Bool ( keyRepeat) )
2832 }
29-
33+
3034 super. sendEvent ( theEvent)
3135 }
3236
3337 func mediaKeyEvent( key: Int32 , state: Bool , keyRepeat: Bool ) {
3438 // Only send events on KeyDown. Without this check, these events will happen twice
3539 if ( state) {
3640 switch ( key) {
37- case NX_KEYTYPE_PLAY:
38- print ( " Play! " )
39- // F8 pressed
40- break
41- case NX_KEYTYPE_FAST:
42- print ( " Fast! " )
43- // F9 pressed
41+ case NX_KEYTYPE_PLAY: // F8 / Play
42+ if ( MediaKeysSettings [ " BackAndForward " ] == true ) {
43+ self . goReloadPage ( )
44+ } else {
45+ self . playPausePressed ( )
46+ }
4447 break
45- case NX_KEYTYPE_REWIND:
46- print ( " Rewind! " )
47- // F7 pressed
48+ case NX_KEYTYPE_FAST, NX_KEYTYPE_NEXT: // F9 / Forward
49+ if ( MediaKeysSettings [ " BackAndForward " ] == true ) {
50+ self . goForwardIfPossible ( )
51+ } else {
52+ self . nextItem ( )
53+ }
4854 break
49- case NX_KEYTYPE_PREVIOUS: // Not called?
50- print ( " Previous! " )
51- // F7
52- break
53- case NX_KEYTYPE_NEXT: // Not called?
54- print ( " Next! " )
55- // F9 pressed
55+ case NX_KEYTYPE_REWIND, NX_KEYTYPE_PREVIOUS: // F7 / Backward
56+ if ( MediaKeysSettings [ " BackAndForward " ] == true ) {
57+ self . goBackIfPossible ( )
58+ } else {
59+ self . previousItem ( )
60+ }
5661 break
5762 default :
5863 break
5964 }
6065 }
6166 }
67+
68+ /**
69+ goBackIfPossible
70+
71+ Since we can't communicate with the ViewController.\
72+ We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us.
73+ */
74+ func goBackIfPossible( ) {
75+ NSUserDefaults . standardUserDefaults ( ) . setBool ( true , forKey: " WSGoBack " )
76+ NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
77+ }
78+
79+ /**
80+ goForwardIfPossible
81+
82+ Since we can't communicate with the ViewController.\
83+ We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us.
84+ */
85+ func goForwardIfPossible( ) {
86+ NSUserDefaults . standardUserDefaults ( ) . setBool ( true , forKey: " WSGoForward " )
87+ NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
88+ }
89+
90+ /**
91+ goReloadPage
92+
93+ Since we can't communicate with the ViewController.\
94+ We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us.
95+ */
96+ func goReloadPage( ) {
97+ NSUserDefaults . standardUserDefaults ( ) . setBool ( true , forKey: " WSGoReload " )
98+ NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
99+ }
100+
101+ func nextItem( ) -> Bool {
102+ // ...
103+ return false
104+ }
105+
106+ func previousItem( ) -> Bool {
107+ // ...
108+ return false
109+ }
110+
111+ func playPausePressed( ) -> Bool {
112+ // ...
113+ return false
114+ }
115+ }
116+
117+ extension ViewController {
118+ /**
119+ Communication for the WebShellMediaKeysSupport class
120+
121+ - Parameter Sender: AnyObject (used for #selector use self)
122+ */
123+ func WSMediaLoop( Sender: AnyObject ) -> Void {
124+ self . performSelector ( #selector( ViewController . WSMediaLoop ( _: ) ) , withObject: nil , afterDelay: 0.5 )
125+
126+ if ( NSUserDefaults . standardUserDefaults ( ) . boolForKey ( " WSGoBack " ) ) {
127+ NSUserDefaults . standardUserDefaults ( ) . setBool ( false , forKey: " WSGoBack " )
128+ NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
129+ self . _goBack ( self )
130+ }
131+
132+ if ( NSUserDefaults . standardUserDefaults ( ) . boolForKey ( " WSGoForward " ) ) {
133+ NSUserDefaults . standardUserDefaults ( ) . setBool ( false , forKey: " WSGoForward " )
134+ NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
135+ self . _goForward ( self )
136+ }
137+
138+ if ( NSUserDefaults . standardUserDefaults ( ) . boolForKey ( " WSGoReload " ) ) {
139+ NSUserDefaults . standardUserDefaults ( ) . setBool ( false , forKey: " WSGoReload " )
140+ NSUserDefaults . standardUserDefaults ( ) . synchronize ( )
141+ self . _reloadPage ( self )
142+ }
143+ }
62144}
0 commit comments