Skip to content

Commit

Permalink
Fix non-key event modifier flag calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Jul 12, 2024
1 parent 4c4d7d8 commit 132b130
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 0 additions & 4 deletions Sources/KeyCodes/NSEvent+Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ extension NSEvent {
///
/// This value can be a subset of the results of `deviceIndependentOnly`. There are flag values that `NSEvent` can include which are not representable by the `KeyModifierFlags` set.
public var keyModifierFlags: KeyModifierFlags? {
guard type == .keyDown || type == .keyUp else {
return nil
}

var flags = KeyModifierFlags(rawValue: 0)

let deviceIndependentFlags = modifierFlags.deviceIndependentOnly
Expand Down
12 changes: 12 additions & 0 deletions Tests/KeyCodesTests/NSEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@ final class NSEventTests: XCTestCase {

XCTAssertEqual(event.keyModifierFlags, [.command, .numericPad])
}

func testDeviceIndependentOnlyFromMouseDown() throws {
let eventRef = try XCTUnwrap(CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: CGPoint(), mouseButton: .left))
eventRef.flags = [.maskCommand]

let event = try XCTUnwrap(NSEvent(cgEvent: eventRef))

XCTAssertEqual(event.modifierFlags, [.command])
XCTAssertEqual(event.modifierFlags.deviceIndependentOnly, [.command])

XCTAssertEqual(event.keyModifierFlags, [.command])
}
}
#endif

0 comments on commit 132b130

Please sign in to comment.