1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

original key event type

This commit is contained in:
Serg 2018-05-07 09:08:39 +07:00
parent b761551384
commit 85aa2c4f2b
3 changed files with 22 additions and 40 deletions

View File

@ -7,6 +7,7 @@
// //
#import "TouchBarSupport.h" #import "TouchBarSupport.h"
#import <IOKit/hidsystem/ev_keymap.h>
@implementation MediaKeys @implementation MediaKeys

View File

@ -229,7 +229,7 @@ enum ItemType: Decodable {
enum ActionType: Decodable { enum ActionType: Decodable {
case none case none
case hidKey(keycode: Int) case hidKey(keycode: Int32)
case keyPress(keycode: Int) case keyPress(keycode: Int)
case appleSctipt(source: SourceProtocol) case appleSctipt(source: SourceProtocol)
case shellScript(executable: String, parameters: [String]) case shellScript(executable: String, parameters: [String])
@ -258,7 +258,7 @@ enum ActionType: Decodable {
let type = try container.decodeIfPresent(ActionTypeRaw.self, forKey: .action) let type = try container.decodeIfPresent(ActionTypeRaw.self, forKey: .action)
switch type { switch type {
case .some(.hidKey): 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) self = .hidKey(keycode: keycode)
case .some(.keyPress): case .some(.keyPress):
let keycode = try container.decode(Int.self, forKey: .keycode) let keycode = try container.decode(Int.self, forKey: .keycode)
@ -282,7 +282,7 @@ enum ActionType: Decodable {
enum LongActionType: Decodable { enum LongActionType: Decodable {
case none case none
case hidKey(keycode: Int) case hidKey(keycode: Int32)
case keyPress(keycode: Int) case keyPress(keycode: Int)
case appleSctipt(source: SourceProtocol) case appleSctipt(source: SourceProtocol)
case shellScript(executable: String, parameters: [String]) case shellScript(executable: String, parameters: [String])
@ -311,7 +311,7 @@ enum LongActionType: Decodable {
let longType = try container.decodeIfPresent(LongActionTypeRaw.self, forKey: .longAction) let longType = try container.decodeIfPresent(LongActionTypeRaw.self, forKey: .longAction)
switch longType { switch longType {
case .some(.hidKey): 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) self = .hidKey(keycode: keycode)
case .some(.keyPress): case .some(.keyPress):
let keycode = try container.decode(Int.self, forKey: .keycode) let keycode = try container.decode(Int.self, forKey: .keycode)
@ -350,8 +350,9 @@ func ==(lhs: ActionType, rhs: ActionType) -> Bool {
switch (lhs, rhs) { switch (lhs, rhs) {
case (.none, .none): case (.none, .none):
return true return true
case let (.hidKey(a), .hidKey(b)), case let (.hidKey(a), .hidKey(b)):
let (.keyPress(a), .keyPress(b)): return a == b
case let (.keyPress(a), .keyPress(b)):
return a == b return a == b
case let (.appleSctipt(a), .appleSctipt(b)): case let (.appleSctipt(a), .appleSctipt(b)):
return a == b return a == b
@ -370,8 +371,9 @@ func ==(lhs: LongActionType, rhs: LongActionType) -> Bool {
switch (lhs, rhs) { switch (lhs, rhs) {
case (.none, .none): case (.none, .none):
return true return true
case let (.hidKey(a), .hidKey(b)), case let (.hidKey(a), .hidKey(b)):
let (.keyPress(a), .keyPress(b)): return a == b
case let (.keyPress(a), .keyPress(b)):
return a == b return a == b
case let (.appleSctipt(a), .appleSctipt(b)): case let (.appleSctipt(a), .appleSctipt(b)):
return a == b return a == b

View File

@ -29,45 +29,24 @@ extension KeyPress {
} }
} }
func doKey(_ key: Int, down: Bool) { func doKey(_ key: UInt16, down: Bool) {
let flags = NSEvent.ModifierFlags(rawValue: down ? 0xa00 : 0xb00) let ev = NSEvent.keyEvent(
let data1 = (key << 16) | ((down ? 0xa : 0xb) << 8) with: down ? .keyDown : .keyUp,
location: .zero,
let ev = NSEvent.otherEvent( modifierFlags: [],
with: NSEvent.EventType.systemDefined,
location: NSPoint(x:0.0, y:0.0),
modifierFlags: flags,
timestamp: TimeInterval(0), timestamp: TimeInterval(0),
windowNumber: 0, windowNumber: 0,
context: nil, context: nil,
// context: 0, characters: "",
subtype: 8, charactersIgnoringModifiers: "",
data1: data1, isARepeat: false,
data2: -1 keyCode: key)
)
let cev = ev!.cgEvent! let cev = ev!.cgEvent!
cev.post(tap: CGEventTapLocation(rawValue: 0)!) 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: true)
doKey(key, down: false) 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