From 85aa2c4f2bd152b7a92496cc32fe9b96fe09a3ef Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 7 May 2018 09:08:39 +0700 Subject: [PATCH] original key event type --- MTMR/CBridge/TouchBarSupport.m | 1 + MTMR/ItemsParsing.swift | 18 +++++++------- MTMR/KeyPress.swift | 43 +++++++++------------------------- 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/MTMR/CBridge/TouchBarSupport.m b/MTMR/CBridge/TouchBarSupport.m index dbd620a..6e92a5f 100644 --- a/MTMR/CBridge/TouchBarSupport.m +++ b/MTMR/CBridge/TouchBarSupport.m @@ -7,6 +7,7 @@ // #import "TouchBarSupport.h" +#import @implementation MediaKeys diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index c6115a5..9bb37ab 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -229,7 +229,7 @@ enum ItemType: Decodable { enum ActionType: Decodable { case none - case hidKey(keycode: Int) + case hidKey(keycode: Int32) case keyPress(keycode: Int) case appleSctipt(source: SourceProtocol) case shellScript(executable: String, parameters: [String]) @@ -258,7 +258,7 @@ enum ActionType: Decodable { let type = try container.decodeIfPresent(ActionTypeRaw.self, forKey: .action) switch type { case .some(.hidKey): - let keycode = try container.decode(Int.self, forKey: .keycode) + let keycode = try container.decode(Int32.self, forKey: .keycode) self = .hidKey(keycode: keycode) case .some(.keyPress): let keycode = try container.decode(Int.self, forKey: .keycode) @@ -282,7 +282,7 @@ enum ActionType: Decodable { enum LongActionType: Decodable { case none - case hidKey(keycode: Int) + case hidKey(keycode: Int32) case keyPress(keycode: Int) case appleSctipt(source: SourceProtocol) case shellScript(executable: String, parameters: [String]) @@ -311,7 +311,7 @@ enum LongActionType: Decodable { let longType = try container.decodeIfPresent(LongActionTypeRaw.self, forKey: .longAction) switch longType { case .some(.hidKey): - let keycode = try container.decode(Int.self, forKey: .keycode) + let keycode = try container.decode(Int32.self, forKey: .keycode) self = .hidKey(keycode: keycode) case .some(.keyPress): let keycode = try container.decode(Int.self, forKey: .keycode) @@ -350,8 +350,9 @@ func ==(lhs: ActionType, rhs: ActionType) -> Bool { switch (lhs, rhs) { case (.none, .none): return true - case let (.hidKey(a), .hidKey(b)), - let (.keyPress(a), .keyPress(b)): + case let (.hidKey(a), .hidKey(b)): + return a == b + case let (.keyPress(a), .keyPress(b)): return a == b case let (.appleSctipt(a), .appleSctipt(b)): return a == b @@ -370,8 +371,9 @@ func ==(lhs: LongActionType, rhs: LongActionType) -> Bool { switch (lhs, rhs) { case (.none, .none): return true - case let (.hidKey(a), .hidKey(b)), - let (.keyPress(a), .keyPress(b)): + case let (.hidKey(a), .hidKey(b)): + return a == b + case let (.keyPress(a), .keyPress(b)): return a == b case let (.appleSctipt(a), .appleSctipt(b)): return a == b diff --git a/MTMR/KeyPress.swift b/MTMR/KeyPress.swift index fe0f109..23870dc 100644 --- a/MTMR/KeyPress.swift +++ b/MTMR/KeyPress.swift @@ -29,45 +29,24 @@ extension KeyPress { } } -func doKey(_ key: Int, down: Bool) { - let flags = NSEvent.ModifierFlags(rawValue: down ? 0xa00 : 0xb00) - let data1 = (key << 16) | ((down ? 0xa : 0xb) << 8) - - let ev = NSEvent.otherEvent( - with: NSEvent.EventType.systemDefined, - location: NSPoint(x:0.0, y:0.0), - modifierFlags: flags, +func doKey(_ key: UInt16, down: Bool) { + let ev = NSEvent.keyEvent( + with: down ? .keyDown : .keyUp, + location: .zero, + modifierFlags: [], timestamp: TimeInterval(0), windowNumber: 0, context: nil, - // context: 0, - subtype: 8, - data1: data1, - data2: -1 - ) + characters: "", + charactersIgnoringModifiers: "", + isARepeat: false, + keyCode: key) let cev = ev!.cgEvent! cev.post(tap: CGEventTapLocation(rawValue: 0)!) } -func HIDPostAuxKey(_ key: Int) { +func HIDPostAuxKey(_ key: Int32) { + let key = UInt16(key) doKey(key, down: true) doKey(key, down: false) } - - -// hidsystem/ev_keymap.h -let NX_KEYTYPE_SOUND_UP = 0 -let NX_KEYTYPE_SOUND_DOWN = 1 -let NX_KEYTYPE_MUTE = 7 - -let NX_KEYTYPE_BRIGHTNESS_UP = 2 -let NX_KEYTYPE_BRIGHTNESS_DOWN = 3 - -let NX_KEYTYPE_PLAY = 16 -let NX_KEYTYPE_NEXT = 17 -let NX_KEYTYPE_PREVIOUS = 18 - - - - -