3.4. CBT-hooks Follow the Focus and Changes in the Active Window

CBT stands for computer based training which indicates the primary usage for this type of hook. You can get a wide variety of information from a CBT hook including changes of active window or keyboard focus as well as window minimizing, creation and deletion events. We'll concentrate on the essential events regarding accessibility that is active window and focus changes. The term focus refers to the active control receiving the keyboard input.

Tracking the focus is one of the key ingredients for efficient screen reading because the user can concentrate on moving around the GUI without having to invoke special read commands manually. A CBT-hook function gets notified of a focus change by the hook code value HCBT_SETFOCUS. Additionally, wParam is a handle to the window getting the focus and lParam the window losing it respectively. IF the user-defined hook function returns true, focus isn't given to the window whose handle is in wParam. In the example app, the focus change is handled the following way:

if(ncode == HCBT_SETFOCUS && GetParent((HWND) wParam) != hObserver  && ((HWND) wParam != hObserver))
{ // Handle focuschange.
	lstrcat(textOut, "Focus ");
	handleControls((HWND) wParam);
	lstrcat(textOut, "\r\n");
	notifyObserver();
} // if
The if statement makes sure that the window getting the focus is not the main program of the window or any of it's children. The reason for this check is practical, the text field would fill up too quickly if the program reported it's own output. lstrcat is the Windows equivalent of the C standard library function strcat joining two strings.

Merely tracking the focus isn't really enough for following all significant events in the GUI. The state changes in a control such as a chekc box don't usually generate a focus change, because the window having the focus remains the same (it is the same check box). Also, focus doesn't change when you move in a menu probably because menus are resources and not windows.

The code HCBT_ACTIVATE for a CBT-hook indicates that a window having a title bar has been activated (it's title bar has been changed to active). wParam is again a handle to the window being activated and lParam provides a pointer to a struct giving extra information. Again as was the case with HCBT_SETFOCUS, a true return value prevents the destination window from getting activated.

Back to the Contents