Created
April 28, 2023 18:36
-
-
Save emmaly/681604b0e82b7bde3baa49ef5a9a197f to your computer and use it in GitHub Desktop.
Ensure NumLock is ON via PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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