Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping Control key modifier combination #152

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions AKPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AKPlugin: NSObject, Plugin {
}

private var modifierFlag: UInt = 0
func setupKeyboard(keyboard: @escaping(UInt16, Bool, Bool) -> Bool,
func setupKeyboard(keyboard: @escaping(UInt16, Bool, Bool, Bool) -> Bool,
swapMode: @escaping() -> Bool) {
func checkCmd(modifier: NSEvent.ModifierFlags) -> Bool {
if modifier.contains(.command) {
Expand All @@ -81,7 +81,8 @@ class AKPlugin: NSObject, Plugin {
if checkCmd(modifier: event.modifierFlags) {
return event
}
let consumed = keyboard(event.keyCode, true, event.isARepeat)
let consumed = keyboard(event.keyCode, true, event.isARepeat,
event.modifierFlags.contains(.control))
if consumed {
return nil
}
Expand All @@ -91,7 +92,8 @@ class AKPlugin: NSObject, Plugin {
if checkCmd(modifier: event.modifierFlags) {
return event
}
let consumed = keyboard(event.keyCode, false, false)
let consumed = keyboard(event.keyCode, false, false,
event.modifierFlags.contains(.control))
if consumed {
return nil
}
Expand All @@ -104,13 +106,15 @@ class AKPlugin: NSObject, Plugin {
let pressed = self.modifierFlag < event.modifierFlags.rawValue
let changed = self.modifierFlag ^ event.modifierFlags.rawValue
self.modifierFlag = event.modifierFlags.rawValue
if pressed && NSEvent.ModifierFlags(rawValue: changed).contains(.option) {
let changedFlags = NSEvent.ModifierFlags(rawValue: changed)
if pressed && changedFlags.contains(.option) {
if swapMode() {
return nil
}
return event
}
let consumed = keyboard(event.keyCode, pressed, false)
let consumed = keyboard(event.keyCode, pressed, false,
event.modifierFlags.contains(.control))
if consumed {
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions PlayTools/Controls/Frontend/ControlMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public class ControlMode: Equatable {
}
}

AKInterface.shared!.setupKeyboard(keyboard: { keycode, pressed, isRepeat in
self.keyboardAdapter.handleKey(keycode: keycode, pressed: pressed, isRepeat: isRepeat)},
AKInterface.shared!.setupKeyboard(keyboard: { keycode, pressed, isRepeat, ctrlModified in
self.keyboardAdapter.handleKey(keycode: keycode, pressed: pressed,
isRepeat: isRepeat, ctrlModified: ctrlModified)},
swapMode: ModeAutomaton.onOption)

if PlaySettings.shared.enableScrollWheel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public class EditorKeyboardEventAdapter: KeyboardEventAdapter {
.rightGUI,
.leftAlt,
.rightAlt,
.leftControl,
.rightControl,
.printScreen
]

public func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool) -> Bool {
public func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool, ctrlModified: Bool) -> Bool {
if AKInterface.shared!.cmdPressed || !pressed || isRepeat {
return false
}
Expand All @@ -31,7 +33,16 @@ public class EditorKeyboardEventAdapter: KeyboardEventAdapter {
// Toast.showHint(title: "Invalid Key", text: ["This key is intentionally forbidden. Keyname: \(name)"])
return false
}
EditorController.shared.setKey(rawValue)

if ctrlModified {
if let name = KeyCodeNames.virtualCodes[keycode] {
// Setkey by name does not work with all kinds of mapping
EditorController.shared.setKey("⌃" + name)
}
} else {
EditorController.shared.setKey(rawValue)
}

return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,41 @@ import Foundation
// Keyboard events handler when keyboard mapping is on

public class TouchscreenKeyboardEventAdapter: KeyboardEventAdapter {
public func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool) -> Bool {
private var modifiedKeys: [UInt16] = []
public func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool, ctrlModified: Bool) -> Bool {

if isRepeat {
// eat, eat, eat!
return true
}

let name = KeyCodeNames.virtualCodes[keycode] ?? "Btn"
var name = KeyCodeNames.virtualCodes[keycode] ?? "Btn"

if keycode == 59 || keycode == 62 {
// if this is Control
if !pressed {
// just in case <pressed=true, ctrl=true> followed by <pressed=false, ctrl=false>
// release modified keys when ctrl release
while let key = modifiedKeys.first {
_ = handleKey(
keycode: key,
pressed: false, isRepeat: false,
ctrlModified: true)
}
}
} else if ctrlModified && pressed {
name = "⌃" + name // "⌃" is not "^"
// Record pressed key
modifiedKeys.append(keycode)

} else if !pressed && modifiedKeys.contains(keycode) {
// just in case <pressed=true, ctrl=false> followed by <pressed=false, ctrl=true>
// does not modify on release if not recorded
name = "⌃" + name // "⌃" is not "^"
// unrecord released key
modifiedKeys.removeAll(where: {code in code == keycode})
}

return ActionDispatcher.dispatch(key: name, pressed: pressed)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
// Keyboard events handler when keyboard mapping is off

public class TransparentKeyboardEventAdapter: KeyboardEventAdapter {
public func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool) -> Bool {
public func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool, ctrlModified: Bool) -> Bool {
// explicitly eat repeated Enter key
isRepeat && keycode == 36
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import Foundation
// All keyboard events under any mode

public protocol KeyboardEventAdapter: EventAdapter {
func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool) -> Bool
func handleKey(keycode: UInt16, pressed: Bool, isRepeat: Bool, ctrlModified: Bool) -> Bool
}
2 changes: 1 addition & 1 deletion Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol Plugin: NSObjectProtocol {
func warpCursor()
func unhideCursor()
func terminateApplication()
func setupKeyboard(keyboard: @escaping(UInt16, Bool, Bool) -> Bool,
func setupKeyboard(keyboard: @escaping(UInt16, Bool, Bool, Bool) -> Bool,
swapMode: @escaping() -> Bool)
func setupMouseMoved(_ mouseMoved: @escaping(CGFloat, CGFloat) -> Bool)
func setupMouseButton(left: Bool, right: Bool, _ consumed: @escaping(Int, Bool) -> Bool)
Expand Down