in reply to Reading keyboard input without focus

I think what you're in need of are global mouse and keyboard hooks. I've done this in a number of programs in c, but I doubt it will be as easy in Perl.

Here is the traditional steps to take when writing a global mouse (or keyboard) hook in c

  1. Create a DLL which has an exported function (e.g. createMouseHook(HWND h)). This DLL needs to have a section of shared memory which will be used to store the HWND of the calling program. This function calls SetWindowsHookEx, using WH_MOUSE or WH_KEYBOARD as the hook type. The callback specified must also be in this DLL.
  2. Within the mouse hook callback, use SendMessage to send a WM_COPYDATA message to the window whose HWND was stored during createMouseHook. In a COPYDATASTRUCT struct, store the necessary information to be passed to the program.
  3. In your main program, in your WndProc function, when you encounter a WM_COPYDATA message, do the appropriate processing for that message

These things are fairly trivial to do, but I've never attempted anything like it in Perl. Anyone know if there is an easier way to do this?

Replies are listed 'Best First'.
Re^2: Reading keyboard input without focus
by KovaaK (Initiate) on Nov 24, 2008 at 22:14 UTC
    Ah hah! Thanks to googling for "perl keyboard hook", I came across this! I'm surprised that I didn't think of the word "hook" for this - would have saved me some trouble. This should do the trick once I get some time to sort through it. Thanks much for the lead :)!

      Unfortunately all that code does is register a hot key. It's just a windows API call that enables a program to receive global hot keys (e.g. CTRL+ALT+Z). It won't allow your program to receive all keyboard input.

      There is a good essay on how to set up a global mouse hook over at the code project