C#: Detecting which application has focus
Asked Answered
H

3

21

I'm looking to create a C# application that changes content according to which application currently has focus. So if the user is using Firefox, my app would know that. Same for Chrome, Visual Studio, TweetDeck etc.

Is this possible, and if so - how would I go about achieving it?

I have a feeling I'm asking for to much - but it's worth a try.

Many thanks in advance.

Hambrick answered 2/2, 2010 at 11:17 Comment(2)
Use the accessibility interfaces. This is exactly the sort of thing they are for.Inoperative
The accepted answer to this question expands upon @RaymondChen's comment: #11711900Biak
H
5

Take a look at Application.AddMessageFilter, and look for WM_ACTIVATEAPP messages, which will tell you when an app is activated, i.e. receives focus.

Hux answered 2/2, 2010 at 11:28 Comment(1)
Application.AddMessageFilter only intercepts messages for the current process, not other processes.Abdomen
A
13

This can be done in pure .NET using the Automation framework that is part of the WPF. Add references to UIAutomationClient and UIAutomationTypes and use Automation.AddAutomationFocusChangedEventHandler, e.g.:

public class FocusMonitor
{
    public FocusMonitor()
    {
        AutomationFocusChangedEventHandler focusHandler = OnFocusChanged;
        Automation.AddAutomationFocusChangedEventHandler(focusHandler);
    }

    private void OnFocusChanged(object sender, AutomationFocusChangedEventArgs e)
    {
        AutomationElement focusedElement = sender as AutomationElement;
        if (focusedElement != null)
        {
            int processId = focusedElement.Current.ProcessId;
            using (Process process = Process.GetProcessById(processId))
            {
                Debug.WriteLine(process.ProcessName);
            }
        }
    }
}
Abdomen answered 16/9, 2014 at 19:30 Comment(0)
H
6

Grrr. As is often the way, I spent some time Googling before posting this question.

Once I finally posted the question, my next Google search revealed the answer.

I've yet to test it, but it looks as though GetForegroundWindow() is the key.

Rather than me rewrite what's already written, here's a link to the page that provided the information:

http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/

Appologies for anyone's time I've wasted by asking a Googalable (?) answer.

Hambrick answered 2/2, 2010 at 11:27 Comment(2)
AddMessageFilter has the advantage to be purely managed code, GetForegroundWindows is less "heavy" (you check the foreground windows when you need, instead of being notified for every application change) but needs PInvoke. You have only to choose now ;)Quadrillion
Link rot is a thing.Sizzle
H
5

Take a look at Application.AddMessageFilter, and look for WM_ACTIVATEAPP messages, which will tell you when an app is activated, i.e. receives focus.

Hux answered 2/2, 2010 at 11:28 Comment(1)
Application.AddMessageFilter only intercepts messages for the current process, not other processes.Abdomen

© 2022 - 2024 — McMap. All rights reserved.