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