Improve key event handling.

This commit is contained in:
2025-01-09 12:43:31 -08:00
parent c15691a1a1
commit 75bbcdb049
4 changed files with 43 additions and 34 deletions

View File

@@ -11,6 +11,7 @@ final class EditableNSTextField: NSTextField {
NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue)
== commandKey == commandKey
{ {
// TODO: Use virtual key codes instead of characters.
switch event.charactersIgnoringModifiers! { switch event.charactersIgnoringModifiers! {
case "x": case "x":
if NSApp.sendAction(#selector(NSText.cut(_:)), if NSApp.sendAction(#selector(NSText.cut(_:)),

View File

@@ -7,6 +7,20 @@ fileprivate let logger = Logger(
category: String("Helpers") category: String("Helpers")
) )
let OSCtrl = NSEvent.ModifierFlags.control.rawValue
let OSCmd = NSEvent.ModifierFlags.command.rawValue
let OSOpt = NSEvent.ModifierFlags.option.rawValue
let OSShift = NSEvent.ModifierFlags.shift.rawValue
let OSMods = UInt(OSCtrl | OSCmd | OSOpt | OSShift)
func modsContains(keys: UInt, in modifiers: UInt) -> Bool {
return (modifiers & keys) == keys && ((modifiers ^ keys) & OSMods) == 0
}
func modsContainsNone(in modifiers: UInt) -> Bool {
return (modifiers & OSMods) == 0
}
enum ViewConstants { enum ViewConstants {
static let spacing2: CGFloat = 2 static let spacing2: CGFloat = 2
static let spacing5: CGFloat = 2 static let spacing5: CGFloat = 2

View File

@@ -1,4 +1,5 @@
import Cocoa import Cocoa
import Carbon
import OSLog import OSLog
class PopoverPanel: NSPanel { class PopoverPanel: NSPanel {
@@ -41,32 +42,27 @@ class PopoverPanel: NSPanel {
override func performKeyEquivalent(with event: NSEvent) -> Bool { override func performKeyEquivalent(with event: NSEvent) -> Bool {
let modifiers = event.modifierFlags.rawValue let modifiers = event.modifierFlags.rawValue
let command = NSEvent.ModifierFlags.command.rawValue let key = event.keyCode
let shift = NSEvent.ModifierFlags.shift.rawValue
let control = NSEvent.ModifierFlags.control.rawValue
let option = NSEvent.ModifierFlags.option.rawValue
if event.type == NSEvent.EventType.keyDown { if event.type == NSEvent.EventType.keyDown {
// Checks if flags contains a command key, if modsContains(keys: OSCmd, in: modifiers) &&
// then check if flags doesn't contain any other keys. key == kVK_ANSI_Q
if (modifiers & command) == command,
(modifiers & (control | shift | option)) == 0,
event.keyCode == 12 // Q
{ {
NSApplication.shared.terminate(self) NSApplication.shared.terminate(self)
return true return true
} else if (modifiers & command) == command, } else if modsContains(keys: OSCmd, in: modifiers) &&
(modifiers & (control | shift | option)) == 0, key == kVK_ANSI_W
event.keyCode == 13 // W
{ {
resignKey() resignKey()
return true return true
} else if (modifiers & (command & shift)) == command & shift,
(modifiers & (control | option)) == 0, } else if modsContains(keys: OSCmd | OSShift,
event.keyCode == 15 // R in: modifiers) &&
key == kVK_ANSI_R
{ {
PathManager.shared.rebuildIndex() PathManager.shared.rebuildIndex()
} else if event.keyCode == 53 { // ESC return true
} else if key == kVK_Escape {
resignKey() resignKey()
return true return true
} }

View File

@@ -1,4 +1,5 @@
import AppKit import AppKit
import Carbon
import OSLog import OSLog
class SearchViewController: NSViewController, NSTextFieldDelegate, class SearchViewController: NSViewController, NSTextFieldDelegate,
@@ -151,25 +152,20 @@ class SearchViewController: NSViewController, NSTextFieldDelegate,
{ [weak self] event in { [weak self] event in
let key = event.keyCode let key = event.keyCode
let modifiers = event.modifierFlags.rawValue let modifiers = event.modifierFlags.rawValue
let command = NSEvent.ModifierFlags.command.rawValue
let shift = NSEvent.ModifierFlags.shift.rawValue
let control = NSEvent.ModifierFlags.control.rawValue
let option = NSEvent.ModifierFlags.option.rawValue
// TODO: Implement helper functions for modifiers. // TODO: Implement helper functions for modifiers.
if let controller = self { if let controller = self {
if ((modifiers & control) == control && if modsContains(keys: OSCtrl, in: modifiers) &&
(modifiers & (command | shift | option)) == 0 && key == kVK_ANSI_P ||
key == 35) || // P modsContainsNone(in: modifiers) &&
(modifiers & (command | control | shift | option)) == 0 && key == kVK_UpArrow
(key == 126) // UP
{ {
controller.programsTableViewSelection -= 1 controller.programsTableViewSelection -= 1
} else if ((modifiers & control) == control &&
(modifiers & (command | shift | option)) == 0 && } else if modsContains(keys: OSCtrl, in: modifiers) &&
key == 45) || // N key == kVK_ANSI_N ||
(modifiers & (command | control | shift | option)) == 0 && modsContainsNone(in: modifiers) &&
(key == 125) // DOWN (key == kVK_DownArrow)
{ {
controller.programsTableViewSelection += 1 controller.programsTableViewSelection += 1
} }
@@ -322,10 +318,12 @@ class SearchViewController: NSViewController, NSTextFieldDelegate,
doCommandBy commandSelector: Selector) -> Bool doCommandBy commandSelector: Selector) -> Bool
{ {
if commandSelector == #selector(NSResponder.insertNewline(_:)) { if commandSelector == #selector(NSResponder.insertNewline(_:)) {
if programsList.count > 0 {
let program = programsList[programsTableViewSelection] let program = programsList[programsTableViewSelection]
openProgram(program) openProgram(program)
NSApp.sendAction(#selector(NSResponder.selectAll(_:)), NSApp.sendAction(#selector(NSResponder.selectAll(_:)),
to: nil, from: self) to: nil, from: self)
}
return true return true
} else if commandSelector == #selector(NSResponder.insertTab(_:)) { } else if commandSelector == #selector(NSResponder.insertTab(_:)) {
return true return true