Link to home
Start Free TrialLog in
Avatar of dr34m3rs
dr34m3rs

asked on

Perl keyboard hook or trap for out of focus key strokes

I want to write a program in PERL that will be able to detect my keyboard input even if my perl consol window is out of focus. I am creating an implied prompt program so depending on the window in focus I can run scripts based on my key combinations, keywords, and the title text of the window. Sort of a "hidden prompt" that will be very useful to me.

1) I need to be able to get the title bar text of the window in focus.
2) I need to be able to "trap" or "hook" the keyboard input.

I want to use Win32::GUI or maybe Win32::API to get the title text. Just need the code to get the title text of the window in focus by the system.

# Thank you for any help.

Open in new window

Avatar of nedfine
nedfine
Flag of India image

perl script_name.pl explorer

would print the title for all the explorer windows open.
hope this helps.


Thanks
Ned
use strict;
use warnings;
use Win32::API;
use Win32::API::Callback;
 
my $getwindowtext = Win32::API->new( 'user32', 'GetWindowText', 'NPN',+ 'N' );
my $enumwindows = Win32::API->new( 'user32', 'EnumWindows', 'KN', +'N');
 
my $title = shift;
$title = qr/\Q$title\E/i;#
 
my $cb = Win32::API::Callback-> new( 
        sub {
            my $hwnd = shift;
            my $text = " " x 255;
            my $length = $getwindowtext->Call( $hwnd, $text, 255 );
            $text = substr( $text, 0, $length );
            if ( $text =~ $title ) {
                print "$text\n";
            }
            1;
        }, "NN", "N",
    );
my $ret = $enumwindows->Call( $cb, 0 );

Open in new window

Avatar of dr34m3rs
dr34m3rs

ASKER

Hello and thank you for your reply. I would need the title of all the windows open, not just explorer windows.
That is: I would need to know the title of the in focus window, even if it isn't an explorer window. Also need notepad, consol, browser, and so forth.
I came accross this code but am not sure how to impliment it. Any ideas?

It's a system wide keyboard hook which is what I am looking for.

For use with win32::API ?

SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)FilterFunc, GetModuleHandle(NULL), 0);

Can that be used with win32:API and if so, how? Just need a simple example and way to capture keyboard input. Seems simple enough but I need help.
It seems that needs to be in a DLL to be able to hook system wide hooks, else it would only be a thread hook.
Still waiting for more on this...
ASKER CERTIFIED SOLUTION
Avatar of dr34m3rs
dr34m3rs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial