Skip to content

Commit

Permalink
fix: 修复切换中英文按键只有左侧按钮才可切换的问题; 新增左shift, 右shift和fn键切换中英文
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Jun 30, 2022
1 parent 4443664 commit 2668b68
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
459DE98E232EB26500A3ACD1 /* CandidatesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CandidatesView.swift; sourceTree = "<group>"; };
459DE98F232EB26500A3ACD1 /* CandidatesWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CandidatesWindow.swift; sourceTree = "<group>"; };
45ACDE1A2841C52500658F46 /* Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Bridging-Header.h"; sourceTree = "<group>"; };
45D996A9253C5232001460A8 /* GeneralPane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralPane.swift; sourceTree = "<group>"; };
45D996A9253C5232001460A8 /* GeneralPane.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = GeneralPane.swift; sourceTree = "<group>"; };
45DB6EC627E5B8FE00A39925 /* ThemeConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeConfig.swift; sourceTree = "<group>"; };
45DB6EC827E609CB00A39925 /* ThemePane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemePane.swift; sourceTree = "<group>"; };
45DCE62126A31F140009FED1 /* ApplicationSettingCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationSettingCache.swift; sourceTree = "<group>"; };
Expand All @@ -108,7 +108,7 @@
63CA87FF28AC32324A6D50A5 /* Pods-TableBuilder.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TableBuilder.release.xcconfig"; path = "Target Support Files/Pods-TableBuilder/Pods-TableBuilder.release.xcconfig"; sourceTree = "<group>"; };
673A400B253D9FE70003901E /* InputSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSource.swift; sourceTree = "<group>"; };
673C416825468A4800F462A3 /* FireMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FireMenu.swift; sourceTree = "<group>"; };
673C417125468FFA00F462A3 /* ModifierKeyUpChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModifierKeyUpChecker.swift; sourceTree = "<group>"; };
673C417125468FFA00F462A3 /* ModifierKeyUpChecker.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ModifierKeyUpChecker.swift; sourceTree = "<group>"; };
673C4177254697E400F462A3 /* TipsWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TipsWindow.swift; sourceTree = "<group>"; };
677A0873254BD47D000B58D4 /* ToastWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastWindow.swift; sourceTree = "<group>"; };
67E8B511233B978E00D7CE80 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down
1 change: 1 addition & 0 deletions Fire/FireMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import AppKit
import Sparkle

extension FireInputController {
Expand Down
11 changes: 7 additions & 4 deletions Fire/Preferences/GeneralPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ struct GeneralPane: View {
}
HStack {
Picker("快捷键", selection: $toggleInputModeKey) {
Text("control").tag(NSEvent.ModifierFlags.control.rawValue)
Text("shift").tag(NSEvent.ModifierFlags.shift.rawValue)
Text("option").tag(NSEvent.ModifierFlags.option.rawValue)
Text("command").tag(NSEvent.ModifierFlags.command.rawValue)
Text("control").tag(ModifierKey.control)
Text("shift").tag(ModifierKey.shift)
Text("左shift").tag(ModifierKey.leftShift)
Text("右shift").tag(ModifierKey.rightShift)
Text("option").tag(ModifierKey.option)
Text("command").tag(ModifierKey.command)
Text("fn").tag(ModifierKey.function)
}
.disabled(disableEnMode)
Spacer(minLength: 50)
Expand Down
4 changes: 2 additions & 2 deletions Fire/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum HandlerStatus {
}

class Utils {
var toggleInputModeKeyUpChecker = ModifierKeyUpChecker(.shift)
var toggleInputModeKeyUpChecker = ModifierKeyUpChecker(Defaults[.toggleInputModeKey])

var toast: ToastWindowProtocol?

Expand All @@ -33,7 +33,7 @@ class Utils {
self.initToastWindow()
}.tieToLifetime(of: self)
Defaults.observe(.toggleInputModeKey) { (val) in
let modifier = NSEvent.ModifierFlags(rawValue: val.newValue)
let modifier = val.newValue
print("modifier: ", modifier)
self.toggleInputModeKeyUpChecker = ModifierKeyUpChecker(modifier)
}.tieToLifetime(of: self)
Expand Down
50 changes: 38 additions & 12 deletions Fire/Utils/ModifierKeyUpChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,44 @@ extension Date {
}

class ModifierKeyUpChecker {
init(_ modifier: NSEvent.ModifierFlags) {
checkModifier = modifier
init(_ modifier: ModifierKey) {
checkModifierKey = modifier
}
let checkModifier: NSEvent.ModifierFlags
var checkKeyCode: Int {
switch self.checkModifier {
let checkModifierKey: ModifierKey
private var checkModifier: NSEvent.ModifierFlags {
switch self.checkModifierKey {
case .command:
return NSEvent.ModifierFlags.command
case .control:
return NSEvent.ModifierFlags.control
case .shift:
return NSEvent.ModifierFlags.shift
case .option:
return NSEvent.ModifierFlags.option
case .function:
return NSEvent.ModifierFlags.function
default:
return NSEvent.ModifierFlags.shift
}
}
var checkKeyCode: [Int] {
switch self.checkModifierKey {
case .shift:
return kVK_Shift
return [kVK_Shift, kVK_RightShift]
case .leftShift:
return [kVK_Shift]
case .rightShift:
return [kVK_RightShift]
case .command:
return kVK_Command
return [kVK_Command, kVK_RightCommand]
case .control:
return kVK_Control
return [kVK_Control, kVK_RightControl]
case .option:
return kVK_Option
return [kVK_Option, kVK_RightOption]
case .function:
return [kVK_Function]
default:
return 0
return []
}
}

Expand All @@ -53,9 +75,13 @@ class ModifierKeyUpChecker {
}

private func checkModifierKeyDown(event: NSEvent) -> Bool {
if event.type == .flagsChanged
let isLeftShift = event.modifierFlags.rawValue & UInt(NX_DEVICELSHIFTKEYMASK) != 0
let isRightShift = event.modifierFlags.rawValue & UInt(NX_DEVICERSHIFTKEYMASK) != 0
print("isLeftShift: \(isLeftShift), isRightShift: \(isRightShift)")
let isKeyDown = event.type == .flagsChanged
&& event.modifierFlags.intersection(.deviceIndependentFlagsMask) == checkModifier
&& event.keyCode == checkKeyCode {
&& checkKeyCode.contains(Int(event.keyCode))
if isKeyDown {
// modifier keydown event
lastTime = Date()
} else {
Expand Down
13 changes: 11 additions & 2 deletions Fire/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ enum InputModeTipWindowType: Int, Decodable, Encodable {
case none
}

enum ModifierKey: Codable {
case shift
case leftShift
case rightShift
case control
case command
case option
case function
}

class ApplicationSettingItem: ObservableObject, Codable, Identifiable {
// let identifier: String = ""

Expand Down Expand Up @@ -78,8 +88,7 @@ extension Defaults.Keys {
// 禁止切换英文
static let disableEnMode = Key<Bool>("diableEnMode", default: false)
// 切换英文模式的按键
static let toggleInputModeKey = Key<NSEvent.ModifierFlags.RawValue>("toggleInputModeKey",
default: NSEvent.ModifierFlags.shift.rawValue)
static let toggleInputModeKey = Key<ModifierKey>("toggleInputModeKey", default: ModifierKey.shift)
// 中英文切换提示弹窗位置
static let inputModeTipWindowType = Key<InputModeTipWindowType>(
"inputModeTipWindowType",
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ target 'Fire' do
# use_frameworks!

# Pods for Fire
pod 'Sparkle'
pod 'Sparkle', '~> 1.26.0'

# Pods for lint
pod 'SwiftLint'
Expand Down
12 changes: 6 additions & 6 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ PODS:
- Defaults (4.2.2)
- KeychainSwift (20.0.0)
- NanoID (1.0.3)
- Preferences (2.2.0)
- Preferences (2.5.0)
- Sparkle (1.26.0)
- SQLCipher (4.5.1):
- SQLCipher/standard (= 4.5.1)
- SQLCipher/common (4.5.1)
- SQLCipher/standard (4.5.1):
- SQLCipher/common
- SwiftLint (0.46.5)
- SwiftLint (0.47.1)

DEPENDENCIES:
- Defaults
- KeychainSwift (~> 20.0)
- NanoID (~> 1.0)
- Preferences
- Sparkle
- Sparkle (~> 1.26.0)
- SQLCipher (~> 4.5)
- SwiftLint

Expand All @@ -34,11 +34,11 @@ SPEC CHECKSUMS:
Defaults: d785e56c0fb8890dc40351603f05c8e1df1bdd45
KeychainSwift: 0ce6a4d13f7228054d1a71bb1b500448fb2ab837
NanoID: 060d290630f851ddd8fb0169ebfd7d44bd8670bd
Preferences: 4894cc28df3557336b0b72440c893d95203d3e33
Preferences: a78dbe5c407afb66408a929d4708f8fd82abad85
Sparkle: d56d028f4b0c123577825f5d1004f6140d77f90e
SQLCipher: 712e8416685e8e575b9b0706ffee71678b2fdcf8
SwiftLint: 2c16e8fa99dac2e440dc0c70ea74d2532048f6cf
SwiftLint: f80f1be7fa96d30e0aa68e58d45d4ea1ccaac519

PODFILE CHECKSUM: 472d93c52c91d268ea3db355b7bb91cb3b36a655
PODFILE CHECKSUM: 3675cd5596e2426de3e0330cb088ec63b9943d01

COCOAPODS: 1.11.3

0 comments on commit 2668b68

Please sign in to comment.