Win32::Shortkeys::Kbh - Perl extension for hooking the keyboard on windows
0.01
use Win32::Shortkeys::Kbh qw(:hook :input VK_BACK VK_TAB); set_key_processor(sub { my ($cup, $code, $alt, $ext) = @_; # return on keypressed return if $cup == 0; print "process_key in perl cup: $cup code: $code alt: $alt ext: $ext \n"; if ($code == 123) { unregister_hook(); quit(); } if ($code == 83) { sleep 1; #or usleep from Time::HiRes for shorter time unregister_hook(); send_cmd(1, VK_BACK); #erase the key pressed send_string("You hit the s key !!!"); #and send something else register_hook(); } }); register_hook(); msg_loop();
This module uses Win32 API fucntions to create a keyboard hook with register_hook. A sub ref is pass to the hook with set_key_processor. This sub is called on key press and key release. It receives the key code and can react using seng_string or send_cmd.
register_hook
set_key_processor
seng_string
send_cmd
If you run the code (also in example/example.pl) in the synopsis and if you open the notepad any keys except the s will be print out normaly. When the s key is relead the keyboard will print 'You hit the s key'. F12 will terminate the script.
set_key_processor( $my_sub_ref )
The sub passed to this function received the following parameters
Indicates if the key is released ($cup > 0) or pressed ($cup == 0)
The virtual key code of the key
Indicates if the alt key is pressed ($alt > 0) or not ($alt == 0)
Indicates ($ext == 1) if extended-key flag is set or not ($ext == 0). Following ms windows documentation this flag is set if one of the additional keys on the enhanced keyboard is used. The extended keys consist of the ALT and CTRL keys on the right-hand side of the keyboard; the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; the NUM LOCK key; the BREAK (CTRL+PAUSE) key; the PRINT SCRN key; and the divide (/) and ENTER keys in the numeric keypad.
unregister_hook
msg_loop
calls this to prevent your script from leaving and use next function to quit.
quit
send_cmd($how_much, $vk_code)
VK_CODE are exported by this module and can be use as constant in the perl code.
send_string("bla")
C<register_hook>. Send string (C<send_string("bla")>or commands C<send_cmd(how_much, VK_TAB)> to the keyboard:
None by default.
The following export tags are defined to import on request:
input for send_string send_cmd paste_from_clpb
input
send_string send_cmd paste_from_clpb
hook for register_hook unregister_hook msg_loop set_key_processor qui
hook
register_hook unregister_hook msg_loop set_key_processor qui
vkcode to import all the VK_CODE defined
vkcode
all to import everything.
all
The code from the SYNOPSIS above can be run from the example/example.pl script.
To install this module type the following: perl Makefile.PL make make test make install
On windows use nmake or dmake instead of make.
Win32::Shortkeys
Questions or problems can be posted to me (rappazf) on my gmail account.
The current state of the source can be extract using Mercurial from
http://sourceforge.net/projects/win32-shortkeys-kbh/.
François Rappaz <rappazf@gmail.com>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
To install Win32::Shortkeys::Kbh, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::Shortkeys::Kbh
CPAN shell
perl -MCPAN -e shell install Win32::Shortkeys::Kbh
For more information on module installation, please visit the detailed CPAN module installation guide.