题
有人可以显示如何创建一个NSEvent模拟按回车键的例子吗?
解决方案
您应该看看的 NSEvent类参考时,并且更具体地,-keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:
。
从那里,它不应该是困难的。构建所有必要的元件,以及使用NSApplication
的-sendEvent:
方法发送该事件。
其他提示
您可以使用此斯威夫特代码创建一个简单的键盘 - 事件:
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!)
不隶属于 StackOverflow