From bee7b995bc5f9037e075f2cd731c80a383303ed8 Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Tue, 6 Apr 2021 15:23:14 +0200 Subject: [PATCH 1/3] rename non-localized keyEquivalent to qwertyKeyLabel (temp) --- Lib/Magnet/KeyCombo.swift | 2 +- Lib/MagnetTests/KeyComboTests.swift | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Lib/Magnet/KeyCombo.swift b/Lib/Magnet/KeyCombo.swift index 84496c7..441b37b 100644 --- a/Lib/Magnet/KeyCombo.swift +++ b/Lib/Magnet/KeyCombo.swift @@ -26,7 +26,7 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable { guard !doubledModifiers else { return "" } return Sauce.shared.character(by: Int(Sauce.shared.keyCode(by: key)), carbonModifiers: modifiers) ?? "" } - public var keyEquivalent: String { + public var qwertyKeyLabel: String { guard !doubledModifiers else { return "" } let keyCode = Int(Sauce.shared.keyCode(by: key)) guard key.isAlphabet else { return Sauce.shared.character(by: keyCode, cocoaModifiers: []) ?? "" } diff --git a/Lib/MagnetTests/KeyComboTests.swift b/Lib/MagnetTests/KeyComboTests.swift index 2c76a70..f74b044 100644 --- a/Lib/MagnetTests/KeyComboTests.swift +++ b/Lib/MagnetTests/KeyComboTests.swift @@ -93,31 +93,31 @@ final class KeyComboTests: XCTestCase { var keyCombo: KeyCombo? // Command + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.command]) - XCTAssertEqual(keyCombo?.keyEquivalent, "a") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "a") // Shift + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.shift]) - XCTAssertEqual(keyCombo?.keyEquivalent, "A") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "A") // Option + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.option]) - XCTAssertEqual(keyCombo?.keyEquivalent, "a") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "a") // Option + Shift + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.option, .shift]) - XCTAssertEqual(keyCombo?.keyEquivalent, "A") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "A") // Option + Shift + 1 keyCombo = KeyCombo(key: .one, cocoaModifiers: [.option, .shift]) - XCTAssertEqual(keyCombo?.keyEquivalent, "1") // Option + Shift + Keypad 1 + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "1") keyCombo = KeyCombo(key: .keypadOne, cocoaModifiers: [.option, .shift]) - XCTAssertEqual(keyCombo?.keyEquivalent, "1") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "1") // Option + ; keyCombo = KeyCombo(key: .semicolon, cocoaModifiers: [.option]) - XCTAssertEqual(keyCombo?.keyEquivalent, ";") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, ";") // Shift + F1 keyCombo = KeyCombo(key: .f1, cocoaModifiers: [.shift]) - XCTAssertEqual(keyCombo?.keyEquivalent, "F1") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "F1") // Option double tap keyCombo = KeyCombo(doubledCocoaModifiers: .option) - XCTAssertEqual(keyCombo?.keyEquivalent, "") + XCTAssertEqual(keyCombo?.qwertyKeyLabel, "") } func testKeyEquivalentModifierMaskString() { From c7beaa3f889608999218f50fee682eb14338fc05 Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Tue, 6 Apr 2021 15:30:18 +0200 Subject: [PATCH 2/3] add keyEquivalent as NSMenuItem compatible value --- Lib/Magnet/KeyCombo.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/Magnet/KeyCombo.swift b/Lib/Magnet/KeyCombo.swift index 441b37b..f875bca 100644 --- a/Lib/Magnet/KeyCombo.swift +++ b/Lib/Magnet/KeyCombo.swift @@ -26,6 +26,8 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable { guard !doubledModifiers else { return "" } return Sauce.shared.character(by: Int(Sauce.shared.keyCode(by: key)), carbonModifiers: modifiers) ?? "" } + /// The QWERTY key equivalent of this key combo's key code. Using a different keyboard layout, this + /// will not match the typed character. Use `keyEquivalent` instead. public var qwertyKeyLabel: String { guard !doubledModifiers else { return "" } let keyCode = Int(Sauce.shared.keyCode(by: key)) @@ -33,6 +35,16 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable { let modifiers = self.modifiers.convertSupportCocoaModifiers().filterNotShiftModifiers() return Sauce.shared.character(by: keyCode, cocoaModifiers: modifiers) ?? "" } + /// Character that would be typed in a text field when the user presses the key. + /// Use this for `NSMenuItem`'s `keyEquivalent` which expects the character and not the QWERTY key code. + public var keyEquivalent: String { + guard !doubledModifiers else { return "" } + // Not calling `Sauce.shared.keyCode` skips translation of the current layout + let keyCode = Int(key.QWERTYKeyCode) + guard key.isAlphabet else { return Sauce.shared.character(by: keyCode, cocoaModifiers: []) ?? "" } + let modifiers = self.modifiers.convertSupportCocoaModifiers().filterNotShiftModifiers() + return Sauce.shared.character(by: keyCode, cocoaModifiers: modifiers) ?? "" + } public var keyEquivalentModifierMask: NSEvent.ModifierFlags { return modifiers.convertSupportCocoaModifiers() } From a8415b4488b0794a016dcb9249175112470ccc34 Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Tue, 6 Apr 2021 15:38:17 +0200 Subject: [PATCH 3/3] upcase QWERTYKeyLabel --- Lib/Magnet/KeyCombo.swift | 10 +++++----- Lib/MagnetTests/KeyComboTests.swift | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Lib/Magnet/KeyCombo.swift b/Lib/Magnet/KeyCombo.swift index f875bca..809577c 100644 --- a/Lib/Magnet/KeyCombo.swift +++ b/Lib/Magnet/KeyCombo.swift @@ -22,19 +22,19 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable { guard !doubledModifiers else { return 0 } return Int(key.QWERTYKeyCode) } - public var characters: String { - guard !doubledModifiers else { return "" } - return Sauce.shared.character(by: Int(Sauce.shared.keyCode(by: key)), carbonModifiers: modifiers) ?? "" - } /// The QWERTY key equivalent of this key combo's key code. Using a different keyboard layout, this /// will not match the typed character. Use `keyEquivalent` instead. - public var qwertyKeyLabel: String { + public var QWERTYKeyLabel: String { guard !doubledModifiers else { return "" } let keyCode = Int(Sauce.shared.keyCode(by: key)) guard key.isAlphabet else { return Sauce.shared.character(by: keyCode, cocoaModifiers: []) ?? "" } let modifiers = self.modifiers.convertSupportCocoaModifiers().filterNotShiftModifiers() return Sauce.shared.character(by: keyCode, cocoaModifiers: modifiers) ?? "" } + public var characters: String { + guard !doubledModifiers else { return "" } + return Sauce.shared.character(by: Int(Sauce.shared.keyCode(by: key)), carbonModifiers: modifiers) ?? "" + } /// Character that would be typed in a text field when the user presses the key. /// Use this for `NSMenuItem`'s `keyEquivalent` which expects the character and not the QWERTY key code. public var keyEquivalent: String { diff --git a/Lib/MagnetTests/KeyComboTests.swift b/Lib/MagnetTests/KeyComboTests.swift index f74b044..c1c6677 100644 --- a/Lib/MagnetTests/KeyComboTests.swift +++ b/Lib/MagnetTests/KeyComboTests.swift @@ -93,31 +93,31 @@ final class KeyComboTests: XCTestCase { var keyCombo: KeyCombo? // Command + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.command]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "a") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "a") // Shift + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.shift]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "A") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "A") // Option + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.option]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "a") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "a") // Option + Shift + a keyCombo = KeyCombo(key: .a, cocoaModifiers: [.option, .shift]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "A") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "A") // Option + Shift + 1 keyCombo = KeyCombo(key: .one, cocoaModifiers: [.option, .shift]) // Option + Shift + Keypad 1 - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "1") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "1") keyCombo = KeyCombo(key: .keypadOne, cocoaModifiers: [.option, .shift]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "1") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "1") // Option + ; keyCombo = KeyCombo(key: .semicolon, cocoaModifiers: [.option]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, ";") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, ";") // Shift + F1 keyCombo = KeyCombo(key: .f1, cocoaModifiers: [.shift]) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "F1") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "F1") // Option double tap keyCombo = KeyCombo(doubledCocoaModifiers: .option) - XCTAssertEqual(keyCombo?.qwertyKeyLabel, "") + XCTAssertEqual(keyCombo?.QWERTYKeyLabel, "") } func testKeyEquivalentModifierMaskString() {