Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
44 changes: 33 additions & 11 deletions PlayerUI/Views/PUIPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1140,15 +1140,37 @@ public final class PUIPlayerView: NSView {

private var keyDownEventMonitor: Any?

private enum KeyCommands: UInt16 {
case spaceBar = 49
case leftArrow = 123
case rightArrow = 124
case minus = 27
case plus = 24
case j = 38
case k = 40
case l = 37
private enum KeyCommands {
case spaceBar
case leftArrow
case rightArrow
case minus
case plus
case j
case k
case l

static func fromEvent(_ event: NSEvent) -> KeyCommands? {
switch event.keyCode {
case 123: return .leftArrow
case 124: return .rightArrow
default: break
}

guard let character = event.charactersIgnoringModifiers else {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading through the docs, keyCode and charactersIgnoringModifiers both say they they can raise exceptions if the event is not a key event.

addLocalMonitorForEvents(matching: .keyDown) should ensure it's always a key event, but this function could use a guard for the type of event for thoroughness.

To increase the accuracy of this function, we could also ensure no modifiers are pressed as well via event.modifierFlags.isEmpty.

return nil
}

switch character {
case " ": return .spaceBar
case "-": return .minus
case "+": return .plus
case "j": return .j
case "k": return .k
case "l": return .l
default: return nil
}
}
}

public var isEnabled = true {
Expand All @@ -1165,8 +1187,8 @@ public final class PUIPlayerView: NSView {

keyDownEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [unowned self] event in
guard self.isEnabled else { return event }

guard let command = KeyCommands(rawValue: event.keyCode) else {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: trailing space on empty line

guard let command = KeyCommands.fromEvent(event) else {
return event
}

Expand Down