question

BenTam-3003 avatar image
0 Votes"
BenTam-3003 asked BenTam-3003 commented

How to detect the key that the user just pressed

Dear All,

How to detect the key that the user just pressed? @Castorix31 told me to use WM_XXXX. I just want to know if the Up-Arrow or the Down-Arrow keys. Any simpler way you can suggest?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered BenTam-3003 commented

@BenTam-3003, If you want to detect the key when the user pressed, Please override the ProcessCmdKey method in your Form.

 public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
         {
             if (keyData == Keys.Up)
             {
                 MessageBox.Show("Up Arrow key");
             }
             if (keyData == Keys.Down)
             {
                 MessageBox.Show("Down Arrow key");
             }
             return base.ProcessCmdKey(ref msg, keyData);
         }
    
     }

When you press Up-Arrow key, it will show a message box called Up Arrow key and so on the Down Arrow key.

Updated code:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
         {
             byte[] ascii = System.Text.Encoding.UTF8.GetBytes(keyData.ToString());
             switch(ascii[0])
             {
                 case byte m when m > 32 && m < 255:
                     if(true)
                     {
                         break;
                     }
                 case byte m when m == 13:
    
                     break;
                 default:
                     if (keyData == Keys.Up)
                     {
                         MessageBox.Show("up Arrow key");
                        
                     }
                     else if (keyData == Keys.Down)
                     {
                         MessageBox.Show("Down Arrow key");
                            
                     }
                     break;
    
    
             }
               
             return base.ProcessCmdKey(ref msg, keyData);
         }

Best Regards,
Jack


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JackJJun-MSFT

Thanks for your reply. However, I want to use the retrieved value in my code as shown in the following figure. How to do it?


200779-error1.gif


0 Votes 0 ·
error1.gif (41.5 KiB)

@BenTam-3003, I have updated my answer. We need to move all the code from keypresseventhandler to ProcessCmdKey method.
·

0 Votes 0 ·

Hi @JackJJun-MSFT

Thanks for your update.

0 Votes 0 ·