zhuang@linux:~/reading/programming-windows/06-the-keyboard/$ less
The Keyboard
This post extracts some knowledge from Programming Windows Chapter 6 – The Keyboard.
There are eight different messages that Windows uses to indicate various keyboard events.
WM_KEYDOWN, WM_KEYUP
WM_CHAR, WM_DEADCHAR
WM_SYSKEYDOWN, WM_SYSKEYUP
WM_SYSCHAR, WM_SYSDEADCHARControl Character Processing
The basic rule for processing keystroke and character messages is this:
If you need to read keyboard character input in your window, you process the WM_CHAR message.
If you need to read the cursor keys, function keys, Delete, Insert, Shift, Ctrl, and Alt, you process the WM_KEYDOWN message.
But what about the Tab key? Or Enter or Backspace or Escape? Traditionally, these keys generate ASCII control characters, as shown in the preceding table. But in Windows they also generate virtual key codes. Should these keys be processed during WM_CHAR processing or WM_KEYDOWN processing?
The author prefered treating the Tab, Enter, Backspace, and Escape keys as control characters rather than as virtual keys. His
WM_CHAR processing often looks something like this:
case WM_CHAR:
// [other program lines]
switch (wParam)
{
case `\b': // backspace
// [other program line]
break ;
case `\t': // tab
// [other program lines]
break ;
case `\n': // linefeed
// [other program lines]
break ;
case `\r': // carriage return
// [other program lines]
break ;
default: // character codes
// [other program lines]
break;
}
return 0;
zhuang@linux:~/reading/programming-windows/06-the-keyboard/$ comments