Question

I know how to get the title and exe name of the foreground window application that is running now, but I use a TTimer to verify when it changes.

Is there a way to detect events triggered by Alt+Tab, taskbar application selection or even the start of a new program?

I use Delphi 2006 and Windows 7 64 bits.

Was it helpful?

Solution

One option is to install a global hook. With a CBT hook, the system will call the hook procedure whenever a window is activated (among other things). A global hook callback is to be placed in a dll which gets loaded in the address space of processes, hence it can get mapped into only processes having the same 'bit'ness (using Delphi 2006, the callback will be called only by 32 bit processes). Moreover, it cannot be mapped into address space of a process that is created with a higher privilege (i.e. an application that is run as administrator if the process installing the hook is not). You also have to devise some kind of interprocess communication mechanism since your callback runs in other applications. You use SetWindowsHookEx to install a global hook.

One other option is using event hooks, that's SetWinEventHook. There are two kinds, in-context and out-of-context. The former one, like a global hook, is placed in a dll to be mapped into address spaces of other processes, so you'll suffer from same downsides. Out-of-context events are the most relaxed of all. They wouldn't be prompt as global hooks or in-context events when notifying but I believe that could still be better then a timer. One disadvantage of events to hooks in your context is that, you would have to code a bit more in the callback, f.i. you'll receive a window focus notification even for child windows and you would have to resolve which application it belongs to etc..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top