How to detect the key that the user just pressed

BenTam-3003 686 Reputation points
2022-05-10T14:47:12.853+00:00

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?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-05-11T02:42:12.6+00:00

    @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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.