Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Everybody,
Can anyone explain me briefly how to check the caps lock keys status in vb application without using timer control.

I have checked the status of the caps lock key by using timer control as follows.

************************************************************************
********************* codes ******************************
************************************************************************

I have taken a label & named it lblCapsLock. Then take a timer control & named as timer1. Set the timer1 control properties Enable, False to True and interval= 1000.
Now on tick event of the timer control I have written


VB
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblCapslock.Text = "CAPS LOCK: " & IIf(My.Computer.Keyboard.CapsLock = True, "ON", "OFF")
    End Sub



similarly U can set the status for other keys.
It works nice. But, I want to know that, how to set the status without using timer control. If anybody knows then please tell me.

Regards,
Subrat, Desktop Application Developer
Rourkela, Orissa, India :-O :cool::cool::thumbsup::cool::thumbsup:
Posted
Updated 2-Nov-10 1:14am
v3
Comments
JF2015 2-Nov-10 8:03am    
Here is an article on keyboard hooks. Although it is in C# it might help you:
http://www.codeproject.com/KB/cs/globalhook.aspx

You can use the KeyDown event for your purpose. All you need to do is to create an event handler that will notice if the Caps Lock key was pressed. You would also want to read the Caps status at the start of your application.
VB
Private Sub Form1_KeyDown (...) Handles MyBase.KeyDown
  If e.KeyCode = Keys.CapsLock Then
    lblCapslock.Text = "CAPS LOCK: " & IIf(My.Computer.Keyboard.CapsLock = True, "ON", "OFF")
  End If
End Sub
 
Share this answer
 
Comments
Subrat Kumar Rautara 2-Nov-10 7:59am    
You are right. I have done it so earlier. But there is a error. It will works only when the window is in active state. If you will minimized the window and then test it, or ON/OFF the key then it will not work.
Is there any method?
Simon_Whale 2-Nov-10 13:24pm    
nice answer
 
Share this answer
 
The only other way to do this, based on your requirements, would be a global keyboard hook. There's tons of examples on the web, all you have to do is Google "VB.NET Global Keyboard Hook". Warning! Not for the faint of heart!
 
Share this answer
 
I am having really hard time with this VB class i am taking would anyone care to help Robert6090 AIM name
 
Share this answer
 
Comments
Simon_Whale 5-Nov-10 7:46am    
best thing is to start with the following http://msdn.microsoft.com/en-gb/beginner/default.aspx

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