SetFocus Function

The SetFocus function sets the keyboard focus to the specified window. A subsequent keyboard input message is directed to the specified window, and also to the thread that created the specified window.

BOOL SetFocus(
    HWND hWnd
);

Parameters

Parameter Description
hWnd

A handle to the window that will receive keyboard input. If this parameter is NULL, the system clears the keyboard focus.

Return value

If the function succeeds, the return value is a handle to the window that had keyboard focus before this call. If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

One of the requirements for SetFocus to succeed is that the target window must be visible and enabled. If the target window is owned by another process, the function will succeed only if the target window belongs to a process that has the same user interface privilege level as the calling process.

If the calling process has no other windows that are visible and enabled, SetFocus will set the keyboard focus to the specified window.

If the specified window is a child window, the keyboard focus is set to the specified window. Otherwise, the keyboard focus is set to the top-level window.

Requirements

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header winuser.h
Library User32.lib
DLL User32.dll

See also