MacOS Swiftui发送模拟器键盘事件
每个人:
我只是开发一个MacOS桌面应用程序,我创建一个按钮,然后单击模拟器keyborad事件发送一些char,例如a
到上面的文本字段,但不起作用。有帮助吗?
struct ContentView: View {
@State private var username: String = ""
@FocusState private var emailFieldIsFocused: Bool
var body: some View {
VStack {
TextField(
"User name (email address)",
text: $username
)
.focused($emailFieldIsFocused)
.onSubmit {
}
.disableAutocorrection(true)
.border(.secondary)
Button {
print("button clicked")
let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)
let eventDown = CGEvent(keyboardEventSource: src, virtualKey: UInt16(0), keyDown: true)
eventDown?.post(tap: .cghidEventTap)
let eventUp = CGEvent(keyboardEventSource: src, virtualKey: UInt16(0), keyDown: false)
eventUp?.post(tap: .cghidEventTap)
} label: {
Text("click me")
}.padding()
}
}
我希望将键盘输入a
仿真到文本字段,然后单击按钮,但无需发送。
Everybody:
I just dev a macOS desktop app, I create one button then clicked for simulator the keyborad event send some char such as a
to the textfield above, but not working. Any help?
struct ContentView: View {
@State private var username: String = ""
@FocusState private var emailFieldIsFocused: Bool
var body: some View {
VStack {
TextField(
"User name (email address)",
text: $username
)
.focused($emailFieldIsFocused)
.onSubmit {
}
.disableAutocorrection(true)
.border(.secondary)
Button {
print("button clicked")
let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)
let eventDown = CGEvent(keyboardEventSource: src, virtualKey: UInt16(0), keyDown: true)
eventDown?.post(tap: .cghidEventTap)
let eventUp = CGEvent(keyboardEventSource: src, virtualKey: UInt16(0), keyDown: false)
eventUp?.post(tap: .cghidEventTap)
} label: {
Text("click me")
}.padding()
}
}
I hope simulate the keyboard input a
to the textfield then click the button, but nothing to send.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论