Windows Develop Bookmark and Share   
 index > Windows Forms General > How to display caps lock and num lock status on status bar
 

How to display caps lock and num lock status on status bar

I am doing a simple application in C# where i need to use status strip on a form and display the num lock , caps lock status on it ? Is there any property associated with status bar for this ? Or I need to write a seperate function for this ??

Please let me know some code snippet which will solve this problem.

Abhijeet

abhijeetk  Tuesday, September 09, 2008 3:18 PM

Hi Abhijeetk

Use Control.IsKeyLocked to determine whether specific lock key has been activated, please refer to the following code sample:

Code Snippet

private void Form2_KeyDown(object sender, KeyEventArgs e)

{

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

{

if (Control.IsKeyLocked(Keys.CapsLock))

this.toolStripStatusLabel1.Text = "CapsLock is activated.";

else

this.toolStripStatusLabel1.Text = null;

}

if ((e.KeyCode & Keys.KeyCode) == Keys.NumLock)

{

if (Control.IsKeyLocked(Keys.NumLock))

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

else

this.toolStripStatusLabel2.Text = null;

}

}

If there is any question, please feel free to let me know.

Thanks.

Best wishes

Jun Wang

Jun Wang Tim  Thursday, September 11, 2008 3:07 AM

I know of no such code. However, this can be done programatically by first setting the KeyPreview property of the main form to true. Then you would handle the KeyDown event:

private void Form1_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.CapsLock)

{

//your code here

}

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

{

//your code here

}

}

I would then add a label ("CAPS" and "NUM") to the status bar and toggle its visible property. If it is visible when the key is pressed, make it invisible, and vice versa.
BobLeavell  Wednesday, September 10, 2008 12:20 PM

Hi Abhijeetk

Use Control.IsKeyLocked to determine whether specific lock key has been activated, please refer to the following code sample:

Code Snippet

private void Form2_KeyDown(object sender, KeyEventArgs e)

{

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

{

if (Control.IsKeyLocked(Keys.CapsLock))

this.toolStripStatusLabel1.Text = "CapsLock is activated.";

else

this.toolStripStatusLabel1.Text = null;

}

if ((e.KeyCode & Keys.KeyCode) == Keys.NumLock)

{

if (Control.IsKeyLocked(Keys.NumLock))

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

else

this.toolStripStatusLabel2.Text = null;

}

}

If there is any question, please feel free to let me know.

Thanks.

Best wishes

Jun Wang

Jun Wang Tim  Thursday, September 11, 2008 3:07 AM

You can use google to search for other answers

Custom Search

More Threads

• User Control size changed from one computer to the other
• Displaying long lines in a text box
• Charting controls
• Synchronizing 2 SQL Databases
• Flow layout panel, is it dot net control?
• WM_DRAWCLIPBOARD is received with delay
• DataGridView extra Value
• IShellItemImageFactory increasing GDI leaks
• ListView Image Centering
• TabControl TabPage question