Find handle of previous active window (win32)

I'm trying to write a small win32 application (in C or C++) that sends pre-cooked keystrokes to another application. The user experience should be this:

The user works with application X (which can be anything), then uses the mouse to click somewhere in the window belonging to application Y (written by me), which is visible on the screen. Application Y uses SendInput to send keystrokes to application X.

The problem is: SendInput sends keystrokes to the foreground window. How can application Y determine which window was the foreground window immediately before it received the mouse click?

However, if that does not work for you, you might want to consider setting a global hook (see http://msdn.microsoft.com/en-us/library/ms997537.aspx - "Win32 Hooks") that filters for WM_KILLFOCUS and sends the handle of the window that message was targetted at to your app. E.g.

Featured Post

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.

This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel.
Part 1 of this series discussed basic error handling code using VBA.
http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you!
In this Micro Tutorial, you'll learn yo…