简体   繁体   English

如何按下键盘键 c#

[英]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.条件:需要在表单上锚定面板。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM