473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 15532
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Robert | last post by:
Hi! I was wondering if the was any way to determine the state of the caps lock key, on or off. Of course I can capture the key events and see whether the caps lock is pressed, but that does not...
4
by: Peter D | last post by:
I have a second hand bar code reader (keyboard wedge) en i can read the bar codes but after every scan he turns my caps lock on. (GRRRRRR). I search a code to turn my caps lock off or can anyone...
3
by: Tegdeep | last post by:
Here's what I want to do: I have a hash table which contains data associated to different keys. The Hash keys are represented by a single character from the keyboard: 0-9, a-z, A-Z, and the...
3
by: Mike L | last post by:
How do I turn Caps Lock on, when my form opens?
2
by: Woody Splawn | last post by:
Could someone tell me what code I run to determine if the user's numlock is on? Is there code I can run to set the numlock to on?
0
by: fNew VBer | last post by:
Hi. I'm new to Visual Basic .NET, so you'll have to bear with me . . . I want to create an app that turns off CAPS LOCK when the SHIFT key is pressed. I got the code to turn off CAPS LOCK to...
1
by: yxq | last post by:
Hello, I have a main form, there other some controls(i.e. textbox) in the form. I want to detect whether Caps Lock key is pressed in form-key event. Private Sub Form1_KeyUp(ByVal sender As...
4
by: zacks | last post by:
I'm not sure when it came into being, since this it the first time I have worked very much with a password control in .NET, but if a textbox that has a non-empty value for PasswordChar or the...
1
by: cmrhema | last post by:
Hi, Two questions 1. In windows application if we put on the below code, it will identify whether caps lock is on or not if (TextBox.IsKeyLocked(Keys.CapsLock)) { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.