Jump to content
Sign in to follow this  

GetFocus not working on other windows

Recommended Posts



I tried to get the handle of the control with the focus from another window. I know I can do this with ControlGetFocus, but I just want to do it with the WinAPI. I can get the handle if I start the application with Run(), but if the application already exists I cannot get the handle. I googled a lot, but I didn't find the answer of the problem. Does anybody know how to do this?

#include <WinAPI.au3>

;~ Run("notepad.exe")
Global $hWnd = WinWait("[CLASS:Notepad]", "", 5)
If $hWnd = 0 Then Exit

Global $curThreadID = _Winapi_GetCurrentThreadId();
Global $iPID
Global $winThreadID = _Winapi_GetWindowThreadProcessId($hWnd, $iPID);
ConsoleWrite("PID notepad.exe: " & $iPID & @LF)
Global $success = _WinAPI_AttachThreadInput($curThreadID, $winThreadID, True);
ConsoleWrite("Success: attaching: " & $success & @LF)
Global $hCtrl = _Winapi_GetFocus();
$success = _WinAPI_AttachThreadInput($curThreadID, $winThreadID, False);
ConsoleWrite("Success: detaching: " & $success & @LF)
ConsoleWrite("Control handle with focus: " & $hCtrl & @LF)

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this