Repo // Fix the capslock state detection.

This commit is contained in:
ShikiSuen 2023-10-30 10:57:49 +08:00
parent c943f4ec52
commit 65cf83d902
2 changed files with 12 additions and 15 deletions

View File

@ -7,13 +7,7 @@ import CapsLockToggler
public enum CapsLockToggler { public enum CapsLockToggler {
public static func toggle() { public static func toggle() {
let thePort: mach_port_t = { let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass))
if #available(macOS 12.0, *) {
return kIOMainPortDefault
}
return kIOMasterPortDefault
}()
let ioService: io_service_t = IOServiceGetMatchingService(thePort, IOServiceMatching(kIOHIDSystemClass))
var ioConnect: io_connect_t = 0 var ioConnect: io_connect_t = 0
IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect) IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect)
var state = false var state = false
@ -23,14 +17,17 @@ public enum CapsLockToggler {
IOServiceClose(ioConnect) IOServiceClose(ioConnect)
} }
public static func turnOff() { public static var isOn: Bool {
let thePort: mach_port_t = { let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass))
if #available(macOS 12.0, *) { var ioConnect: io_connect_t = 0
return kIOMainPortDefault IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect)
var state = false
IOHIDGetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), &state)
return state
} }
return kIOMasterPortDefault
}() public static func turnOff() {
let ioService: io_service_t = IOServiceGetMatchingService(thePort, IOServiceMatching(kIOHIDSystemClass)) let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass))
var ioConnect: io_connect_t = 0 var ioConnect: io_connect_t = 0
IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect) IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect)
IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), false) IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), false)

View File

@ -81,7 +81,7 @@ public extension SessionCtl {
if #available(macOS 12, *) { if #available(macOS 12, *) {
if event.type == .flagsChanged, event.keyCode == KeyCode.kCapsLock.rawValue { if event.type == .flagsChanged, event.keyCode == KeyCode.kCapsLock.rawValue {
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
let isCapsLockTurnedOn = Self.isCapsLocked let isCapsLockTurnedOn = CapsLockToggler.isOn
if PrefMgr.shared.shiftEisuToggleOffTogetherWithCapsLock, !isCapsLockTurnedOn, self?.isASCIIMode ?? false { if PrefMgr.shared.shiftEisuToggleOffTogetherWithCapsLock, !isCapsLockTurnedOn, self?.isASCIIMode ?? false {
self?.isASCIIMode.toggle() self?.isASCIIMode.toggle()
} }