Reputation: 105
I have the following code in my project which disables arrow keys completely:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!msg.HWnd.Equals(Handle) &&
(keyData == Keys.Left || keyData == Keys.Right ||
keyData == Keys.Up || keyData == Keys.Down))
return true;
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
I also have this code after it which I need in order to make it such that the up arrow key counts up.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
int count;
if (count_lbl.Text.Equals(""))
count = 0;
else
count = Convert.ToInt32(count_lbl.Text);
count++;
count_lbl.Text = count.ToString();
}
}
I don't want the arrow keys to change focus to any control, other than the one I initialized my form to, but I still want to be able to use them later on in order to be able to do other things. Is there a way to do this?
Thanks.
Upvotes: 0
Views: 145
Reputation: 39122
Just put the "counting" code in ProcessCmdKey()
?
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!msg.HWnd.Equals(Handle) &&
(keyData == Keys.Left || keyData == Keys.Right ||
keyData == Keys.Up || keyData == Keys.Down)) {
if (keyData == Keys.Up) {
int count;
if (count_lbl.Text.Equals(""))
count = 0;
else
count = Convert.ToInt32(count_lbl.Text);
count++;
count_lbl.Text = count.ToString();
}
return true;
}
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Upvotes: 1