SetFocus

  HWND SetFocus(hwnd)    
  HWND hwnd; /* handle of window to receive focus */

The SetFocus function sets the keyboard focus to the given window. All subsequent keyboard input is directed to this window. The window, if any, that previously had the keyboard focus loses it.

Parameters

hwnd

Identifies the window to receive the keyboard input. If this parameter is NULL, keystrokes are ignored.

Return Value

The return value identifies the window that previously had the keyboard focus, if the function is successful. It is NULL if there is no such window or if the hwnd parameter is invalid.

Comments

If the window identified by the hwnd parameter was created by the calling thread, the keyboard focus status of the calling thread is set to hwnd. Otherwise, the keyboard focus status of the calling thread is set to NULL, and the keyboard focus status of the thread that created the window is set to hwnd.

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

If a window is active but does not 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.

See Also

GetActiveWindow, GetFocus, SetActiveWindow, SetCapture, WM_KILLFOCUS, WM_SETFOCUS, WM_SYSCHAR, WM_SYSKEYDOWN, WM_SYSKEYUP