Permalink
Browse files

add MediaKeyEvent remapping

  • Loading branch information...
1 parent a6bf15d commit 94f1009714504fa48cc83d6ea8d65ab07e5b0e0e @iMasanari committed Nov 8, 2016
@@ -845,7 +845,7 @@
<popUpButton identifier="hage" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dzf-C4-fIi" customClass="MappingMenu" customModule="_英かな" customModuleProvider="target">
<rect key="frame" x="2" y="-2" width="71" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
- <popUpButtonCell key="cell" type="bevel" title="menu" bezelStyle="regularSquare" image="2EEDBBD4-D504-4B65-92D2-E743C3826DCD" imagePosition="left" alignment="center" lineBreakMode="truncatingTail" state="on" identifier="test" inset="2" pullsDown="YES" id="H2F-I4-Cda">
+ <popUpButtonCell key="cell" type="bevel" title="menu" bezelStyle="regularSquare" image="741A0E3C-7C37-42E9-93D0-C339707E1207" imagePosition="left" alignment="center" lineBreakMode="truncatingTail" state="on" identifier="test" inset="2" pullsDown="YES" id="H2F-I4-Cda">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="BOb-Li-1q3">
@@ -1084,7 +1084,7 @@
</scene>
</scenes>
<resources>
- <image name="2EEDBBD4-D504-4B65-92D2-E743C3826DCD" width="1" height="1">
+ <image name="741A0E3C-7C37-42E9-93D0-C339707E1207" width="1" height="1">
<mutableData key="keyedArchiveRepresentation">
YnBsaXN0MDDUAQIDBAUGPT5YJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoK4HCBMU
GR4fIyQrLjE3OlUkbnVsbNUJCgsMDQ4PEBESVk5TU2l6ZVYkY2xhc3NcTlNJbWFnZUZsYWdzVk5TUmVw
@@ -69,22 +69,22 @@ class KeyEvent: NSObject {
func watch() {
let eventMaskList = [
- CGEventType.keyDown,
- CGEventType.keyUp,
- CGEventType.flagsChanged,
- CGEventType.leftMouseDown,
- CGEventType.leftMouseUp,
- CGEventType.rightMouseDown,
- CGEventType.rightMouseUp,
- CGEventType.otherMouseDown,
- CGEventType.otherMouseUp,
- CGEventType.scrollWheel,
- // CGEventType.MouseMovedMask,
+ CGEventType.keyDown.rawValue,
+ CGEventType.keyUp.rawValue,
+ CGEventType.flagsChanged.rawValue,
+ CGEventType.leftMouseDown.rawValue,
+ CGEventType.leftMouseUp.rawValue,
+ CGEventType.rightMouseDown.rawValue,
+ CGEventType.rightMouseUp.rawValue,
+ CGEventType.otherMouseDown.rawValue,
+ CGEventType.otherMouseUp.rawValue,
+ CGEventType.scrollWheel.rawValue,
+ UInt32(NX_SYSDEFINED) // Media key Event
]
var eventMask: UInt32 = 0
for mask in eventMaskList {
- eventMask |= (1 << mask.rawValue)
+ eventMask |= (1 << mask)
}
let observer = UnsafeMutableRawPointer(Unmanaged.passRetained(self).toOpaque())
@@ -119,26 +119,30 @@ class KeyEvent: NSObject {
return Unmanaged.passRetained(event)
}
+ if let mediaKeyEvent = MediaKeyEvent(event) {
+ return mediaKeyEvent.keyDown ? mediaKeyDown(mediaKeyEvent) : mediaKeyUp(mediaKeyEvent)
+ }
+
switch type {
- case CGEventType.flagsChanged:
- let keyCode = CGKeyCode(event.getIntegerValueField(.keyboardEventKeycode))
-
- if modifierMasks[keyCode] == nil {
- return Unmanaged.passRetained(event)
- }
- return event.flags.rawValue & modifierMasks[keyCode]!.rawValue != 0 ?
- modifierKeyDown(event) : modifierKeyUp(event)
-
- case CGEventType.keyDown:
- return keyDown(event)
-
- case CGEventType.keyUp:
- return keyUp(event)
-
- default:
- self.keyCode = nil
-
+ case CGEventType.flagsChanged:
+ let keyCode = CGKeyCode(event.getIntegerValueField(.keyboardEventKeycode))
+
+ if modifierMasks[keyCode] == nil {
return Unmanaged.passRetained(event)
+ }
+ return event.flags.rawValue & modifierMasks[keyCode]!.rawValue != 0 ?
+ modifierKeyDown(event) : modifierKeyUp(event)
+
+ case CGEventType.keyDown:
+ return keyDown(event)
+
+ case CGEventType.keyUp:
+ return keyUp(event)
+
+ default:
+ self.keyCode = nil
+
+ return Unmanaged.passRetained(event)
}
}
@@ -206,13 +210,50 @@ class KeyEvent: NSObject {
return Unmanaged.passRetained(event)
}
- func getConvertedEvent(_ event: CGEvent) -> CGEvent? {
- // let event = event.copy()!
- let keyCode = KeyboardShortcut(event).keyCode
+ func mediaKeyDown(_ mediaKeyEvent: MediaKeyEvent) -> Unmanaged<CGEvent>? {
+ #if DEBUG
+ print(KeyboardShortcut(keyCode: CGKeyCode(1000 + Int(mediaKeyEvent.keyCode)),
+ flags: CGEventFlags(rawValue: UInt64(mediaKeyEvent.flags))).toString())
+ #endif
+
+ self.keyCode = nil
+
+ if let keyTextField = activeKeyTextField {
+ if keyTextField.isAllowModifierOnly {
+ keyTextField.shortcut = KeyboardShortcut(keyCode: CGKeyCode(1000 + Int(mediaKeyEvent.keyCode)),
+ flags: CGEventFlags(rawValue: UInt64(mediaKeyEvent.flags)))
+ keyTextField.stringValue = keyTextField.shortcut!.toString()
+ }
+
+ return nil
+ }
+
+ if let event = getConvertedEvent(mediaKeyEvent.event, keyCode: CGKeyCode(1000 + Int(mediaKeyEvent.keyCode)), keyDown: true) {
+ event.post(tap: CGEventTapLocation.cghidEventTap)
+ return nil
+ }
+
+ return Unmanaged.passRetained(mediaKeyEvent.event)
+ }
+ func mediaKeyUp(_ mediaKeyEvent: MediaKeyEvent) -> Unmanaged<CGEvent>? {
+
+ if let event = getConvertedEvent(mediaKeyEvent.event, keyCode: CGKeyCode(1000 + Int(mediaKeyEvent.keyCode)), keyDown: false) {
+ event.post(tap: CGEventTapLocation.cghidEventTap)
+ return nil
+ }
+
+ return Unmanaged.passRetained(mediaKeyEvent.event)
+ }
+
+ func getConvertedEvent(_ event: CGEvent, keyCode: CGKeyCode? = nil, keyDown: Bool = false) -> CGEvent? {
+ let event = event.type.rawValue == UInt32(NX_SYSDEFINED) ?
+ CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: keyDown)! : event.copy()!
+
+ let shortcht = KeyboardShortcut(event)
- if let mappingList = shortcutList[keyCode] {
+ if let mappingList = shortcutList[keyCode ?? shortcht.keyCode] {
for mappings in mappingList {
- if KeyboardShortcut(event).isCover(mappings.input) {
+ if shortcht.isCover(mappings.input) {
event.setIntegerValueField(.keyboardEventKeycode, value: Int64(mappings.output.keyCode))
event.flags = CGEventFlags(
rawValue: (event.flags.rawValue & ~mappings.input.flags.rawValue) | mappings.output.flags.rawValue
@@ -129,8 +129,7 @@ class KeyboardShortcut: NSObject {
}
func isCover(_ shortcut: KeyboardShortcut) -> Bool {
- if shortcut.keyCode != self.keyCode ||
- shortcut.isCommandDown() && !self.isCommandDown() ||
+ if shortcut.isCommandDown() && !self.isCommandDown() ||
shortcut.isShiftDown() && !self.isShiftDown() ||
shortcut.isControlDown() && !self.isControlDown() ||
shortcut.isAlternateDown() && !self.isAlternateDown() ||
@@ -202,7 +201,7 @@ let keyCodeDictionary: Dictionary<CGKeyCode, String> = [
54: "Command_R",
55: "Command_L",
56: "Shift_L",
- 57: "Capslock",
+ 57: "CapsLock",
58: "Option_L",
59: "Control_L",
60: "Shift_R",
@@ -269,5 +268,29 @@ let keyCodeDictionary: Dictionary<CGKeyCode, String> = [
131: "Launchpad",
144: "BRIGHTNESS_UP",
145: "BRIGHTNESS_DOWN",
- 160: "Expose_All"
+ 160: "Expose_All",
+
+ // media key (bata)
+ 1000 + UInt16(NX_KEYTYPE_SOUND_UP): "Sound_up",
+ 1000 + UInt16(NX_KEYTYPE_SOUND_DOWN): "Sound_down",
+ 1000 + UInt16(NX_KEYTYPE_BRIGHTNESS_UP): "Brightness_up",
+ 1000 + UInt16(NX_KEYTYPE_BRIGHTNESS_DOWN): "Brightness_down",
+ 1000 + UInt16(NX_KEYTYPE_CAPS_LOCK): "CapsLock",
+ 1000 + UInt16(NX_KEYTYPE_HELP): "HELP",
+ 1000 + UInt16(NX_POWER_KEY): "PowerKey",
+ 1000 + UInt16(NX_KEYTYPE_MUTE): "mute",
+ 1000 + UInt16(NX_KEYTYPE_NUM_LOCK): "NUM_LOCK",
+ 1000 + UInt16(NX_KEYTYPE_CONTRAST_UP): "CONTRAST_UP",
+ 1000 + UInt16(NX_KEYTYPE_CONTRAST_DOWN): "CONTRAST_DOWN",
+ 1000 + UInt16(NX_KEYTYPE_LAUNCH_PANEL): "LAUNCH_PANEL",
+ 1000 + UInt16(NX_KEYTYPE_EJECT): "EJECT",
+ 1000 + UInt16(NX_KEYTYPE_VIDMIRROR): "VIDMIRROR",
+ 1000 + UInt16(NX_KEYTYPE_PLAY): "Play",
+ 1000 + UInt16(NX_KEYTYPE_NEXT): "NEXT",
+ 1000 + UInt16(NX_KEYTYPE_PREVIOUS): "PREVIOUS",
+ 1000 + UInt16(NX_KEYTYPE_FAST): "Fast",
+ 1000 + UInt16(NX_KEYTYPE_REWIND): "Rewind",
+ 1000 + UInt16(NX_KEYTYPE_ILLUMINATION_UP): "Illumination_up",
+ 1000 + UInt16(NX_KEYTYPE_ILLUMINATION_DOWN): "Illumination_down",
+ 1000 + UInt16(NX_KEYTYPE_ILLUMINATION_TOGGLE): "ILLUMINATION_TOGGLE"
]
@@ -0,0 +1,62 @@
+//
+// MediaKeyEvent.swift
+// ⌘英かな
+//
+// MIT License
+// Copyright (c) 2016 iMasanari
+//
+
+import Cocoa
+
+class MediaKeyEvent: NSObject {
+ let event: CGEvent
+ let nsEvent: NSEvent
+
+ var keyCode: Int32
+ var flags: Int
+ var keyDown: Bool
+
+ init?(_ event: CGEvent) {
+ if event.type.rawValue != UInt32(NX_SYSDEFINED) {
+ return nil
+ }
+
+ if let nsEvent = NSEvent(cgEvent: event), nsEvent.subtype.rawValue == 8 {
+ self.nsEvent = nsEvent
+ }
+ else {
+ return nil
+ }
+ self.event = event
+ keyCode = Int32(nsEvent.data1 & 0xffff0000) >> 16
+ flags = nsEvent.data1 & 0x0000ffff
+ keyDown = ((flags & 0xff00) >> 8) == 0xa
+
+ super.init()
+ }
+}
+
+let mediaKeyDic = [
+ NX_KEYTYPE_SOUND_UP: "Sound_up",
+ NX_KEYTYPE_SOUND_DOWN: "Sound_down",
+ NX_KEYTYPE_BRIGHTNESS_UP: "Brightness_up",
+ NX_KEYTYPE_BRIGHTNESS_DOWN: "Brightness_down",
+ NX_KEYTYPE_CAPS_LOCK: "CapsLock",
+ NX_KEYTYPE_HELP: "HELP",
+ NX_POWER_KEY: "PowerKey",
+ NX_KEYTYPE_MUTE: "mute",
+ NX_KEYTYPE_NUM_LOCK: "NUM_LOCK",
+ NX_KEYTYPE_CONTRAST_UP: "CONTRAST_UP",
+ NX_KEYTYPE_CONTRAST_DOWN: "CONTRAST_DOWN",
+ NX_KEYTYPE_LAUNCH_PANEL: "LAUNCH_PANEL",
+ NX_KEYTYPE_EJECT: "EJECT",
+ NX_KEYTYPE_VIDMIRROR: "VIDMIRROR",
+ NX_KEYTYPE_PLAY: "Play",
+ NX_KEYTYPE_NEXT: "NEXT",
+ NX_KEYTYPE_PREVIOUS: "PREVIOUS",
+ NX_KEYTYPE_FAST: "Fast",
+ NX_KEYTYPE_REWIND: "Rewind",
+ NX_KEYTYPE_ILLUMINATION_UP: "Illumination_up",
+ NX_KEYTYPE_ILLUMINATION_DOWN: "Illumination_down",
+ NX_KEYTYPE_ILLUMINATION_TOGGLE: "ILLUMINATION_TOGGLE"
+]
@@ -17,6 +17,7 @@
623E2E071DA60DA800AF163C /* PreferenceWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623E2E061DA60DA800AF163C /* PreferenceWindowController.swift */; };
6242D4BC1DA2455D00CA80DD /* KeyboardShortcut.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6242D4BB1DA2455D00CA80DD /* KeyboardShortcut.swift */; };
6242D4BE1DA395AC00CA80DD /* KeyTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6242D4BD1DA395AC00CA80DD /* KeyTextField.swift */; };
+ 624BF4CA1DD182F500A70E2C /* MediaKeyEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 624BF4C91DD182F500A70E2C /* MediaKeyEvent.swift */; };
627265F61D3CC84000703F85 /* KeyEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 627265F51D3CC84000703F85 /* KeyEvent.swift */; };
628908EE1DADB0B6008EF46A /* KeyMapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628908ED1DADB0B6008EF46A /* KeyMapping.swift */; };
6294281B1D387503001BD4E9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6294281A1D387503001BD4E9 /* AppDelegate.swift */; };
@@ -66,6 +67,7 @@
623E2E061DA60DA800AF163C /* PreferenceWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferenceWindowController.swift; sourceTree = "<group>"; };
6242D4BB1DA2455D00CA80DD /* KeyboardShortcut.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardShortcut.swift; sourceTree = "<group>"; };
6242D4BD1DA395AC00CA80DD /* KeyTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyTextField.swift; sourceTree = "<group>"; };
+ 624BF4C91DD182F500A70E2C /* MediaKeyEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MediaKeyEvent.swift; sourceTree = "<group>"; };
627265F51D3CC84000703F85 /* KeyEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyEvent.swift; sourceTree = "<group>"; };
628908ED1DADB0B6008EF46A /* KeyMapping.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyMapping.swift; sourceTree = "<group>"; };
629428171D387503001BD4E9 /* ⌘英かな.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "⌘英かな.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -157,6 +159,7 @@
62D162E61D3B350B009C95B0 /* toggleLaunchAtStartup.swift */,
627265F51D3CC84000703F85 /* KeyEvent.swift */,
6242D4BB1DA2455D00CA80DD /* KeyboardShortcut.swift */,
+ 624BF4C91DD182F500A70E2C /* MediaKeyEvent.swift */,
62FE51621DC75ECE0037B8D1 /* AppData.swift */,
628908ED1DADB0B6008EF46A /* KeyMapping.swift */,
6294281E1D387503001BD4E9 /* Assets.xcassets */,
@@ -334,6 +337,7 @@
62FE51631DC75ECE0037B8D1 /* AppData.swift in Sources */,
620E0AD71D9E97BA0067F88B /* checkUpdate.swift in Sources */,
62BEA2A41DA739B6004409AE /* ShortcutsController.swift in Sources */,
+ 624BF4CA1DD182F500A70E2C /* MediaKeyEvent.swift in Sources */,
627265F61D3CC84000703F85 /* KeyEvent.swift in Sources */,
620E47771DB3C6E9004A2664 /* MappingMenu.swift in Sources */,
6294281D1D387503001BD4E9 /* ViewController.swift in Sources */,

0 comments on commit 94f1009

Please sign in to comment.