Examples Delphi

To retrieve the window handle of the currently focused control can easily be done by GetFocs() if it is part of your own process.
For controls in other processes windows you need to attach the input processing mechanism of the thread with the focus to that your own thread. Function AttachThreadInput will attach or detach depending on the last parameter.
You may use this code:

function GetFocussedWindow(ParentWnd:HWnd):HWnd;
var
OtherThread,
Buffer : DWord;
begin
OtherThread := GetWindowThreadProcessID(ParentWnd, @Buffer);
if AttachThreadInput(GetCurrentThreadID, OtherThread, true) then
begin
Result := GetFocus;
AttachThreadInput(GetCurrentThreadID, OtherThread, false);
end
else
Result:=0;
end;