Question

Can someone show an example on how to create an NSEvent that simulates pressing return?

Was it helpful?

Solution

You should take a look at the NSEvent Class Reference, and more specifically, -keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:.

From there, it shouldn't be difficult. Construct all the necessary components, and send the event using NSApplication's -sendEvent: method.

OTHER TIPS

You can use this Swift-Code for creating a simple Keyboard-Event:

    let ThePoint = CGPoint(x:0,y:0)
    let theEventType: NSEventType = NSEventType(rawValue: 10)! // = KeyDown
    let theModifierFlags: NSEventModifierFlags = NSEventModifierFlags(rawValue: 0)
    var event = NSEvent.keyEventWithType(theEventType, location: ThePoint, modifierFlags: theModifierFlags, timestamp: 0.0, windowNumber: 0, context: nil, characters: "\n", charactersIgnoringModifiers: "", isARepeat: false, keyCode: 0)
    NSApplication.sharedApplication().sendEvent(event!)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top