如何在Windows窗体应用程序中实现键盘按键 [英] How to implement the keyboard key press in Windows Form Application
问题描述
我想在c#Windows窗体应用程序中实现键盘按钮按下命令.假设达到某个值,我想实现使用Windows窗体应用程序按下的键"L".这可能吗?怎么做?
这可能为您解决问题
但是,更好的方法可能是将表单的 KeyPreview
属性设置为 true
,然后将代码放入表单的 keyDown
事件中(并且还设置e.Handled = true,以防止将键事件传递给具有焦点的任何控件.)
private void Form1_KeyDown(对象发送者,KeyEventArgs e){如果(e.KeyCode == Keys.L){//在这里做}}
您还希望查看 MSDN:在窗体级别处理键盘输入,其中会显示有关Windows窗体的信息,从而可以在消息到达控件之前在窗体级别处理键盘消息.
修改
WM_KEYDOWN :此消息与按下非系统键时键盘焦点.非系统键是在未按下ALT键的情况下按下的键.
SendKeys :使用SendKeys可以将击键和击键组合发送到活动应用程序.此类无法实例化.要将击键发送给班级并立即继续执行程序流程,请使用发送".要等待按键启动的任何过程,请使用SendWait.
因此,当您从任何设备接收信号
System.Windows.Forms.SendKeys.Send("S");
并在keyDown上
private void Form1_KeyDown(对象发送者,KeyEventArgs e){如果(e.KeyCode == Keys.S){//拍摄代码}否则(e.KeyCode == Keys.L){//其他代码}}
并且除了以编程方式在C#中生成按键事件?还可以为您提供实现任务的想法>
I want to implement the keyboard button press commands inside a c# windows form application. Suppose if some value is reached I want to implement the key "L" pressed using the windows form application.Is this possible ? How to do it ?
This might do the trick for you
However, a better way is probably to set your form's KeyPreview
property to true
, and then put your code into the form's keyDown
event (and set e.Handled = true as well, to prevent the key event from being passed on to whichever control does have the focus).
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L)
{
//Do here
}
}
You would also like to view MSDN: Handle Keyboard Input at the Form Level which states about Windows Forms provides the ability to handle keyboard messages at the form level, before the messages reach a control.
Edit
WM_KEYDOWN: This message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.
SendKeys: Use SendKeys to send keystrokes and keystroke combinations to the active application. This class cannot be instantiated. To send a keystroke to a class and immediately continue with the flow of your program, use Send. To wait for any processes started by the keystroke, use SendWait.
so when you receive signal from any device
System.Windows.Forms.SendKeys.Send("S");
and on keyDown
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.S)
{
//Shooting Code
}
else if (e.KeyCode == Keys.L)
{
//Some Other Code
}
}
and apart from that Programmatically generate keypress events in C#? will also give you an idea to implement your task
这篇关于如何在Windows窗体应用程序中实现键盘按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!