[英]How to get keyboard key pressed c#
I'm trying to get the key pressed on the keyboard (not a specific), i watched somes "solutions" everywhere and i found nothing working .我正在尝试按下键盘上的键(不是特定的),我到处都看到了一些“解决方案”,但没有发现任何工作。 Conditions: Need anchoring a panel on the form.
条件:需要在表单上锚定面板。
https://docs.microsoft.com/en-us/dotnet/desktop/winforms/input-keyboard/how-to-handle-forms?view=netdesktop-6.0 https://docs.microsoft.com/en-us/dotnet/desktop/winforms/input-keyboard/how-to-handle-forms?view=netdesktop-6.0
This worked on a Console, i want the same without cancelling it!这在控制台上有效,我想要同样的而不取消它! https://docs.microsoft.com/en-us/dotnet/api/System.Console.CancelKeyPress?view=net-6.0
https://docs.microsoft.com/en-us/dotnet/api/System.Console.CancelKeyPress?view=net-6.0
private void Form1_KeyPress(object sender, KeyPressEventArgs e) { KeysConverter convertor = new KeysConverter(); string keyPressed = convertor.ConvertToString(e.KeyChar); if (keyPressed == "t") { Console.WriteLine("THIS IS T"); } }
try this in your Form1.cs在你的 Form1.cs 中试试这个
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.T)
{
MessageBox.Show("THIS IS T");
}
return base.ProcessCmdKey(ref msg, keyData);
}
enter code here
as I understood, you put the focus on a button B and defined that if the A key is pressed, the code related to the A button will be executed. enter code here
据我了解,您将焦点放在按钮B上并定义如果按下A键,将执行与A按钮相关的代码。 protected override bool ProcessCmdKey(ref Message message, Keys KeyData) use code protected override bool ProcessCmdKey(ref Message message, Keys KeyData) 使用代码
protected override bool ProcessCmdKey(ref Message message, Keys KeyData)
{
switch (KeyData)
{
case Keys. E:
MessageBox.Show("Hello");
break;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.