Perl keyboard hook or trap for out of focus key strokes

Posted on 2009-02-22
Last Modified: 2012-05-06
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

Question by:dr34m3rs
    LVL 8

    Expert Comment

    perl explorer

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

    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";
            }, "NN", "N",
    my $ret = $enumwindows->Call( $cb, 0 );

    Open in new window

    LVL 1

    Author Comment

    Hello and thank you for your reply. I would need the title of all the windows open, not just explorer windows.
    LVL 1

    Author Comment

    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.
    LVL 1

    Author Comment

    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.
    LVL 1

    Author Comment

    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.
    LVL 1

    Author Comment

    Still waiting for more on this...
    LVL 1

    Accepted Solution

    The answer to this question is simple: Use another programming language like C++. :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now