NSEvent供给系统

时间:2010-10-03 22:41:21

标签: objective-c nsevent

有人能举例说明如何创建一个模拟按下返回的NSEvent吗?

2 个答案:

答案 0 :(得分:1)

您可以使用此Swift-Code创建简单的键盘事件:

    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!)

答案 1 :(得分:0)

您应该查看NSEvent Class Reference,更具体地说,-keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:

从那里开始,应该不难。构建所有必要的组件,并使用NSApplication的{​​{1}}方法发送事件。