Solved

SetKeyboardState not working (InterOp, user32.dll)

Posted on 2003-12-02
3
1,464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

726 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