1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ perl -Mstrict -MTk -w my $mw = tkinit; my $tl = $mw->Toplevel; $tl->geometry('=1x1+0+0'); $tl->withdraw; $mw->bind('<Control-Shift-Key-M>' => sub { $mw->iconify; $tl->deiconify; }); $tl->bind('<Control-Shift-Key-T>' => sub { $mw->deiconify; $tl->withdraw; }); MainLoop;
QuoteSorry to dredge this topic up, but I thought I would post my demo code to create system-wide hotkeys. Feel free to poke holes in my coding style (I wont be listening :) )
Its a modified version of the code that Robert May posted for the generic "bring the window to the front" hotkey code. This version calls a specific routine when the hotkey is pressed. On the 6th press, the code will clean up and exit
I hope this is helpful to someone.
Steve
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#!perl -w use strict; use warnings; use Win32::GUI 1.03_04, qw( WM_HOTKEY VK_A ); use Win32::API; sub HOTKEYF_SHIFT() {0x01} sub HOTKEYF_CONTROL() {0x02} sub HOTKEYF_ALT() {0x04} sub HOTKEYF_EXT() {0x08} my $hotkey_count = 0; # Registering a hotkey requires a Unizue "ID" from the system # Use GlobalAddAtom to request this "ID" my $GlobalAddAtom = Win32::API->new("kernel32","GlobalAddAtom", "P", "L") or die "Failed to load GlobalAddAtom: $!"; # After use, the Atom should be returned to the system pool my $GlobalDeleteAtom = Win32::API->new("kernel32","GlobalDeleteAtom", "L", "L") or die "Failed to load GlobalDeleteAtom: $!"; my $RegisterHotKey = Win32::API->new("user32","RegisterHotKey", "LIII", "I") or die "Failed to load RegisterHotKey: $!"; my $UnregisterHotKey = Win32::API->new("user32","UnregisterHotKey", "LI", "I") or die "Failed to load UnregisterHotKey: $!"; my $mw = Win32::GUI::Window->new( -size => [400,300], -title => "Hotkey $hotkey_count / 5" ); # request an ID or Atom using a unique string my $atom = $GlobalAddAtom->Call("hotkey-string") or die "unable add atom: $!\n"; my $hotkey = VK_A; my $modifier = HOTKEYF_CONTROL | HOTKEYF_ALT | HOTKEYF_SHIFT; unless ($RegisterHotKey->Call($mw->{-handle}, $atom, $modifier, $hotkey)) { print "RegisterHotKey for $atom failed\n"; #if the system fails, you must return Atom to system # because ther loiter around long after your code has died $GlobalDeleteAtom->Call($atom); exit 0; } #now hook the WM_HOTKEY message call unless ($mw->Hook(WM_HOTKEY, \&process_hotkeys)) { print "hook failed\n"; $UnregisterHotKey->Call($mw->{-handle}, $atom); $GlobalDeleteAtom->Call($atom); exit 0; } $mw->Show(); while (Win32::GUI::DoEvents() != -1 and $hotkey_count <= 5) { select(undef, undef, undef, 0.01); } print "6th press of hotkey, clearing up and exiting\n"; #unregister the hotkey $UnregisterHotKey->Call($mw->{-handle}, $atom); #return the atom $GlobalDeleteAtom->Call($atom); exit 0; sub process_hotkeys { my ($object, $wParam, $lParam) = @_; if ($wParam == $atom) { $hotkey_count++; print "found my atom -> $atom\n"; print "Count now $hotkey_count\n"; $mw->Change( -title => "Hotkey $hotkey_count / 5"); } return; }