以编程方式关闭大写锁定macOS

时间:2019-02-09 00:45:32

标签: swift macos

到目前为止,我已经按照自己的意愿尝试将大写锁定键重新映射为返回键,但是问题是大写锁定处于打开状态,因此按大写锁定后我按的每个键都是大写字母。是否可以通过编程方式关闭大写锁定键?甚至更好,我是否可以阻止它首先启动?

我试图复制事件并再次发送以将其关闭,但程序进入了再次调用myCGEventCallback的循环。

let eventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue) | (1 << (CGEventType.flagsChanged.rawValue))
let eventTap = CGEvent.tapCreate(tap: CGEventTapLocation.cghidEventTap,
    place: CGEventTapPlacement.headInsertEventTap,
    options: CGEventTapOptions.defaultTap,
    eventsOfInterest: CGEventMask(eventMask),
    callback: myCGEventCallback,
    userInfo: nil);do {
        print("failed to create eventTap")
    };

func myCGEventCallback(proxy : CGEventTapProxy, type : CGEventType, event : 
CGEvent, refcon : UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? 
{
    var keyCode = event.getIntegerValueField(.keyboardEventKeycode);
    if keyCode == 0x39 //caps lock
    {
        event.setIntegerValueField(.keyboardEventKeycode, value: 0x24)  // return       
    }
    return Unmanaged.passRetained(event)
}

0 个答案:

没有答案