Pergunta

Alguém pode mostrar um exemplo sobre como criar um NSEVEV que simula o retorno pressionado?

Foi útil?

Solução

Você deve dar uma olhada no Referência da classe NSEVENT, e mais especificamente, -keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:.

A partir daí, não deve ser difícil. Construa todos os componentes necessários e envie o evento usando NSApplication's -sendEvent: método.

Outras dicas

Você pode usar esse código SWIFT para criar um evento simples de teclado:

    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!)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top