Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, can any one help me to show whether caps and num lock is on or off if i press caps button and num button. thnx in advance..
Posted

 
Share this answer
 
Comments
Member 8233601 24-Aug-12 6:22am    
thnx...
ridoy 24-Aug-12 7:38am    
welcome
C#
public KeyStatusStatusBar(bool CapsLock, bool NumLock)
{
 // This call is required by the Windows.Forms Form Designer.
 InitializeComponent();
 ...
 Application.Idle += new System.EventHandler(OnIdle);
}
...
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);

public void OnIdle(object sender, EventArgs e)
{
 // Update the panels when the program is idle.
 bool CapsLock = (((ushort) GetKeyState(0x14 /*VK_CAPITAL*/)) & 0xffff) != 0;
 bool NumLock  = (((ushort) GetKeyState(0x90 /*VK_NUMLOCK*/)) & 0xffff) != 0;
 if (CapsLockPanel != null)
 {
  CapsLockPanel.Text = CapsLock ? "CAP" : "";
 }
 if (NumLockPanel != null)
 {
  NumLockPanel.Text  = NumLock  ? "NUM" : "";
 }
}
 
Share this answer
 
Comments
Member 8233601 24-Aug-12 3:19am    
I USED THE FOLLOWING CODE IT WORKS FINE BUT NUMS LOCK WORK FOR ONLE ONCE i.e if my nums lock is off IT SHOW NUMS IS OFF BUT AFTER THAR IT SHOWS THE SAME STATUS
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode & Keys.KeyCode) == Keys.CapsLock)
{
if (Control.IsKeyLocked(Keys.CapsLock))
this.toolStripStatusLabel3.Text = "Caps is activated.";
else
this.toolStripStatusLabel3.Text = "Caps is Deactivated.";
}

else if ((e.KeyCode & Keys.KeyCode) == Keys.NumLock)
{
if (Control.IsKeyLocked(Keys.NumLock))

this.toolStripStatusLabel5.Text = "NumLock is activated.";

else

this.toolStripStatusLabel5.Text = "NumLock is Deactivated.";


}



}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900