Focus, Focus, Who's Got the Focus?

The keyboard must be shared by all applications running under Windows. Some applications may have more than one window, and the keyboard must be shared by these windows within the same application. When a key on the keyboard is pressed, only one window procedure can receive a message that the key has been pressed. The window that receives this keyboard message is the window with the ”input focus.“

The concept of input focus is closely related to the concept of ”active window.“ The window with the input focus is either the active window or a child window of the active window. The active window is usually easy to identify. If the active window has a caption bar, Windows highlights the caption bar. If the active window has a dialog frame (a form most commonly seen in dialog boxes) instead of a caption bar, Windows highlights the frame. If the active window is an icon, Windows highlights the window's caption bar text below the icon.

The most common child windows are controls such as push buttons, radio buttons, check boxes, scroll bars, edit boxes, and list boxes that usually appear in a dialog box. Child windows are never themselves active windows. If a child window has the input focus, then the active window is its parent. Child window controls indicate that they have the input focus generally by using a flashing cursor or caret.

If the active window is an icon, then no window has the input focus. Windows continues to send keyboard messages to the icon, but these messages are in a different form from keyboard messages sent to active windows that are not icons.

A window procedure can determine when it has the input focus by trapping WM_SETFOCUS and WM_KILLFOCUS messages. WM_SETFOCUS indicates that the window is receiving the input focus, and WM_KILLFOCUS signals that the window is losing the input focus.