1

I am trying to remap the caps lock key to the return key so far I did what I want, but the problem is the caps lock is switching on, so every key I press after I press caps lock will be capital. is it possible to switch caps lock key off programmatically? or even better, can I prevent it from switching on in the first place?

I tried to copy the event and send it again to turn it off, but the program entered a loop of calling myCGEventCallback again.

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 Answers0