Skip to content

Set num lock?

Weiran

Asked May 29, 2006, 2:22 PM EDT1 answer
Share
LinkedInX
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.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,

    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 DLL

    private static extern short GetKeyState(int keyCode);

    4. Declare a variable with the code for the NumLock Key

    private const int VK_NUMLOCK = 0x90;

    5. Declare a state function that will use the external function as source.

    Code
    public bool IsKeyOn(int nKey){
    return (GetKeyState(nKey) & 1) > 0;
    }
    6. Test it

    MessageBox.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
    0