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)
== commandKey
{
// TODO: Use virtual key codes instead of characters.
switch event.charactersIgnoringModifiers! {
case "x":
if NSApp.sendAction(#selector(NSText.cut(_:)),

View File

@@ -7,6 +7,20 @@ fileprivate let logger = Logger(
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 {
static let spacing2: CGFloat = 2
static let spacing5: CGFloat = 2

View File

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

View File

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