# ControlGetFocus

Retrieves which control of the target window has input focus, if any.

    ControlGetFocus, OutputVar [, WinTitle, WinText, ExcludeTitle, ExcludeText]

### Parameters

OutputVar | The name of the variable in which to store the identifier of the control, which consists of its classname followed by its sequence number within its parent window, e.g. Button12.
WinTitle | A window title or other criteria identifying the target window. See WinTitle.
WinText | If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if DetectHiddenText is ON.
ExcludeTitle | Windows whose titles include this value will not be considered.
ExcludeText | Windows whose text include this value will not be considered.

### ErrorLevel

[v1.1.04+] This command is able to throw an exception on failure. For more information, see Runtime Errors.

ErrorLevel is set to 0 if the control with input focus was successfully retrieved. Otherwise (e.g. the target window doesn't exist or none of its controls have input focus) it will be set to 1.

### Remarks

The control retrieved by this command is the one that has keyboard focus, that is, the one that would receive keystrokes if the user were to type any.

The target window must be active to have a focused control. If the window is not active, OutputVar will be made blank.

If ControlGetFocus is executed repeatedly at a high frequency (i.e. every 500 ms or faster), it will probably disrupt the user's ability to double-click. One workaround is to call the OS's GetGUIThreadInfo() via DllCall. For example:

    ; This script retrieves the ahk_id (HWND) of the active window's focused control.
    ; This script requires Windows 98, NT 4.0 SP3+, or any OS more recent than those.
    GuiThreadInfoSize = 48
    VarSetCapacity(GuiThreadInfo, GuiThreadInfoSize)
    NumPut(GuiThreadInfoSize, GuiThreadInfo, 0)
    if not DllCall("GetGUIThreadInfo", uint, 0, str, GuiThreadInfo)
    {
        MsgBox GetGUIThreadInfo() indicated a failure.
        return
    }
    FocusedHWND := NumGet(GuiThreadInfo, 12)  ; Retrieve the hwndFocus field from the struct.
    MsgBox % "The focused control's ahk_id (HWND) is " . FocusedHWND
    ; This ID can be used all control commands.  For example:
    ; ControlGetText, OutputVar,, ahk_id %FocusedHWND%

Window titles and text are case sensitive. Hidden windows are not detected unless DetectHiddenWindows has been turned on.

### Related

ControlFocus, ControlMove, ControlClick, ControlGetText, ControlSetText, ControlSend

### Example

    ControlGetFocus, OutputVar, Untitled - Notepad
    if ErrorLevel
        MsgBox, The target window doesn't exist or none of its controls has input focus.
    else
        MsgBox, Control with focus = %OutputVar%