Check out
Continueing the Quest to get RegisterHotKey to work!
#This works! We have global keyboard hook for hotkey, ctrl+a.
use strict;
use warnings;
use Win32::GUI;
use Win32::API;
use Win32::API::Callback;
$| = 1;
# From PlatformSDK/Include/WinUser.h
use constant MOD_CONTROL => 2;
use constant KEY_A => ord('A');
use constant MY_HOTKEY_ID => 99;
use constant {
# Codes for SetWindowHook
WH_GETMESSAGE => 3,
WH_KEYBOARD => 2,
# Window Messages
WM_HOTKEY => 0x0312,
### # Hook Codes
### HC_ACTION => 0,
### HC_GETNEXT => 1,
### HC_SKIP => 2,
### HC_NOREMOVE => 3,
### HC_SYSMODALON => 4,
### HC_SYSMODALOFF => 5,
###
### # PeekMessage() Options
### PM_NOREMOVE => 0x0000,
### PM_REMOVE => 0x0001,
### PM_NOYIELD => 0x0002,
};
sub WindowProc {
my ( $nCode, $wParam, $lParam ) = @_;
# See http://msdn.microsoft.com/library/en-us/winui/winui/windowsu
+serinterface/windowing/messagesandmessagequeues/messagesandmessageque
+uesreference/messageandmessagequeuestructures/msg.asp
# typedef struct {
# HWND hwnd;
# UINT message;
# WPARAM wParam; # Unsigned
# LPARAM lParam; # Signed
# DWORD time;
# POINT pt;
# } MSG, *PMSG;
my ( $msg_hwnd, $msg_message, $msg_wParam, $msg_lParam, $msg_time,
+ $msg_pt )
= unpack 'L L L l L L ', unpack( 'P24', pack('L', $lParam) );
# print "\n\nTime:", $msg_time;
# print "\nMsg_lparam", $msg_lParam;
#Hotkey code to run
if ( $msg_message == WM_HOTKEY ) {
#print "\a**************************\n";
system("notepad.exe");
}
print "\nMessage:", $msg_message;
CallNextHookEx( 0, $nCode, $wParam, $lParam );
}
my $WinProc = Win32::API::Callback->new( \&WindowProc, 'NNN', 'N' );
Win32::API->Import( kernel32 => GetCurrentThreadId => '', 'N' ) or
+ die;
Win32::API->Import( user32 => SetWindowsHookEx => 'NKNN', 'N' ) or
+ die;
Win32::API->Import( user32 => CallNextHookEx => 'PNNN', 'N' ) or
+ die;
Win32::API->Import( user32 => RegisterHotKey => 'NNNN', 'N' ) or
+ die;
Win32::API->Import( user32 => UnregisterHotKey => 'NN', 'N' ) or
+ die;
my $ThreadId = GetCurrentThreadId() or die;
print "\nThreadID:", $ThreadId;
SetWindowsHookEx( WH_GETMESSAGE, $WinProc, 0, $ThreadId ) or die $^E;
#SetWindowsHookEx( WH_KEYBOARD, $WinProc, 0, $ThreadId ) or die $^E;
my $mw_win32 = Win32::GUI::Window->new(
-name => 'MainWindow',
-title => 'This is a test!',
-width => 100,
-height => 100,
-pos => [ -400, -400 ]
);
$mw_win32->Show();
$mw_win32->Hide();
my $mw_handle = $mw_win32->GetActiveWindow();
RegisterHotKey( $mw_handle, MY_HOTKEY_ID, MOD_CONTROL, KEY_A ) or die
+$^E;
Win32::GUI::Dialog();
# print "done";
UnregisterHotKey( $mw_handle, MY_HOTKEY_ID ) or die $^E;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.