Creating a Keyboard Interface for a Standard Scroll Bar

Although a scroll bar control provides a built-in keyboard interface, a standard scroll bar does not. To implement a keyboard interface for a standard scroll bar, a window procedure must process the WM_KEYDOWN message and examine the virtual-key code specified by the wParam parameter. If the virtual-key code corresponds to an arrow key, the window procedure sends itself a WM_HSCROLL or WM_VSCROLL message with the low-order word of the wParam parameter set to the appropriate scroll bar notification message. For example, when the user presses the up arrow key, the window procedure receives a WM_KEYDOWN message with wParam equal to VK_UP. In response, the window procedure sends itself a WM_VSCROLL message with the low-order word of wParam set to the SB_LINEUP notification message.

The following example shows how to include a keyboard interface for a standard scroll bar.

WORD wScrollNotify = 0xFFFF; 
 
case WM_KEYDOWN: 
    switch (wParam) 
    { 
        case VK_UP: 
            wScrollNotify = SB_LINEUP; 
            break; 
 
        case VK_PRIOR: 
            wScrollNotify = SB_PAGEUP; 
            break; 
 
        case VK_NEXT: 
            wScrollNotify = SB_PAGEDOWN; 
            break; 
 
        case VK_DOWN: 
            wScrollNotify = SB_LINEDOWN; 
            break; 
 
        case VK_HOME: 
            wScrollNotify = SB_TOP; 
            break; 
 
        case VK_END: 
            wScrollNotify = SB_BOTTOM; 
            break; 
    } 
 
    if (wScrollNotify != -1) 
        SendMessage(hwnd, WM_VSCROLL, MAKELONG(wScrollNotify, 0), 0L); 
 
    break;