Hi,
Has anyone gotten the system wide RegisterHotkey and  UnrEgister hotkey
Win32 functions working with Win32::GUI and Win32::API? I just tried but
my event handler never gets called. Any help as to why this is would be
appreciated. I hope it's some quirk in my own code I just haven't
noticed.

My understanding is this:
YOu call RegisterHotkey with a window handle via which virtual keycode
messages should be posted, a unique ID for that window handle to
distinguish multiple key handlers, a list of modifier keys that need to
be down and finally a virtual keycode to match for the posting to happen
in the first place. UnregisterHotkey is much the same except that only
the first two params are needed. 

The first problem I had was having to manually dig up constants like
MOD_WIN from winuser.h (VC2005 Express), it appears to be missing in the
listing in Win32::GUI::Constant. The second thing was that I couldn't
find a method of retrieving the Win32 handle from a Win32::GUI::Window
without having to look at its internals. Thirdly and finally, my event
handler is never called. This might either be some glich in importing
the functions, registering the hotkey handlers or adding event handling
hooks to Win32::GUI, which I've never done before.

Here's the whole code, for your information. What it does is create a
dummy, currently visible window, register a hotkey handler for
shift+ctrl+q, and if that handler is triggered, then prints out the
params in the console:


use strict; use warnings;

use constant {MOD_CONTROL => 2, MOD_SHIFT => 4}; # HOtkey modifiers from
winuser.h

use constant qw|HOTKEY_ID 32|; # Almost any int will do.

use Win32::GUI qw||; use Win32::GUI::Constants qw|WM_HOTKEY|;
use Win32::API;

# Import the funcs use to register system wide hotkey notifications.
Win32::API->Import
(
   user32 => # target window, some id, modifiers, virtual key code.
   'BOOL RegisterHotKey(HWND w, int id, UINT mod, UINT vk)'
);

Win32::API->Import
(
   user32 =>
   'BOOL UnregisterHotKey(HWND w, int id)'
);

my $win = Win32::GUI::Window->new
(
   -name => 'main', -text => 'main', -size => [320, 240],
   -onTerminate => \&quit
);
$win->Show();


# Process the virtual key codes sent to us.
$win->Hook(WM_HOTKEY, \&key); 

# Register hotkey, for alphanumerics ASCIi code is virtual key code.
RegisterHotKey
(
   $win->{'-handle'}, HOTKEY_ID,
   MOD_SHIFT | MOD_CONTROL, 
   ord 'q' 
) or die "HOtkey registration failed: $^E\n";

Win32::GUI::Dialog();

sub quit
{ # Unregister hotkey handler and quit.
   UnregisterHotKey($win->{'-handle'}, HOTKEY_ID) or die "Unregistration
failed: $^E\n";
   -1;
} # sub

sub key
{ # Just a debug print of the key postedd to us.
   print "Key with params: @_\n"; 
   1
} # sub

Finally, a cool solution to the hotkey problem and many other uses, in
which hooks are desired would be this:

Create a DLL in C which does the actual hooking and is somewhat
customizable in terms of what it posts back to its user i.e. functions
to specify how to match params and which window receives the message.

Post the desired data to a window registered with the DLL using
WM_COPYMESSAGE.
Then use Win32::API to wrap that DLL and have a possibly invisible
Win32::GUI::Window receive the things that were hooked.

Even better would be a Perl module which encapsulates having to create
the window and wrap the DLL.

There are plenty of code examples for hooks in the code project.
Googling for Win32 hooks produces some nice links, for instance. I've
used hooks a bit in screen reading but the code is way too ugly to be
published, <smile>. And for screen reading, Win32::ActAcc is infinitely
better.


-- 
With kind regards Veli-Pekka Tätilä ([EMAIL PROTECTED])
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to