I'm using the following code to check is NumLock, CapsLock or Insert are pressed, and if their status is ON update a label's text accordingly. But for some reason I can't get NumLock to work properly. It updates the label's text with "Num" if NumLock is set on but fails to update if NumLock is set off. Every other check in the code works. Would appreciate any help. Thank you.
private void num_ins_caps_keyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode & Keys.KeyCode) == Keys.CapsLock)
{
if (Control.IsKeyLocked(Keys.CapsLock))
num_ins_caps1.capsLabel.Text = "Caps";
else
num_ins_caps1.capsLabel.Text = null;
}
if ((e.KeyCode & Keys.KeyCode) == Keys.NumLock)
{
if (Control.IsKeyLocked(Keys.NumLock))
num_ins_caps1.numLabel.Text = "Num";
else
num_ins_caps1.numLabel.Text = null;
}
if ((e.KeyCode & Keys.KeyCode) == Keys.Insert)
{
if (Control.IsKeyLocked(Keys.Insert))
num_ins_caps1.insLabel.Text = "Ins";
else
num_ins_caps1.insLabel.Text = null;
}
}
I'm not sure where you're calling this from, but if you're using some control's
KeyDown
event, try theKeyUp
event instead.I tested your code and experienced the same issue. I don't know why it doesn't work with
KeyDown
, but it worked as expected when I attached it to theKeyUp
event.I'm assuming that the signal from Numlock turning "on" is sent before the
KeyDown
event fires, but the signal from Numlock turning "off" is sent after theKeyDown
event fires, so the code executes and still thinks it's enabled. There's nothing in the msdn docs to explain the behavior.