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

    Question

  •  

    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

    Tuesday, September 09, 2008 3:18 PM

Answers

  • 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

     

    Thursday, September 11, 2008 3:07 AM

All replies

  • 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.
    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

     

    Thursday, September 11, 2008 3:07 AM
  • Hi

    I want to show caps Lock, Num Lock, scrol lock on my application in vb.net on a status bar can you please help in this regards

    Regards

    Mirza

    Sunday, July 11, 2010 12:31 PM
  • This thread is already answered. Please start a new one. 

    btw, if I answer you shortly, you can gather inforamtions from the answered post how to do the code for caps, num and scroll lock. You have already an example for caps lock.

    best regards

    Mitja

    Sunday, July 11, 2010 12:52 PM
  • <form id="aspnetForm" action="edit" enctype="application/x-www-form-urlencoded" method="post">
    <input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" /> <input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type="hidden" /> <input id="__VIEWSTATE" name="__VIEWSTATE" type="hidden" value="/wEPDwULLTEzNzk0MzkwMDlkZEMpty7R2lukRYyoGDAFiw9m2frw" />
    <script type="text/javascript"></script> <script type="text/javascript"></script> </form>
    <form action="/Forums/en-US/winforms/thread/cf06fc03-331e-4e33-a696-946c9aeb0ab3/0129362c-3989-4332-8ba9-d29bf020e4f5/edit" enctype="application/x-www-form-urlencoded" method="post">

    Edit Message

    <textarea cols="100" rows="20" name="body"><p>Hi</p> <p>I am still having problem with the same code i found above that it is not displaying it on the status bar like caps lock on or off.</p> <p>secondly if i use the same code it gives me a message box only once in the entire lifetime of the whole application, however i want to do some thing like &quot;when the User presses any of the keys any time during the life time of the application caps, num and scroll lock on/off must be shown in their respective cells/labels of the status bar.&quot;</p> <p>my code is:</p> <span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small"><font size="2" color="#0000ff"><font size="2" color="#0000ff"> <p>Private</p> </font></font></span><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></span> <p><span style="font-size:x-small"></span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Sub</span></span><span style="font-size:x-small"> Form1_KeyDown(</span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">ByVal</span></span><span style="font-size:x-small"> sender </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">As</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Object</span></span><span style="font-size:x-small">, </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">ByVal</span></span><span style="font-size:x-small"> e </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">As</span></span><span style="font-size:x-small"> System.Windows.Forms.KeyEventArgs) </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Handles</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Me</span></span><span style="font-size:x-small">.KeyDown<font size="2"> </p> <p>&nbsp;</p> </font></span> <p></p> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> e.KeyCode = Keys.CapsLock </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> CapLoc() = </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>sstUniqFoods.Text =</p> </font></span> <p><span style="color:#a31515; font-size:x-small"><span style="color:#a31515; font-size:x-small">&quot;On&quot;</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Me</span></span><span style="font-size:x-small">.sstUniqFoods.Text = </span><span style="color:#a31515; font-size:x-small"><span style="color:#a31515; font-size:x-small">&quot;Off&quot;</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">ElseIf</span></span><span style="font-size:x-small"> e.KeyCode = Keys.NumLock </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> NumLoc() = </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>sstUniqFoods.Text =</p> </font></span> <p><span style="color:#a31515; font-size:x-small"><span style="color:#a31515; font-size:x-small">&quot;On&quot;</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Me</span></span><span style="font-size:x-small">.sstUniqFoods.Text = </span><span style="color:#a31515; font-size:x-small"><span style="color:#a31515; font-size:x-small">&quot;Off&quot;</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> ScrolLoc() = </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>sstUniqFoods.Text =</p> </font></span> <p><span style="color:#a31515; font-size:x-small"><span style="color:#a31515; font-size:x-small">&quot;On&quot;</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Me</span></span><span style="font-size:x-small">.sstUniqFoods.Text = </span><span style="color:#a31515; font-size:x-small"><span style="color:#a31515; font-size:x-small">&quot;Off&quot;</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Sub</span></span></p> <p><span style="color:#0000ff; font-size:x-small"></span></p> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">'#########################</span></span></p> <span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small"><font size="2" color="#0000ff"><font size="2" color="#0000ff"> <p>Private</p> </font></font></span><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></span> <p><span style="color:#000000; font-size:x-small"></span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Function</span></span><span style="color:#000000; font-size:x-small"> CapLoc() </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">As</span></span><span style="color:#000000; font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Boolean</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> Keys.CapsLock = </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>CapLoc =</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>CapLoc =</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">False</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Function</span></span></p> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">#################################</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Private</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Function</span></span><span style="font-size:x-small"> NumLoc() </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">As</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Boolean</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> Keys.NumLock = </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>NumLoc =</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>NumLoc =</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">False</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Function</span></span></p> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">######################################</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Private</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Function</span></span><span style="font-size:x-small"> ScrolLoc() </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">As</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Boolean</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span><span style="font-size:x-small"> Keys.CapsLock = </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Then</span></span></p> <span style="font-size:x-small"><font size="2"> <p>ScrolLoc =</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">True</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Else</span></span></p> <span style="font-size:x-small"><font size="2"> <p>ScrolLoc =</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">False</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">If</span></span></p> <span style="font-size:x-small"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">End</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff; font-size:x-small"><span style="color:#0000ff; font-size:x-small">Function</span></span></p> </span></span> <p>Regards</p> <p>Mirza</p></textarea>
    <label for="hasCode">Resource.HasCodeLabel</label><input name="hasCode" type="checkbox" value="true" /> <label for="reason">Reason</label><input name="reason" type="text" />
    <input title="Submit" type="submit" value="Submit" />
    </form>
    <script type="text/javascript"></script> <script type="text/javascript"></script> <script type="text/javascript"></script> <noscript></noscript> <noscript></noscript>
    • Edited by MirAsh Sunday, July 11, 2010 1:18 PM not complete, now complete
    Sunday, July 11, 2010 1:03 PM
  • What are you saying? I doubt I can get the point out of this :)
    Sunday, July 11, 2010 1:43 PM