473,545 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programmaticall y 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 -524515211930396 2624 as the result.

Declare Function GetMessageExtra Info Lib "user32.dll " alias
"GetMessageExtr aInfo" () As IntPtr

Declare Function GetKeyState Lib "user32" Alias "GetKeyStat e" (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.wSca n = 0
myInput.ki.time = 0
myInput.ki.dwFl ags = 0
myInput.ki.dwEx traInfo = GetMessageExtra Info()

' If Caps Lock is on, send another Caps Lock
If GetKeyState(VK_ CAPSLOCK) = 1 Then
' send CAPSLOCK Key Down
myInput.ki.dwFl ags = 0
myInput.ki.wVk = VK_CAPSLOCK
Dim myResult As Long = SendInput(1, myInput, sizeofINPUT)

' send CAPSLOCK Key UP
myInput.ki.dwFl ags = KEYEVENTF_KEYUP
myInput.ki.wVk = VK_CAPSLOCK
Dim myNextResult As Long = SendInput(1, myInput, sizeofINPUT)
End If
Thanks

Charlie Chang
[ch*********@hot mail.com]

Nov 21 '05 #1
1 15586
<ch*********@ho tmail.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 -524515211930396 2624 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
'SetKeyboardSta te'/'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
4902
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 help. I have seen some example that looks at the characters entered in an input field to determine if the caps lock is on, but I was wondering if...
4
6630
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 tell me why (the hell)it happens that way.
3
7934
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 other special characters. I want my program to detect what key the user pressed from the keyboard and get the associated data from the hash table...
3
6606
by: Mike L | last post by:
How do I turn Caps Lock on, when my form opens?
2
3201
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
1630
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 work, but it only works when the program has the focus. How can I write an app that will do this while running in the background?
1
5694
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 Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = 20 Then MessageBox.Show("Caps Lock is pressed!") End If End...
4
6002
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 UseSystemPasswordChar property is true, and the textbox has focus, if the Caps Lock key is pressed, a warning balloon tooltip is automatically displayed...
1
3153
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)) { MessageBox.Show("caps lock on"); }
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.