Solved

SetKeyboardState not working (InterOp, user32.dll)

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

7 Experts available now in Live!

Get 1:1 Help Now