NumLock & CapsLock in StatusBar [dot net]

Merchand

Well-known member
Joined
Dec 28, 2005
Messages
73
Location
USA Eastcoast
Programming Experience
10+
Hey world, it's getting close to go home time here...

I'm trying to add the NumLock and CapsLock to a status bar in a UserControl I'm building and it looks like I may have to go to the old API Declares for a GetKeyState() call. Unless there is any one who knows how to set the num and caps lock on and off in .net?

I'd like to not use APIs to do this functionality should be built-in like in VB6. I thought vb.net was suppose to help the developer? :(

Help!!!
 
Ok, I coded the API solution found on the inet... And tweaked it a bit...

Add this declaration to your class as public...
VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Declare [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] GetKeyState [/SIZE][SIZE=2][COLOR=#0000ff]Lib[/COLOR][/SIZE][SIZE=2] "user32" [/SIZE][SIZE=2][COLOR=#0000ff]Alias[/COLOR][/SIZE][SIZE=2] "GetKeyState" ([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nVirtKey [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Long[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]I nteger[/COLOR][/SIZE]


Add this method to your class as private...
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#000000] SetPanelLocks()[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] numLockValue [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] capsLockValue [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]With [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].sbrApplication.Panels(panelIndex.AppCapsLock)
capsLockValue = GetKeyState(Keys.CapsLock)
[/SIZE][SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](capsLockValue, [/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2]) = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2].Text = "Caps On"
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2].Text = "Caps Off"
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].sbrApplication.Panels(panelIndex.AppNumLock)
numLockValue = GetKeyState(Keys.NumLock)
[/SIZE][SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](numLockValue, [/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2]) = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2].Text = "Num On"
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2].Text = "Num Off"
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]'Add Error Handler here or throw exception here.
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE]


Call SetCapsLock() in timer event...

VB.NET:
[COLOR=blue]Private Sub tmrSystem_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSystem.Tick[/COLOR]
[COLOR=blue]Try[/COLOR]
[COLOR=blue]If DesignMode Then tmrSystem.Enabled = False[/COLOR]
[COLOR=blue]SetDateTime()[/COLOR]
[COLOR=blue]SetPanelLocks()[/COLOR]
[COLOR=blue]Catch ex As Exception[/COLOR]
[COLOR=blue]'Raise Error...[/COLOR]
[COLOR=blue]End Try[/COLOR]
[COLOR=blue]End Sub[/COLOR]


Timer enabled via a public property in the User Control and Interval is set that way too.

If anyone knows of a not API solution it would be greatly appreciated!!!
 
Last edited by a moderator:
Kulrom, how do you paste your code sample into post so you don't loss formatting? Looks like some type of pic? Losing this formatting is killing me...
 
Simple coding to check NumLock & CapsLock in StatusBar

merchand,

I wonder why you have to write such long coding to getting the result..Below is an example of mine, it could be suit you.

PrivateSub CheckLockKeys()
If My.Computer.Keyboard.CapsLock Then
ToolStripCAPSLabel.Enabled = True
Else
ToolStripCAPSLabel.Enabled = False
End If
If My.Computer.Keyboard.NumLock Then
ToolStripNUMSLabel.Enabled = True
Else
ToolStripNUMSLabel.Enabled = False
End If
End Sub

PublicSub MDIParent_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMyBase.KeyDown
If e.KeyCode = Keys.CapsLock Or e.KeyCode = Keys.NumLock Then
CheckLockKeys()
End If
End Sub

Just exercute the checklockkeys on your form load.
 
merchand,

I wonder why you have to write such long coding to getting the result..Below is an example of mine, it could be suit you.

PrivateSub CheckLockKeys()
If My.Computer.Keyboard.CapsLock Then
ToolStripCAPSLabel.Enabled = True
Else
ToolStripCAPSLabel.Enabled = False
End If
If My.Computer.Keyboard.NumLock Then
ToolStripNUMSLabel.Enabled = True
Else
ToolStripNUMSLabel.Enabled = False
End If
End Sub

PublicSub MDIParent_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMyBase.KeyDown
If e.KeyCode = Keys.CapsLock Or e.KeyCode = Keys.NumLock Then
CheckLockKeys()
End If
End Sub

Just exercute the checklockkeys on your form load.
Chances are that that code was written before VS 2005, .NET 2.0 and the My.Computer object ever existed. For those using VS.NEt 2003 the provided code would still be required as there's no managed method to get that information.
 
jacky.goy, just by looking at the dates of the posts this is an old thread and back then VS 2005 wasnt out yet

since then the Primary Platform part of our profiles have been added, with VS 2005 being the default

so back when this thread was started, the 'My' concept didnt exist and it took that much code to perform the same task

..... and of course you beat me to it jm....
 
Hey everyone! Thanks for the comments. I did write this back in Dec last year and there was not the My namespace available yet in VS2003. This functionality my have improved for VS2005.

Hey JuggaloBrotha, I'm from just north of you about 1.5 hours. In SC now but Go Michigan and Detroit Tigers!
 
Opps...

jacky.goy, just by looking at the dates of the posts this is an old thread and back then VS 2005 wasnt out yet

since then the Primary Platform part of our profiles have been added, with VS 2005 being the default

so back when this thread was started, the 'My' concept didnt exist and it took that much code to perform the same task

..... and of course you beat me to it jm....

opps..ic..I didn't notice the date posted. Sorry

Seems that I'm a bit out-dated :D
 
Hey everyone! Thanks for the comments. I did write this back in Dec last year and there was not the My namespace available yet in VS2003. This functionality my have improved for VS2005.

Hey JuggaloBrotha, I'm from just north of you about 1.5 hours. In SC now but Go Michigan and Detroit Tigers!

meh, stay down there it's not so fun up here (and the tigers suck IMO) though I am loving the colder cloudy days up here as of late :)
 
Back
Top