When I saw Get Numlock Status, I started thinking about how to acheive this. I couldn't come up with anything that worked until I started digging into the Windows API.

#!/usr/bin/perl use strict; use warnings; use Tk; use Win32::API; my $GetKeyState = new Win32::API("user32","GetKeyState", ['I'], 'I'); my ($num, $caps, $scroll); my $main = MainWindow->new(); my $form = $main->Frame()->pack(-side => 'top'); my $cl = $form->Frame()->pack(-side => 'left'); my $capslock = $cl->Entry(-width => 8)->pack(-side => 'left'); my $nl = $form->Frame()->pack(-side => 'left'); my $numlock = $nl->Entry(-width => 8)->pack(-side => 'left'); my $sl = $form->Frame()->pack(-side => 'left'); my $scrolllock = $nl->Entry(-width => 8)->pack(-side => 'left'); $main->repeat(100, \&UpdateKeyStates); MainLoop(); sub GetCapsLock { return 0+($GetKeyState->Call(0x14) != 0); } sub GetNumLock { return 0+($GetKeyState->Call(0x90) != 0); } sub GetScrollLock { return 0+($GetKeyState->Call(0x91) != 0); } sub UpdateEntry { my $entry = shift; my $text = shift; $entry->delete(0, 'end'); $entry->insert(0, $text); } sub UpdateKeyStates { $caps = GetCapsLock() ? 'Cap' : ''; $num = GetNumLock() ? 'Num' : ''; $scroll = GetScrollLock() ? 'Scroll' : ''; UpdateEntry($capslock, $caps); UpdateEntry($numlock, $num); UpdateEntry($scrolllock, $scroll); $main->title($caps.$num.$scroll); }

Replies are listed 'Best First'.
Re: Show Status of Caps, Num, and ScrollLock on Win32
by Mr. Muskrat (Canon) on Jul 07, 2003 at 18:10 UTC

    I wrote this on a Windows XP system and tested it on Windows XP and Windows 2000. This morning after I posted it, I tried it on Windows 98. I noticed an interesting behaviour. While the Tk window has the focus, I can press and release the CapsLock or ScrollLock key and it will update in the Tk window but the LEDs on the keyboard will only flash on and off. However, if the Tk window is minimized the LEDs functions normally but the Tk window does not update. NumLock is unaffected. I have not seen this behaviour on the Windows XP or 2000 systems.