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

LVL 1
dr34m3rsAsked:
Who is Participating?
 
dr34m3rsAuthor Commented:
The answer to this question is simple: Use another programming language like C++. :-)
0
 
nedfineCommented:
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

0
 
dr34m3rsAuthor Commented:
Hello and thank you for your reply. I would need the title of all the windows open, not just explorer windows.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dr34m3rsAuthor Commented:
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.
0
 
dr34m3rsAuthor Commented:
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.
0
 
dr34m3rsAuthor Commented:
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.
0
 
dr34m3rsAuthor Commented:
Still waiting for more on this...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.