From 132b130d6fc5db158d001fb48501e8ec7ac42600 Mon Sep 17 00:00:00 2001 From: Matt <85322+mattmassicotte@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:22:39 -0400 Subject: [PATCH] Fix non-key event modifier flag calculations --- Sources/KeyCodes/NSEvent+Key.swift | 4 ---- Tests/KeyCodesTests/NSEventTests.swift | 12 ++++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/KeyCodes/NSEvent+Key.swift b/Sources/KeyCodes/NSEvent+Key.swift index 090a9fd..17d4f64 100644 --- a/Sources/KeyCodes/NSEvent+Key.swift +++ b/Sources/KeyCodes/NSEvent+Key.swift @@ -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 diff --git a/Tests/KeyCodesTests/NSEventTests.swift b/Tests/KeyCodesTests/NSEventTests.swift index 6004122..d7a94c2 100644 --- a/Tests/KeyCodesTests/NSEventTests.swift +++ b/Tests/KeyCodesTests/NSEventTests.swift @@ -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