Simulate/Toggle CAPS LOCK programatically in OS X

后端 未结 2 1387
自闭症患者
自闭症患者 2020-12-11 10:53

I have seen many post on this topic. But haven\'t found a clear answer anywhere.

Is there a way to toggle CAPS LOCK in Objective-C or C code? I am not looking for a

2条回答
  •  天涯浪人
    2020-12-11 11:52

    var ioConnect: io_connect_t = .init(0)
    let ioService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching(kIOHIDSystemClass))
    IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect)
    
    var modifierLockState = false
    IOHIDGetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), &modifierLockState) 
    
    modifierLockState.toggle()
    IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), modifierLockState)
    
    IOServiceClose(ioConnect)
    

提交回复
热议问题