This commit is contained in:
Peter 2021-02-08 08:43:03 +08:00
commit 181eb3a89d
4 changed files with 43 additions and 0 deletions

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# `numlock-indicator`
This powershell script adds a small indicator to the system tray to indicate when the numlock is enabled.
Copy files to some directory and run `powershell systray.ps1`. You can also run `start.sh` to ensure that one copy of the script is always active.
I wrote this because for some reason, my laptop keyboard has no LED indicator for the numlock key (yet they have one for capslock and speakers???)
Feel free to replace `icon.png` with any bitmap icon if you don't like my terrible pixel art.

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 B

4
start.sh Normal file
View File

@ -0,0 +1,4 @@
# Ensures one copy of the program is always active. Not necessary for operation.
while true; do
powershell './systray.ps1'
done

30
systray.ps1 Normal file
View File

@ -0,0 +1,30 @@
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
$ICON = [System.Drawing.Icon]::FromHandle(([System.Drawing.Bitmap]::FromFile($(Join-Path $pwd '\icon.png'))).GetHicon()) # Convert my png (bitmap) to an icon
# $ICON = [System.Drawing.Icon]::ExtractAssociatedIcon() # Feel free to use a system icon
function NewTrayIcon(){
$TrayIcon = New-Object System.Windows.Forms.NotifyIcon
$TrayIcon.Text = "NumLock on"
$TrayIcon.Icon = $ICON
$TrayIcon.Add_Click({ # In case our script stops working, allow user to kill icon so that their system tray isn't cluttered
$TrayIcon.Visible = $false
$TrayIcon.Dispose()
Stop-Process $pid
})
return $TrayIcon
}
While ($true) {
While ($TrayIcon.Text) {
If ([console]::numberlock) {
If (!$TrayIcon.Visible) {
$TrayIcon.Visible = $true
}
} ElseIf ($TrayIcon.Visible) {
$TrayIcon.Visible = $false
}
Start-Sleep -Seconds 1 # Set this to whatever polling rate you want.
}
$TrayIcon = NewTrayIcon
}