Numlock Problem ?

  • Thread starter Thread starter Z.K.
  • Start date Start date
Z

Z.K.

I am trying to detect if the Numlock is pressed. I can do it okay with
the CapsLock or the ScrLk, but not with NumLock. The code for all three
checks is exactly the same, but for some reason numlock always thinks it
is on.


Z.K.


code:

private void MainForm_KeyDown(object sender, KeyEventArgs e)
{

if (Control.IsKeyLocked(Keys.NumLock))
{
MessageBox.Show("The Num Lock key is ON.");
}
else
{
MessageBox.Show("The Num Lock key is OFF.");
}


if (Control.IsKeyLocked(Keys.CapsLock))
{
MessageBox.Show("The Caps Lock key is ON.");
}
else
{
MessageBox.Show("The Caps Lock key is OFF.");
}

if (Control.IsKeyLocked(Keys.Scroll))
{
MessageBox.Show("The Scroll Lock key is ON.");
}
else
{
MessageBox.Show("The Scroll Lock key is OFF.");
}


}
 
Z.K. said:
I am trying to detect if the Numlock is pressed. I can do it okay with
the CapsLock or the ScrLk, but not with NumLock. The code for all three
checks is exactly the same, but for some reason numlock always thinks it
is on.


Z.K.


code:

private void MainForm_KeyDown(object sender, KeyEventArgs e)
{

if (Control.IsKeyLocked(Keys.NumLock))
{
MessageBox.Show("The Num Lock key is ON.");
}
else
{
MessageBox.Show("The Num Lock key is OFF.");
}


if (Control.IsKeyLocked(Keys.CapsLock))
{
MessageBox.Show("The Caps Lock key is ON.");
}
else
{
MessageBox.Show("The Caps Lock key is OFF.");
}

if (Control.IsKeyLocked(Keys.Scroll))
{
MessageBox.Show("The Scroll Lock key is ON.");
}
else
{
MessageBox.Show("The Scroll Lock key is OFF.");
}


}


Well, I found a way around the IsKeyLocked problem with NumLock. The
code is below though I still would like to know why IsKeyLocked does not
work with NumLock.

Z.K.

code:

String strLockText;

if(e.KeyCode == Keys.CapsLock)
{

//your code here
strLockText = stbStatusBarPanel3.Text;

if (strLockText.Contains("CAP"))
{
stbStatusBarPanel3.Text = "";

}
else
{
stbStatusBarPanel3.Text = "CAP";

}

}

else if(e.KeyCode == Keys.NumLock)
{

//your code here

strLockText = stbStatusBarPanel4.Text;

if (strLockText.Contains("NUM"))
{
stbStatusBarPanel4.Text = "";

}
else
{
stbStatusBarPanel4.Text = "NUM";

}


}

else if (e.KeyCode == Keys.Scroll)
{

strLockText = stbStatusBarPanel5.Text;

if (strLockText.Contains("SCRL"))
{
stbStatusBarPanel5.Text = "";

}
else
{
stbStatusBarPanel5.Text = "SCRL";

}

}
 
Back
Top