469,306 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

NumLock, CapsLock, Inset/Overwrite

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
Nov 21 '05 #1
10 7387
Hi,

http://groups.google.be/groups?hl=nl...&meta=group%3D
microsoft.public.dotnet.languages.vb.*

Mind that the .* als need to be included in the link when you copy paste it,
but oe won't do this

hth Peter

"lgbjr" <lg***@online.nospam> schreef in bericht
news:uR**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2
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

"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
Hi,

http://groups.google.be/groups?hl=nl...&meta=group%3D
microsoft.public.dotnet.languages.vb.*

Mind that the .* als need to be included in the link when you copy paste
it,
but oe won't do this

hth Peter

"lgbjr" <lg***@online.nospam> schreef in bericht
news:uR**************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #3
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

"lgbjr" <lg***@online.nospam> schreef in bericht
news:#I*************@TK2MSFTNGP14.phx.gbl...
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

"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
Hi,

http://groups.google.be/groups?hl=nl...&meta=group%3D microsoft.public.dotnet.languages.vb.*

Mind that the .* als need to be included in the link when you copy paste
it,
but oe won't do this

hth Peter

"lgbjr" <lg***@online.nospam> schreef in bericht
news:uR**************@TK2MSFTNGP14.phx.gbl...
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



Nov 21 '05 #4
Would you be so kind as to post the code you use to check the state of the
Numlock, Caplock, and Scrolllock keys. Thanks.

"lgbjr" wrote:
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

"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
Hi,

http://groups.google.be/groups?hl=nl...&meta=group%3D
microsoft.public.dotnet.languages.vb.*

Mind that the .* als need to be included in the link when you copy paste
it,
but oe won't do this

hth Peter

"lgbjr" <lg***@online.nospam> schreef in bericht
news:uR**************@TK2MSFTNGP14.phx.gbl...
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



Nov 21 '05 #5
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
Would you be so kind as to post the code you use to check the state of the
Numlock, Caplock, and Scrolllock keys. Thanks.

"lgbjr" wrote:
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

"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> http://groups.google.be/groups?hl=nl...&meta=group%3D
> microsoft.public.dotnet.languages.vb.*
>
> Mind that the .* als need to be included in the link when you copy
> paste
> it,
> but oe won't do this
>
> hth Peter
>
>
>
> "lgbjr" <lg***@online.nospam> schreef in bericht
> news:uR**************@TK2MSFTNGP14.phx.gbl...
>> 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
>>
>>
>
>


Nov 21 '05 #6
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.

Nov 21 '05 #7
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

""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:ZE*************@TK2MSFTNGXA02.phx.gbl...
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.

Nov 21 '05 #8
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.

"lgbjr" wrote:
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

""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:ZE*************@TK2MSFTNGXA02.phx.gbl...
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.


Nov 21 '05 #9
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...B;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.

Nov 21 '05 #10
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...B;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.

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by James Kirkup | last post: by
2 posts views Thread by Chris | last post: by
2 posts views Thread by Woody Splawn | last post: by
5 posts views Thread by Stan Sainte-Rose | last post: by
1 post views Thread by nTn | last post: by
reply views Thread by =?Utf-8?B?Q29tcHV0ZXJfRW5naW5lZXJfU3R1ZGVudA==?= | last post: by
3 posts views Thread by Z.K. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.