Accessing Num Lock state from C#
-
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#.
-
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#.
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? -
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?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.
-
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.
-
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.
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?