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.