471,309 Members | 1,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 software developers and data experts.

Programmatically Detect and Send Num Lock and Caps Lock

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]

Nov 21 '05 #1
1 14817
<ch*********@hotmail.com> schrieb:
[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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Robert | last post: by
4 posts views Thread by Peter D | last post: by
3 posts views Thread by Mike L | last post: by
2 posts views Thread by Woody Splawn | last post: by
4 posts views Thread by zacks | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.