不按键盘按键也能触发键盘按键事件如何不按键盘按键触发按键事件?我尝试了此处的解决方案,但出现以下异常:'System.Windows.PresentationSource.FromVisual(System.Windows.Media.Visual)'的最佳重载方法匹配有一些无效参数。考虑到Shift+A包含来自键盘的2键按键事件,我如何在不按下键盘的情况下执行此操作?(让我们通过按下按钮在文本框中打印一个大写字母A)我的代码varkey=Key.A;//发送键vartarget=Keyboard.FocusedElement;//目标元素varroutedEvent=Keyboard.按键事件;//要发送的事件target.RaiseEvent(newSystem.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice,System.Windows.PresentationSource.FromVisual(target),0,key){RoutedEvent=routedEvent});系统.Windows。Forms.SendKeys.Send()对我有用。有关高级说明和可用代码列表,请参阅该方法的文档。例如,要发送Shift+A,我使用了SendKeys.Send("+(a)")。您需要转换为Visual:varkey=Key.A;//发送键vartarget=Keyboard.FocusedElement;//目标元素varroutedEvent=Keyboard.KeyDownEvent;//要发送的事件target.RaiseEvent(newSystem.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice,System.Windows.PresentationSource.FromVisual((Visual)target),0,key){RoutedEvent=routedEvent});您还可以指定目标是特定的文本框,例如:vartarget=textBox1;//Target元素代码实际做的是触发特定元素的keydown事件。所以有一个keydown处理程序是有意义的:privatevoidtextBox1_KeyDown(objectsender,System.Windows.Input.KeyEventArgse){}模拟键盘事件的一种简单可靠的方法是使用keybd_eventapi你可以在这里参考http://www.pinvoke.net/default.aspx/user32.keybd_event这里还有其他文章http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd_event-funct以上是C#学习教程:无需Pressthekeyboardkey触发键盘按键事件共享的所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
