Skip to content

Commit

Permalink
fix cmd+Q cmd+K etc. usability under key-mapped mode
Browse files Browse the repository at this point in the history
  • Loading branch information
XuYicong committed Aug 12, 2022
1 parent 5132e89 commit 0e902a8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion PlayTools/Controls/PlayAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ButtonAction : Action {
if let keyboard = GCKeyboard.coalesced?.keyboardInput {
if !PlayMice.shared.setMiceButtons(keyid, action: self){
keyboard.button(forKeyCode: key)?.pressedChangedHandler = { (key, keyCode, pressed) in
if !mode.visible {
if !mode.visible && !PlayInput.cmdPressed(){
self.update(pressed: pressed)
}
}
Expand Down
40 changes: 30 additions & 10 deletions PlayTools/Controls/PlayInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ final class PlayInput: NSObject {

var timeoutForBind = true

static private var lCmdPressed = false
static private var rCmdPressed = false

func invalidate(){
MacroController.shared.stopReplaying()
PlayMice.shared.stop()
Expand All @@ -33,14 +36,22 @@ final class PlayInput: NSObject {
}
if let keyboard = GCKeyboard.coalesced?.keyboardInput {
keyboard.keyChangedHandler = { (input, _, keyCode, pressed) in
if EditorController.shared.isReadyToBeOpened && !mode.visible && !editor.editorMode && keyCode == GCKeyCode.keyK && ( keyboard.button(forKeyCode: GCKeyCode(rawValue: 227))!.isPressed || keyboard.button(forKeyCode: GCKeyCode(rawValue: 231))!.isPressed ){
mode.show(true)
EditorController.shared.switchMode()
} else if editor.editorMode && !PlayInput.FORBIDDEN.contains(keyCode) && self.isSafeToBind(keyboard){
if editor.editorMode
&& !PlayInput.cmdPressed()
&& !PlayInput.FORBIDDEN.contains(keyCode)
&& self.isSafeToBind(keyboard){

EditorController.shared.setKeyCode(keyCode.rawValue)
}

}
keyboard.button(forKeyCode: GCKeyCode(rawValue: 227))?.pressedChangedHandler = {(key, keyCode, pressed) in
PlayInput.lCmdPressed = pressed
}
keyboard.button(forKeyCode: GCKeyCode(rawValue: 231))?.pressedChangedHandler = {(key, keyCode, pressed) in
PlayInput.rCmdPressed = pressed
}

keyboard.button(forKeyCode: .leftAlt)?.pressedChangedHandler = { (key, keyCode, pressed) in
self.swapMode(pressed)
}
Expand All @@ -50,6 +61,11 @@ final class PlayInput: NSObject {
}
}

static public func cmdPressed() -> Bool {
// return keyboard.button(forKeyCode: GCKeyCode(rawValue: 227))!.isPressed || keyboard.button(forKeyCode: GCKeyCode(rawValue: 231))!.isPressed
return lCmdPressed || rCmdPressed
}

private func isSafeToBind(_ input : GCKeyboardInput) -> Bool {
var result = true
for f in PlayInput.FORBIDDEN {
Expand Down Expand Up @@ -110,15 +126,19 @@ final class PlayInput: NSObject {
}

setup()
fixBeepSound()
// fix beep sound
eliminateRedundantKeyPressEvents()
}

private func fixBeepSound() {
Dynamic.NSEvent.addLocalMonitorForEventsMatchingMask(1024, handler: { event in
if !mode.visible {
return nil
private func eliminateRedundantKeyPressEvents() {
// dont know how to dynamically get it here
let NSEventMaskKeyDown: UInt64 = 1024
Dynamic.NSEvent.addLocalMonitorForEventsMatchingMask( NSEventMaskKeyDown, handler: { event in
if (mode.visible && !EditorController.shared.editorMode) || PlayInput.cmdPressed() {
return event
}
return event
// Toast.showOver(msg: "mask: \(NSEventMaskKeyDown)")
return nil
} as ResponseBlock)
}

Expand Down
12 changes: 6 additions & 6 deletions PlayTools/Keymap/EditorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class EditorController : NSObject {

static let shared = EditorController()

let lock = NSLock()

var focusedControl : ControlModel? = nil

var controls : Array<ControlModel> = []
Expand All @@ -32,31 +34,29 @@ final class EditorController : NSObject {
}
}

var isReadyToBeOpened = true

public func switchMode(){
lock.lock()
if EditorController.shared.editorMode {
Toast.showOver(msg: "Keymapping saved")
} else{
Toast.showOver(msg: "Click to start keymmaping edit")
}

if editorMode {
KeymapHolder.shared.hide()
saveButtons()
view.removeFromSuperview()
editorMode = false
mode.show(false)
PlayCover.delay(0.1) {
self.isReadyToBeOpened = true
}
focusedControl = nil
} else{
mode.show(true)
editorMode = true
showButtons()
screen.window?.addSubview(view)
view.becomeFirstResponder()
self.isReadyToBeOpened = false
}
lock.unlock()
}

var editorMode : Bool {
Expand Down

0 comments on commit 0e902a8

Please sign in to comment.