SetFocus

Syntax

HWND SetFocus(hWnd)

This function assigns the input focus to the window specified by the hWnd parameter. The input focus directs all subsequent keyboard input to the given window. The window, if any, that previously had the input focus loses it. If hWnd is NULL, key strokes are ignored.

The SetFocus function sends a WM_KILLFOCUS message to the window that loses the input focus and a WM_SETFOCUS message to the window that receives the input focus. It also activates either the window that receives the focus or the parent of the window that receives the focus.

Parameter Type/Description  

hWnd HWND Identifies the window to receive the keyboard input.  

Return Value

The return value identifies the window that previously had the input focus. It is NULL if there is no such window.

Comments

If a window is active but doesn't have the focus (that is, no window has the focus), any key pressed will produce the WM_SYSCHAR, WM_SYSKEYDOWN, or WM_SYSKEYUP message. If the VK_MENU key is also pressed, the lParam parameter of the message will have bit 30 set. Otherwise, the messages that are produced do not have this bit set.