By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Programmatically Detect and Send Num Lock and Caps Lock

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
<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.