• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1065
  • Last Modified:

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

0
dr34m3rs
Asked:
dr34m3rs
  • 6
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now