1

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

4

2 回答 2

2

你可以使用这个 Swift 代码来创建一个简单的键盘事件:

    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!)
于 2014-11-26T21:12:38.433 回答
0

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.

于 2010-10-03T23:36:39.323 回答