mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
convenient keycode buttons
This commit is contained in:
parent
2dc1148a2a
commit
ee0eab6a76
@ -66,44 +66,28 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
||||||
switch identifier {
|
switch identifier {
|
||||||
case .escButton:
|
case .escButton:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "esc", key: ESCKeyPress())
|
||||||
item.view = NSButton(title: "esc", target: self, action: #selector(handleEsc))
|
|
||||||
return item
|
|
||||||
case .dismissButton:
|
case .dismissButton:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
let item = NSCustomTouchBarItem(identifier: identifier)
|
||||||
item.view = NSButton(title: "exit", target: self, action: #selector(dismissTouchBar))
|
item.view = NSButton(title: "exit", target: self, action: #selector(dismissTouchBar))
|
||||||
return item
|
return item
|
||||||
|
|
||||||
case .brightUp:
|
case .brightUp:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔆", key: BrightnessUpPress())
|
||||||
item.view = NSButton(title: "🔆", target: self, action: #selector(handleBrightUp))
|
|
||||||
return item
|
|
||||||
case .brightDown:
|
case .brightDown:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔅", key: BrightnessDownPress())
|
||||||
item.view = NSButton(title: "🔅", target: self, action: #selector(handleBrightDown))
|
|
||||||
return item
|
|
||||||
|
|
||||||
case .volumeDown:
|
case .volumeDown:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔉", HIDKeycode: NX_KEYTYPE_SOUND_DOWN)
|
||||||
item.view = NSButton(title: "🔉", target: self, action: #selector(handleVolumeDown))
|
|
||||||
return item
|
|
||||||
case .volumeUp:
|
case .volumeUp:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔊", HIDKeycode: NX_KEYTYPE_SOUND_UP)
|
||||||
item.view = NSButton(title: "🔊", target: self, action: #selector(handleVolumeUp))
|
|
||||||
return item
|
|
||||||
|
|
||||||
case .prev:
|
case .prev:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "⏪", HIDKeycode: NX_KEYTYPE_PREVIOUS)
|
||||||
item.view = NSButton(title: "⏪", target: self, action: #selector(handlePrev))
|
|
||||||
return item
|
|
||||||
case .play:
|
case .play:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "⏯", HIDKeycode: NX_KEYTYPE_PLAY)
|
||||||
item.view = NSButton(title: "⏯", target: self, action: #selector(handlePlay))
|
|
||||||
return item
|
|
||||||
case .next:
|
case .next:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "⏩", HIDKeycode: NX_KEYTYPE_NEXT)
|
||||||
item.view = NSButton(title: "⏩", target: self, action: #selector(handleNext))
|
|
||||||
return item
|
|
||||||
|
|
||||||
case .time:
|
case .time:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
let item = NSCustomTouchBarItem(identifier: identifier)
|
||||||
@ -128,45 +112,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
timeButton.title = getCurrentTime()
|
timeButton.title = getCurrentTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func handleEsc() {
|
|
||||||
let sender = ESCKeyPress()
|
|
||||||
sender.send()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleVolumeUp() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_UP))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleVolumeDown() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_DOWN))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleBrightDown() {
|
|
||||||
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_DOWN))
|
|
||||||
|
|
||||||
let sender = BrightnessUpPress()
|
|
||||||
sender.send()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleBrightUp() {
|
|
||||||
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_UP))
|
|
||||||
|
|
||||||
let sender = BrightnessDownPress()
|
|
||||||
sender.send()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handlePrev() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_PREVIOUS))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handlePlay() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_PLAY))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleNext() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_NEXT))
|
|
||||||
}
|
|
||||||
|
|
||||||
// func getBattery() {
|
// func getBattery() {
|
||||||
// var error: NSDictionary?
|
// var error: NSDictionary?
|
||||||
// if let scriptObject = NSAppleScript(source: <#T##String#>) {
|
// if let scriptObject = NSAppleScript(source: <#T##String#>) {
|
||||||
@ -180,3 +125,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension CustomButtonTouchBarItem {
|
||||||
|
convenience init(identifier: NSTouchBarItem.Identifier, title: String, HIDKeycode: Int) {
|
||||||
|
self.init(identifier: identifier, title: title) { _ in
|
||||||
|
HIDPostAuxKey(HIDKeycode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
convenience init(identifier: NSTouchBarItem.Identifier, title: String, key: KeyPress) {
|
||||||
|
self.init(identifier: identifier, title: title) { _ in
|
||||||
|
key.send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -34,3 +34,22 @@ extension NSTouchBarItem.Identifier {
|
|||||||
|
|
||||||
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
|
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||||
|
let tapClosure: (NSCustomTouchBarItem) -> ()
|
||||||
|
|
||||||
|
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping (NSCustomTouchBarItem) -> ()) {
|
||||||
|
self.tapClosure = callback
|
||||||
|
super.init(identifier: identifier)
|
||||||
|
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
fatalError("init(coder:) has not been implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc func didTapped() {
|
||||||
|
self.tapClosure(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user