Skip to content

Instantly share code, notes, and snippets.

@json-m
Created July 25, 2021 00:29
Show Gist options
  • Save json-m/aa83b3510bdd5403733d76936d306c11 to your computer and use it in GitHub Desktop.
Save json-m/aa83b3510bdd5403733d76936d306c11 to your computer and use it in GitHub Desktop.
numHardLock - persist number lock
package main
/*
for some reason my number lock kept getting turned off and i couldn't figure out why
so i made this to check every 250ms to see if my number lock is off, and if it is, turn it back on
*/
import (
"github.com/gonutz/w32"
"log"
"os"
"time"
)
func init() {
log.SetOutput(os.Stdout)
log.Println("numHardLock starting")
}
func numLockWatcher() {
// get key state
keyState := w32.GetKeyState(w32.VK_NUMLOCK)
// conditions here
if keyState == 0 {
log.Println("numLock was OFF, turning it back ON")
_ = w32.SendInput(w32.KeyboardInput(w32.KEYBDINPUT{
Vk: w32.VK_NUMLOCK,
Scan: 0,
Flags: 0,
Time: 0,
ExtraInfo: 0,
}))
}
}
func main() {
done := make(chan bool)
// main watch thread
go func() {
for {
numLockWatcher()
time.Sleep(250 * time.Millisecond)
}
}()
<-done
log.Println("numHardLock exiting")
}
@json-m
Copy link
Author

json-m commented Jul 25, 2021

obviously, based on the above code this is only for Windows.

for startup on logon, I use PowerShell via Task Scheduler to hide the command prompt window. i had to use "Run only when user is logged on", and selected my user.

powershell Start-Process -WindowStyle hidden -FilePath c:\numHardLock.exe

note: the Task Scheduler action will need Run with highest privileges checked if you need this to work while a program running as admin has focus. without it, it will only re-enable numlock once window focus changes to something without admin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment