Solved

SetKeyboardState not working (InterOp, user32.dll)

Posted on 2003-12-02
3
1,452 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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