Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@emmaly
Created April 28, 2023 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmaly/681604b0e82b7bde3baa49ef5a9a197f to your computer and use it in GitHub Desktop.
Save emmaly/681604b0e82b7bde3baa49ef5a9a197f to your computer and use it in GitHub Desktop.
Ensure NumLock is ON via PowerShell
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class NumLock {
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
[DllImport("user32.dll", EntryPoint = "keybd_event", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const byte VK_NUMLOCK = 0x90;
public const int KEYEVENTF_EXTENDEDKEY = 0x1;
public const int KEYEVENTF_KEYUP = 0x2;
public static void EnsureNumLockIsOn() {
short numLockState = GetKeyState(VK_NUMLOCK);
bool numLockIsOn = (numLockState & 0x1) == 0x1;
if (!numLockIsOn) {
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
"@
[NumLock]::EnsureNumLockIsOn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment