NumLock, CapsLock, Inset/Overwrite

L

lgbjr

Hi All,

In VB.Net how do I determine if CapsLock is on/off (same for NumLock) and
whether the input mode is set to Insert or Overwrite (Insert Key). I just
added a status bar to my app, and I want to reflect the state of these three
items (similar to Word, Excel, etc.) I've found the key enumerator value for
each key, but I don't know how to tell if the state is on/off, etc.

TIA
Lee
 
L

lgbjr

Peter,

Thanks for the link. I found the info I was looking for (actually more than
what I really wanted!). I've added the code to look at the state of the keys
that I want. and temporarily, I've created a button and put the code in the
button click event. So, when I click the button, the status bar shows the
state of each of the keys. All good so far.

Now, how do I, or where do I, put the code so that it continually checks for
the state of these keys. Obviously I don't want to click a button to see the
state. If the user turns on NumLock, I want the event captured and displayed
in the status bar.

I thought I could just use the status bar keypress event, but that doesn't
seem to work, or at least I'm not doing it properly.

TIA
Lee
 
P

Peter Proost

Hi,

you could set the form's keypreview to true and then always run your code
in the forms keydown or keyup event

hth

Greetz Peter
 
G

Guest

Would you be so kind as to post the code you use to check the state of the
Numlock, Caplock, and Scrolllock keys. Thanks.
 
L

lgbjr

Dennis,

This is the code I use to check the status of NumLock, CapsLock, ScrollLock,
and Insert/OverWrite. In this code, the status is checked when I click
Button1. what I can't figure out is how to setup the event to check any
keypress without user interaction (without clicking a button). I've set the
form keypreview property to true and tried using the following sub, but it
doesn't work:

Private Sub My_Key_Press(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

code to check key status

End Sub

Here's the code to check the key status:

Private Declare Function GetKeyboardState Lib "user32" (ByRef pbKeyState
As Byte) As Integer
Private Declare Function SetKeyboardState Lib "user32" (ByRef
lppbKeyState As Byte) As Integer

' Constant declarations:

Const VK_NUMLOCK As Short = &H90S
Const VK_SCROLL As Short = &H91S
Const VK_CAPITAL As Short = &H14S
Const VK_INS As Short = &H2DS

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean
Dim InsState As Boolean
Dim keys(255) As Byte

GetKeyboardState(keys(0))

' NumLock handling:

NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then
StatusBar.Panels(1).Text = ""
Else
StatusBar.Panels(1).Text = "NUM"
End If

' CapsLock handling:

CapsLockState = keys(VK_CAPITAL)
If CapsLockState <> True Then
StatusBar.Panels(2).Text = ""
Else
StatusBar.Panels(2).Text = "CAP"
End If

' ScrollLock handling:

ScrollLockState = keys(VK_SCROLL)
If ScrollLockState <> True Then
StatusBar.Panels(3).Text = ""
Else
StatusBar.Panels(3).Text = "SCR"
End If

' Insert handling:

InsState = keys(VK_INS)
If InsState <> True Then
StatusBar.Panels(4).Text = ""
Else
StatusBar.Panels(4).Text = "INS"
End If

End Sub

If you know how to make this work without using a control to fire this
event, please let me know.

thanks
Lee
 
P

Peter Huang [MSFT]

Hi

I think the controls keys will fire the key_press event, we need to handle
the KeyUp event and form_active event, so that when we press the key or
switch from another window, the key will be updated.

Private Sub UpdateKeyState()
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean
Dim InsState As Boolean
Dim keys(255) As Byte

GetKeyboardState(keys(0))

' NumLock handling:

NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then
StatusBar1.Panels(1).Text = ""
Else
StatusBar1.Panels(1).Text = "NUM"
End If

' CapsLock handling:

CapsLockState = keys(VK_CAPITAL)
If CapsLockState <> True Then
StatusBar1.Panels(2).Text = ""
Else
StatusBar1.Panels(2).Text = "CAP"
End If

' ScrollLock handling:

ScrollLockState = keys(VK_SCROLL)
If ScrollLockState <> True Then
StatusBar1.Panels(3).Text = ""
Else
StatusBar1.Panels(3).Text = "SCR"
End If

' Insert handling:

InsState = keys(VK_INS)
If InsState <> True Then
StatusBar1.Panels(4).Text = ""
Else
StatusBar1.Panels(4).Text = "INS"
End If
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
UpdateKeyState()
End Sub


Private Sub Form1_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
UpdateKeyState()
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

lgbjr

Peter,

Thanks!! I've got 99% of what I need. when the form has focus, the status
bar updates whenever one of the Lock keys is pressed. And when the app
loads, it gets the state of the keys and updates the status bar.

Now, if the form loses focus, when I go back to the form, the status bar has
not been updated As an example: my app has focus, NumLock is off, I focus on
another app and turn NumLock on. when I go back to my app, the status bar
does not show Num Lock on unless I change the NumLock state when the form
has focus.

so, I need one more event handler for when the form gets focus (after the
activated event).

TIA
Lee
 
G

Guest

I would have thought that the Activate event would have fired but guess not.
You might check the WinProc form method that is called whenever the form
receives a message.
 
P

Peter Huang [MSFT]

Hi

Based on my test, we need to use GetKeyState to update the key state.

BTW: Based on the KB below, if we have datagrid on the form, even if
KeyPreview is set to true, the KeyUp event will not fire, this is by design.
So we need to subclass by override the wndproc of the datagrid to send the
key_up message back to the form.
PRB: Form Key Events Are Not Raised When You Type in a DataGrid Cell with
the KeyPreview Property Set to True
http://support.microsoft.com/default.aspx?scid=KB;EN-US;815252

Private Declare Function GetKeyboardState Lib "user32" (ByRef
pbKeyState As Byte) As Integer
Private Declare Function SetKeyboardState Lib "user32" (ByRef
lppbKeyState As Byte) As Integer
Private Declare Function GetKeyState Lib "user32" Alias "GetKeyState"
(ByVal nVirtKey As Integer) As Short
Private Declare Function GetAsyncKeyState Lib "user32" Alias
"GetAsyncKeyState" (ByVal vKey As Integer) As Short

' Constant declarations:

Const VK_NUMLOCK As Short = &H90S
Const VK_SCROLL As Short = &H91S
Const VK_CAPITAL As Short = &H14S
Const VK_INS As Short = &H2DS
Private Sub UpdateKeyState()
Trace.WriteLine("UpdateKeyState called")
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean
Dim InsState As Boolean
Dim keys(254) As Byte

GetKeyboardState(keys(0))

' NumLock handling:

NumLockState = GetKeyState(VK_NUMLOCK)
Trace.WriteLine(NumLockState)
If NumLockState <> True Then
StatusBar1.Panels(1).Text = ""
Else
StatusBar1.Panels(1).Text = "NUM"
End If

' CapsLock handling:

CapsLockState = GetKeyState(VK_CAPITAL)
Trace.WriteLine(CapsLockState)

If CapsLockState <> True Then
StatusBar1.Panels(2).Text = ""
Else
StatusBar1.Panels(2).Text = "CAP"
End If

' ScrollLock handling:

ScrollLockState = GetKeyState(VK_SCROLL)
Trace.WriteLine(ScrollLockState)
If ScrollLockState <> True Then
StatusBar1.Panels(3).Text = ""
Else
StatusBar1.Panels(3).Text = "SCR"
End If

' Insert handling:

InsState = GetKeyState(VK_INS)
If InsState <> True Then
StatusBar1.Panels(4).Text = ""
Else
StatusBar1.Panels(4).Text = "INS"
End If
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Trace.WriteLine("Form1_KeyUp")
UpdateKeyState()
End Sub


Private Sub Form1_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Trace.WriteLine("Form1_Activated")
UpdateKeyState()
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top