Hi,
I am writting a software that requires me to make sure the Num Lock is
always on and Caps Lock is always off.
First, I know how to detect if Num Lock or Caps Lock is on or off (if
someone is interested, let me know and I will send you the codes). Once
we know if the stat of Num Lock/ Caps Lock is not what we desired, we
just send the Num Lock / Caps Lock key to change the stat. From most of
the news group I have searched, there are 2 ways to do this.
1) Easy way,
Use
SendKeys.Send("{NUMLOCK}") and SendKeys.Send("{CAPSLOCK}")
Unfortunately, this does not seem to work at all even though. A lot of
these SendKeys command do work (I tried to send Tab key, Enter key and
they both work) but the Num Lock, Caps Lock and Scroll Lock does not
seem to work at all.
Did Microsoft or anyone find the problem for this and if there are any
fixes available?
2) More Complex way,
Could someone tell me what did I do wrong here, for this does not work
either... I have tested and GetKeyState works fine. When I run
SendInput command, I would get -5245152119303962624 as the result.
Declare Function GetMessageExtraInfo Lib "user32.dll" alias
"GetMessageExtraInfo" () As IntPtr
Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal
nVirtKey As Int32) As Int16
Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long,
ByRef pInputs As INPUT, ByVal cbSize As Long) As Long
' Constants to verify num lock, cap lock, scroll lock state
Private Const VK_CAPSLOCK = &H14
Private Const VK_NUMLOCK = &H90
Private Const KEYEVENTF_KEYUP = &H2
Private Const INPUT_KEYBOARD = 1
Public Const VK_SCROLL = &H91
Public Structure INPUT
Public type As Integer
Public mi As MOUSEINPUT
Public ki As KEYBDINPUT
Public hi As HARDWAREINPUT
End Structure
Public Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
Public Structure KEYBDINPUT
Dim wVk As Integer
Dim wScan As Integer
Dim dwFlags As Integer
Dim time As Integer
Dim dwExtraInfo As IntPtr
End Structure
Public Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Integer
Public wParamH As Integer
End Structure
Dim myInput As Input = New Input
Dim sizeofINPUT As Integer =
System.Runtime.InteropServices.Marshal.SizeOf(myIn put)
myInput.type = INPUT_KEYBOARD
myInput.ki.wScan = 0
myInput.ki.time = 0
myInput.ki.dwFlags = 0
myInput.ki.dwExtraInfo = GetMessageExtraInfo()
' If Caps Lock is on, send another Caps Lock
If GetKeyState(VK_CAPSLOCK) = 1 Then
' send CAPSLOCK Key Down
myInput.ki.dwFlags = 0
myInput.ki.wVk = VK_CAPSLOCK
Dim myResult As Long = SendInput(1, myInput, sizeofINPUT)
' send CAPSLOCK Key UP
myInput.ki.dwFlags = KEYEVENTF_KEYUP
myInput.ki.wVk = VK_CAPSLOCK
Dim myNextResult As Long = SendInput(1, myInput, sizeofINPUT)
End If
Thanks
Charlie Chang
[ch*********@hotmail.com]