简体   繁体   中英

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

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. protected override bool ProcessCmdKey(ref Message message, Keys KeyData) use code

     protected override bool ProcessCmdKey(ref Message message, Keys KeyData)
     {
         switch (KeyData)
         {
             case Keys. E:
                 MessageBox.Show("Hello");
                 break;
         }
         return true;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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