Turning on the NumLock Key

  • Thread starter Thread starter James Kirkup
  • Start date Start date
J

James Kirkup

Hello,

Does anyone know in code how to turn the NumLock key on in C#?

with regards
James
 
Thanks Punz,

I have seen that link and was hoping for a more straight forward
solution. Tried your suggestion line but unfortunately that did not
work. Thanks for posting anyway.

with regards
 
James Kirkup said:
Hello,

Does anyone know in code how to turn the NumLock key on in C#?

No support in the FCL, so you will have to Pinvoke, here's a working
sample....

using System;
using System.Runtime.InteropServices;

namespace Willys
{
class Tester
{
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal int type;
internal short wVk;
internal short wScan;
internal int dwFlags;
internal int time;
internal IntPtr dwExtraInfo;
int dummy1;
int dummy2;
internal int type1;
internal short wVk1;
internal short wScan1;
internal int dwFlags1;
internal int time1;
internal IntPtr dwExtraInfo1;
int dummy3;
int dummy4;


}
[DllImport("user32.dll")]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);


static void Main()
{
const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
INPUT input = new INPUT();
input.type = 0x01; //INPUT_KEYBOARD
input.wVk = 0x90; //VK_NUMLOCK
input.wScan = 0;
input.dwFlags = 0; //key-down
input.time = 0;
input.dwExtraInfo = IntPtr.Zero;

input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1= 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraInfo1 = IntPtr.Zero;

IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the
MOUSEINPUT tag !!!

if (result == 0 || Marshal.GetLastWin32Error() != 0)
Console.WriteLine(Marshal.GetLastWin32Error());
Marshal.FreeHGlobal(pI);
}
}
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top