in reply to Windows Hotkey

Yet another try. Of coures i'm not even sure i'm calling RegisterHotKey correctly ;(

#!/usr/bin/perl $| =1; use warnings; use strict; use Data::Dumper; use Win32::GUI; use Win32::API; my $proto = 'BOOL RegisterHotKey(HWND hWnd, int id, UINT fsModifiers, +UINT vk)'; Win32::API->Import('user32', $proto) or die "import RegisterHotKey: $! + ($^W)"; my $mw_win32 = new Win32::GUI::Window( -title => 'This is a test!', -width => 100, -height => 100, -name => 'MainWindow'); $mw_win32->Show(); # Call: my $ret = RegisterHotKey ($mw_win32->{-handle}, 10, 11 , 41); if (not $ret) { die "RegisterHotKey: $! ($^E)"; } print "Return: $ret\n"; $mw_win32->Hook( 273, sub {print "TEST"}); while (1) { Win32::GUI::DoEvents(); my $x = [$mw_win32->PeekMessage()]; print Dumper($x) if @$x > 1; }

This still doesn't work, in fact the PeekMessage's doesn't appear to do anything at all! And the hook never fires either.


___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: Windows Hotkey
by Ace128 (Hermit) on Jul 12, 2006 at 05:17 UTC
    Is this the same as a "Global Key"? Key that should work even if window is not in focus?

      Yes that is my goal. Create a hotkey that can do stuff even when the window is not in focus. /me begins searching for GlobalKey to see if that is different! ;)


      ___________
      Eric Hodges
        Well, then this is interesting to me aswell :) Wanted to be able of doing this while back... Gotta check it out more...

        Found this on my hd. Not working properly.. dunno why exactly:
        #!/usr/bin/env perl use Data::Dumper; use strict; use warnings; #use diagnostics; use Win32::API; use Win32::API::Callback; sub FILE_FLAG_BACKUP_SEMANTICS { 0x02000000 } sub WH_KEYBOARD { 2 } sub WH_KEYBOARD_LL { 13 } sub DWORD_SIZE { 4 } #my $kernel32 = 'kernel32.dll'; #my $advapi32 = 'advapi32.dll'; # HHOOK SetWindowsHookEx( # # int idHook, # HOOKPROC lpfn, # HINSTANCE hMod, # DWORD dwThreadId # ); # lpfn # [in] Pointer to the hook procedure. If the dwThreadId parameter +is zero or specifies # the identifier of a thread created by a different process, +the lpfn parameter must point # to a hook procedure in a dynamic-link library (DLL). Otherw +ise, lpfn can point to a hook # procedure in the code associated with the current process. # hMod # [in] Handle to the DLL containing the hook procedure pointed to +by the lpfn parameter. # The hMod parameter must be set to NULL if the dwThreadId pa +rameter specifies a thread # created by the current process and if the hook procedure is + within the code associated # with the current process. # dwThreadId # [in] Specifies the identifier of the thread with which the hook +procedure is to be associated. # If this parameter is zero, the hook procedure is associated + with all existing threads # running in the same desktop as the calling thread. # LRESULT CALLBACK LowLevelKeyboardProc( # int nCode, # WPARAM wParam, # LPARAM lParam # ); my $user32 = 'user32.dll'; my $callback = Win32::API::Callback->new( sub { my($a, $b) = @_; return $a+$b; }, "INN", "N", ); my $SetWindowsHookEx = new Win32::API($user32, 'SetWindowsHookEx', 'IPNN', 'I'); print Dumper($SetWindowsHookEx); if (!$SetWindowsHookEx) { print $^E . "\n"; print Win32::GetLastError(); exit; } my $idHook = WH_KEYBOARD_LL; my $lpfn = \&hello; #pack("L", 0); my $hMod = 0; my $dwThreadId = 0; my $ok = $SetWindowsHookEx->Call( $idHook, $lpfn, $hMod, $dwThreadId ) or die $^E; print Win32::GetLastError(); sub hello { }
        "Doesn't work to set a non-local hook without a modulereferense (handle) at globalKeysWin32.pl line 79, <DATA> line 164."
        Whatever that means... may be helpfull? Please tell me if you make it work!

        Ace