【问题标题】:NSEvent feeding to systemNSEvent 馈送到系统
【发布时间】:2010-10-03 22:41:20
【问题描述】:

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

【问题讨论】:

    标签: objective-c nsevent


    【解决方案1】:

    你可以使用这个 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!)
    

    【讨论】:

      【解决方案2】:

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

      从那里开始,应该不难。构造所有必要的组件,并使用NSApplication-sendEvent: 方法发送事件。

      【讨论】:

      • 你不应该真的需要一个例子(写起来很容易),但这里有一个例子:pastie.org/1199967。它假设了很多你可以/应该改变自己的信息。这取决于您使用按键的目的以及您想要实现的目标。
      • 我用一个例子来支持答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多