使用Swift模拟按键 [英] Simulate keypress using Swift

查看:1011
本文介绍了使用Swift模拟按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在OSX中模拟击键的方法。我找到了另一个解决方案(针对系统范围的热键模拟按键)使用Objective-C,但我需要做它与Swift。我如何适应CGEventCreateKeyboardEvent?

I'm searching for a way to simulate keystrokes in OSX. I found another solution (Simulate keypress for system wide hotkeys) using Objective-C, but i need to do it with Swift. How can i adapt CGEventCreateKeyboardEvent?

推荐答案

链接的答案的代码可以很容易地转换为Swift代码,

The code on that linked answer is fairly readily convertible to Swift code, however there are a handful of gotchas you will need to take care of along the way:

CGEventSourceCreate 需要一个 CGEventSourceStateID ,它是 UInt32 的类型列表,但诸如 kCGEventSourceStateHIDSystemState 定义为 Int ,所以你必须投入它们ie CGEventSourceStateID(kCGEventSourceStateHIDSystemState)。同样, CGEventFlags

CGEventSourceCreate takes a CGEventSourceStateID, which is a typealiase for a UInt32, but the constants such as kCGEventSourceStateHIDSystemState are defined as Int, so you’ll have to cast them i.e. CGEventSourceStateID(kCGEventSourceStateHIDSystemState). Likewise with CGEventFlags.

CGEventSourceCreate $ c> CGEventCreateKeyboardEvent 返回未管理< CGEventSource> (或非托管< CGEvent> ) 。 Core Graphics的自动生成的Swift API不知道返回的对象是否需要由你释放,所以你需要检查这些调用的API文档,然后使用 takeRetainedValue() takeUnretainedValue(),以将它们转换为您想使用的底层类型。

CGEventSourceCreate and CGEventCreateKeyboardEvent return an Unmanaged<CGEventSource> (or Unmanaged<CGEvent>). The auto-generated Swift API for Core Graphics doesn’t know whether the returned objects need to be released by you or not so you need to check the API docs for these calls and then use takeRetainedValue() or takeUnretainedValue() accordingly on the returned value, to convert them into the underlying type you want to work with.

最后,他们返回隐含的未包装的可选项,所以你需要决定是否要检查nils,还是只要遇到运行时爆炸的兴奋,如果他们返回一个。

Finally, they return implicitly unwrapped optionals, so you’ll need to decide if you want to check for nils or just live with the excitement of runtime explosions if they ever return one.

鉴于所有这一切都很简单,在这个答案证明压缩Cmd-Space到Swift,我只是尝试粘贴到一个临时应用程序,它工作正常:

Given all that it’s pretty simple to turn the Objective-C in that answer demonstrating pressing Cmd-Space to Swift, I just tried pasting this into a scratch app and it worked fine:

(虽然我没有检查API文档是否保留是正确的事情做)。

(though I haven't checked the API docs for whether the retain is the correct thing to do or not)

let src = CGEventSourceCreate(CGEventSourceStateID(kCGEventSourceStateHIDSystemState)).takeRetainedValue()

let cmdd = CGEventCreateKeyboardEvent(src, 0x38, true).takeRetainedValue()
let cmdu = CGEventCreateKeyboardEvent(src, 0x38, false).takeRetainedValue()
let spcd = CGEventCreateKeyboardEvent(src, 0x31, true).takeRetainedValue()
let spcu = CGEventCreateKeyboardEvent(src, 0x31, false).takeRetainedValue()

CGEventSetFlags(spcd, CGEventFlags(kCGEventFlagMaskCommand));
CGEventSetFlags(spcd, CGEventFlags(kCGEventFlagMaskCommand));

let loc = CGEventTapLocation(kCGHIDEventTap)

CGEventPost(loc, cmdd)
CGEventPost(loc, spcd)
CGEventPost(loc, spcu)
CGEventPost(loc, cmdu)

这篇关于使用Swift模拟按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆