Solved

SetKeyboardState not working (InterOp, user32.dll)

Posted on 2003-12-02
3
1,450 Views
Last Modified: 2008-12-21
My employer has a problem with the numlock status randomly changing during data entry. I need to write a background app that polls the numlock state regularly, and re-activates it if it is randomly deactivated. I've got the polling part working perfectly, but when setting the state (SetKeyboardState returns true) the lights don't go on / off, and the effect doesn't trigger either. Here's the code I used:

First I import the functions I need from user32.dll:

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
public static extern bool SetKeyboardState( byte[] keys );
            
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
public static extern bool GetKeyboardState( byte[] keys );

Then I read the current state into a byte array:

Byte[] myBytes = new Byte[256];
GetKeyboardState(myBytes);

Set the relevant virtual keys to 'true':

myBytes[144] = 0; // NumLock
myBytes[145] = 0; // ScrollLock (just for testing)
myBytes[20] = 0; // CapsLock (just for testing)

And reload the changed byte array into the keyboard state:

SetKeyboardState(myBytes);

There aren't too many examples on the web, so I'm hoping someone's done this successfully before.
0
Comment
Question by:Rapido
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 70 total points
ID: 9860983
This might help:

HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q177674
0
 

Author Comment

by:Rapido
ID: 9861208
Yeah, that's the function I ended up using. Suppose I asked the question a bit too soon. Works perfectly now. I'll post the code here tomorrow and then I'll give you the points, so we can wrap this up.
0
 

Author Comment

by:Rapido
ID: 9865025
To import the function:

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int extraInfo);

To depress, press, and depress the numlock key:

keybd_event(144, 0, 2, 0);
keybd_event(144, 0, 0, 0);
keybd_event(144, 0, 2, 0);

Turned out you need to depress it first, in case it's still being pressed during the time you want it to reactivate. Strange how MS throws about all kinds of constants, but that there pretty impossible to look up in vs.net. Oh well, problem solved in any case.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now