http://qs321.pair.com?node_id=539067


in reply to Remapping Keyboard on Win32

Greetings all,

The bad news is that I haven't been able to solve this problem yet. The good news is that my wrist is healing much faster than expected, and I'm now in a smaller splint that frees up my right hand for typing. So I'm abandoning this effort; however, just in case someone else wants to work on this in the future, here's the code I ended up with:

use strict; use warnings; use Win32::API; use Win32::API::Callback; use constant WH_KEYBOARD => 2; use constant LEFT_SHIFT => 0xA0; my $get_thread_id = Win32::API->new('kernel32', 'int GetCurrentThreadI +d()'); my $get_key_state = Win32::API->new('user32', 'GetKeyState', 'I', + 'I'); my $set_hook = Win32::API->new('user32', 'SetWindowsHookEx', 'IKI +I', 'I'); my $call_next = Win32::API->new('user32', 'CallNextHookEx', 'III +I', 'I'); my $unhook_hook = Win32::API->new( 'user32', 'BOOL UnhookWindowsHookEx(int hookHandle)' ); my $hook; my $n = 0; my $callback = Win32::API::Callback->new( sub { $n++; my $next = $call_next->Call($hook, @_); return $next; }, 'III', 'I', ); my $thread_id = $get_thread_id->Call(); print "Thread ID: $thread_id\n"; $hook = $set_hook->Call(WH_KEYBOARD, $callback, 0, $thread_id); for (0 .. 9) { printf "%1d : %3d, %4d\n", $_, $n, $get_key_state->Call( LEFT_SHIFT ) || 0; sleep 1; } my $rv = $unhook_hook->Call($hook);

I primarily used this codefetch.com example along with Win32::API. Although I'm not seeing any errors, I don't think I'm getting the keyboard hook set correctly because $n never increments no matter how many keys I hit while the script runs. The $get_key_state call works just fine, but I can't seem get $callback to get called on keypress. Oh well.

gryphon
Whitepages.com Development Manager (WDDC)
code('Perl') || die;