Programmatically Detect and Send Num Lock and Caps Lock

C

charlies224

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(myInput)

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
[[email protected]]
 
H

Herfried K. Wagner [MVP]

[Setting the state of Num Lock and Caps Lock]
1) Easy way,

Use
SendKeys.Send("{NUMLOCK}") and SendKeys.Send("{CAPSLOCK}")

Unfortunately, this does not seem to work at all even though.

Those two keys cannot be sent using 'SendKeys.Send' on my machine too
(Windows XP Professional SP2 German, .NET 1.1 SP1).
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 SendInput Lib "user32.dll" (ByVal nInputs As Long,
ByRef pInputs As INPUT, ByVal cbSize As Long) As Long

Notice that 'Long' is a 64-bit data type in .NET. You may want to use
appropriate 32-bit data types instead ('Int32' (= 'Integer'), 'UInt32',
....).

Alternatively, you may want to use p/invoke on
'SetKeyboardState'/'keybd_event' to set the state of these keys, which is
easier than using 'SendInput'. A sample written in VB6 that can be upgraded
to VB.NET can be found here:

<URL:http://dotnet.mvps.org/vb/samples/misc/SpecialKeyState.zip>
 

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