使用 Swift 模拟按键 [英] Simulate keypress using 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
,所以你必须转换它们,即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
和 CGEventCreateKeyboardEvent
返回一个 Unmanaged
(或 Unmanaged
).用于 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.
最后,它们返回隐式解包的可选项,因此您需要决定是要检查 nil 还是仅仅忍受运行时爆炸的刺激(如果它们返回一个).
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 的答案中将 Objective-C 转换为非常简单的所有内容,我只是尝试将其粘贴到临时应用程序中,并且效果很好:
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屋!