如何在不按下键盘键的情况下触发按键事件?我尝试使用 here 中的解决方案,但我得到以下异常:
The best overloaded method match for 'System.Windows.PresentationSource.FromVisual(System.Windows.Media.Visual)' has some invalid arguments.
考虑 Shift+A 包含来自键盘的 2 个按键事件,我如何在不按下键盘的情况下做到这一点?(让,只需在单击按钮时在文本框中打印大写字母 A)
我的代码
var key = Key.A; // Key to send
var target = Keyboard.FocusedElement; // Target element
var routedEvent = Keyboard.KeyDownEvent; // Event to send
target.RaiseEvent(new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice, System.Windows.PresentationSource.FromVisual(target), 0, key) { RoutedEvent = routedEvent });
最佳答案
System.Windows.Forms.SendKeys.Send()
为我解决了问题。
有关高级说明和可用代码列表,请参阅 the docs for the method .
例如,要发送 Shift+A,我使用了 SendKeys.Send("+(a)")
。
关于c# - 在不按下键盘键的情况下触发键盘按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15574850/