Sorry 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
#!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;
        }
        
        

Reply via email to