Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing Num Lock state from C#

Accessing Num Lock state from C#

Scheduled Pinned Locked Moved C#
csharptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Katalyst
    wrote on last edited by
    #1

    I'd like to be able to determine if the keyboard has "Num Lock" enabled or disabled from C#, as well as to set this state programmatically from C#. I've found some info on how to do this in Win32 in C using "GetKeyboardState" and "keybd_event", but I'm not sure how to call this from C# or if there is a more direct approach in C#.

    N 1 Reply Last reply
    0
    • K Katalyst

      I'd like to be able to determine if the keyboard has "Num Lock" enabled or disabled from C#, as well as to set this state programmatically from C#. I've found some info on how to do this in Win32 in C using "GetKeyboardState" and "keybd_event", but I'm not sure how to call this from C# or if there is a more direct approach in C#.

      N Offline
      N Offline
      Nathan Blomquist
      wrote on last edited by
      #2

      Here is some code, uses Win32 interop using System; using System.ComponentModel; using System.Runtime.InteropServices; public enum SpecialKey { CapsLock = 0x14, NumLock = 0x90, ScrollLock = 0x91 } public class SpecialKeyState { [DllImport("User32.dll")] private static extern short GetKeyState(int nVirtKey); public static bool GetSpecialKeyState( SpecialKey k ) { int s; // get the key state s = GetKeyState((int)k); // check the lowest bit to see if the key is on or off s &= 0x01; // return a boolean if (s == 1) return true; // if on else return false; // if off } } I didn't test this code exactly. It is ripped from a component I made from some other code on the internet. I modified it to let you test any of the speical keys (my version allows you to set a member and then test that key on an interval). Hope this helps, Nathan --------------------------- Hmmm... what's a signature?

      K 1 Reply Last reply
      0
      • N Nathan Blomquist

        Here is some code, uses Win32 interop using System; using System.ComponentModel; using System.Runtime.InteropServices; public enum SpecialKey { CapsLock = 0x14, NumLock = 0x90, ScrollLock = 0x91 } public class SpecialKeyState { [DllImport("User32.dll")] private static extern short GetKeyState(int nVirtKey); public static bool GetSpecialKeyState( SpecialKey k ) { int s; // get the key state s = GetKeyState((int)k); // check the lowest bit to see if the key is on or off s &= 0x01; // return a boolean if (s == 1) return true; // if on else return false; // if off } } I didn't test this code exactly. It is ripped from a component I made from some other code on the internet. I modified it to let you test any of the speical keys (my version allows you to set a member and then test that key on an interval). Hope this helps, Nathan --------------------------- Hmmm... what's a signature?

        K Offline
        K Offline
        Katalyst
        wrote on last edited by
        #3

        Thanks Nathan! That does the trick for getting the num lock state. I'm trying to figure out how to set the num lock now. I found a Win32 example that does this using keybd_event: keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); The signature of this method is: VOID keybd_event( BYTE bVk, // virtual-key code BYTE bScan, // hardware scan code DWORD dwFlags, // function options ULONG_PTR dwExtraInfo // additional keystroke data ); I wasn't exactly sure what types to use for the last two parameters in the extern statement, so I tried "long" and crossed my fingers: [DllImport("User32.dll")] private static extern void keybd_event(byte bVk, byte bScan, long dwflags, long dwExtraInfo); This actually works the first time I call it, but it gets really flaky after that. I figure that I'm not using the right types for the last two parameters, so I'm stomping on memory somehow. Any clue what types I should use? Thanks again.

        K N 2 Replies Last reply
        0
        • K Katalyst

          Thanks Nathan! That does the trick for getting the num lock state. I'm trying to figure out how to set the num lock now. I found a Win32 example that does this using keybd_event: keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); The signature of this method is: VOID keybd_event( BYTE bVk, // virtual-key code BYTE bScan, // hardware scan code DWORD dwFlags, // function options ULONG_PTR dwExtraInfo // additional keystroke data ); I wasn't exactly sure what types to use for the last two parameters in the extern statement, so I tried "long" and crossed my fingers: [DllImport("User32.dll")] private static extern void keybd_event(byte bVk, byte bScan, long dwflags, long dwExtraInfo); This actually works the first time I call it, but it gets really flaky after that. I figure that I'm not using the right types for the last two parameters, so I'm stomping on memory somehow. Any clue what types I should use? Thanks again.

          K Offline
          K Offline
          Katalyst
          wrote on last edited by
          #4

          I also tried unsafe code with a long pointer, but this doesn't work any better than just using longs:

          [DllImport("User32.dll")]
          private unsafe static extern void keybd_event(byte bVk,
          byte bScan,
          uint dwflags,
          ulong* dwExtraInfo);

          1 Reply Last reply
          0
          • K Katalyst

            Thanks Nathan! That does the trick for getting the num lock state. I'm trying to figure out how to set the num lock now. I found a Win32 example that does this using keybd_event: keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); The signature of this method is: VOID keybd_event( BYTE bVk, // virtual-key code BYTE bScan, // hardware scan code DWORD dwFlags, // function options ULONG_PTR dwExtraInfo // additional keystroke data ); I wasn't exactly sure what types to use for the last two parameters in the extern statement, so I tried "long" and crossed my fingers: [DllImport("User32.dll")] private static extern void keybd_event(byte bVk, byte bScan, long dwflags, long dwExtraInfo); This actually works the first time I call it, but it gets really flaky after that. I figure that I'm not using the right types for the last two parameters, so I'm stomping on memory somehow. Any clue what types I should use? Thanks again.

            N Offline
            N Offline
            Nathan Blomquist
            wrote on last edited by
            #5

            Try using IntPtr instead of the long for the ULONG_PTR. I really don't know, perhaps one the people with more experience in Interop will be able to tell us. Later, Nathan --------------------------- Hmmm... what's a signature?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups