Set num lock?
Hi,
I have a rather simple question that I can't seem to find the answer to, but is it possible to set the status of the num lock in a Windows application?
Thanks.
Weiran.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Darren MarshallPosted May 30, 2006, 12:41 AM
I found this post: http://www.codecomments.com/archive290-2005-5-489956.html
I tried it and it works, all you need to do is...
1. libs:
using
System.Runtime.InteropServices;2. DLLImport[
DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]3. Declare the function you want to use from the DLLprivate
static extern short GetKeyState(int keyCode);4. Declare a variable with the code for the NumLock Keyprivate
const int VK_NUMLOCK = 0x90;5. Declare a state function that will use the external function as source.public
bool IsKeyOn(int nKey){ return (GetKeyState(nKey) & 1) > 0;}
6. Test itMessageBox
.Show("State: " + (IsKeyOn(VK_NUMLOCK) ? "YES" : "NO"));Not sure if there is an easier way since this was posted in 2005 and may not be 2.0 framework, but i hope this helps,
Darren