C# 触发键盘按键事件而不按键盘上的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15574850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trigger a keyboard key press event without pressing the key from keyboard
提问by ridoy
How can I trigger a key press event without pressing the key from Keyboard? I tried with the solution from here, but I got the following exception:
如何在不按键盘键的情况下触发按键事件?我尝试使用此处的解决方案,但出现以下异常:
The best overloaded method match for 'System.Windows.PresentationSource.FromVisual(System.Windows.Media.Visual)' has some invalid arguments.
'System.Windows.PresentationSource.FromVisual(System.Windows.Media.Visual)' 的最佳重载方法匹配有一些无效参数。
Consider Shift+Acontains 2 key press event from keyboard,how can I do it without keyboard pressing?(let, just print capital A in a textbox on a button click)
考虑Shift+A包含来自键盘的 2 个按键事件,如何在不按下键盘的情况下执行此操作?(让,只需在单击按钮时在文本框中打印大写 A)
My code
我的代码
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 });
采纳答案by ridoy
System.Windows.Forms.SendKeys.Send()
has done the trick for me.
System.Windows.Forms.SendKeys.Send()
已经为我解决了这个问题。
For advanced instructions and the list of available codes, consult the docs for the method.
有关高级说明和可用代码列表,请参阅方法的文档。
For example, to send Shift+AI used SendKeys.Send("+(a)")
.
例如,发送Shift+A我使用SendKeys.Send("+(a)")
.
回答by Murtuza Kabul
Easy and reliable way to simulate the keyboard event is to use the keybd_event api
模拟键盘事件的简单可靠的方法是使用 keybd_event api
You can get reference to it here
你可以在这里得到参考
http://www.pinvoke.net/default.aspx/user32.keybd_event
http://www.pinvoke.net/default.aspx/user32.keybd_event
There are further articles on it here
这里有更多关于它的文章
http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd_event-funct
http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd_event-funct
回答by Sami Franka
You need to cast to Visual
:
你需要投射到Visual
:
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((Visual)target), 0, key) { RoutedEvent = routedEvent });
You could also specify that your target is a specific textbox for example :
您还可以指定您的目标是特定的文本框,例如:
var target = textBox1; // Target element
What the code actually does is trigger the keydown event for a specific element. So it makes sense to have a keydown handler for it :
代码实际做的是触发特定元素的 keydown 事件。因此,为其设置一个 keydown 处理程序是有意义的:
private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
}