| Store | Cart

Re: Calling SetWindowsHookEx()?

From: Lyle Kopnicky <l...@veicon.com>
Mon, 19 Jun 2006 11:21:14 -0700
Veli-Pekka Tštilš wrote:
> Lyle Kopnicky wrote:>> I'm trying to capture keyboard events using SetWindowsHookEx(). I've>> tried to set this up using the Win32::API and Win32::API::Callback>> modules. But I'm not getting any events.> Umm, according to MSDn docs your hook code must be in a DLL. See:>> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_hooks32.asp >
Thanks for the info. It appears the code only needs to be in a DLL if 
you want to create a system level hook. I only need a thread level hook.

The key thing I need help with now is the Perl. Listed below is what I 
have so far. It waits for a few seconds, then quits. If I change the 
return type of SetWindowsHookEx from 'P' to 'N', my main window will 
open, but I still don't get any calls to KeyboardHook. It may just be a 
matter of specifying the wrong types for the Win32 functions. Any ideas? 

use warnings;
use strict;
use Tk;
use Win32::API;
use Win32::API::Callback;

my $WH_KEYBOARD_LL = 13;

my $GetCurrentThreadId
    = new Win32::API('kernel32', 'GetCurrentThreadId', '', 'N');
my $SetWindowsHookEx =
    new Win32::API('user32', 'SetWindowsHookEx', 'NKPP', 'P');
Win32::API->Import('user32', 'HHOOK SetWindowsHookEx(int idHook, 
HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId)');
my $CallNextHookEx =
    new Win32::API('user32', 'CallNextHookEx', 'PNNN', 'N');
sub KeyboardHook($$$) {
    my ($nCode, $wParam, $lParam) = @_;
    print "nCode=$nCode, wParam=$wParam, lParam=$lParam\n";
    $CallNextHookEx->Call(0, $nCode, $wParam, $lParam);

my $KeyboardHookCallback =
    new Win32::API::Callback(\&KeyboardHook, 'NNP', 'N');
my $ThreadId = $GetCurrentThreadId->Call();
my $Hook = $SetWindowsHookEx->Call($WH_KEYBOARD_LL, $KeyboardHookCallback,
    0, $ThreadId);

my $win = MainWindow->new();

Lyle Kopnicky
Software Project Engineer
Veicon Technology, Inc.

Recent Messages in this Thread
Lyle Kopnicky Jun 16, 2006 10:40 pm
Veli-Pekka Tštilš Jun 18, 2006 11:46 am
Lyle Kopnicky Jun 19, 2006 06:21 pm
Veli-Pekka Tštilš Jun 20, 2006 04:33 pm
Messages in this thread