Keyboard Input

Windows Keyboard Input

The book explains the Windows keyboard messages on pages 175 to 180. The console chapter (Ch. 10) is also helpful for understanding how Windows keyboard input works.

The basic idea is that there are three Windows messages you can use (p. 176):

Your program receives these messages in the WndProc function. Page 403 shows how the author calls his console OnChar function from a case inside WndProc.

If you use this approach to keyboard input, then you need to make some connection from the WndProc function to your display. One possibility is to have a global display pointer in main.cpp. Another possibility is to use static functions and variables in the display class.

The console chapter (Ch. 10) shows how you can use the Windows messages to get build strings from input characters and then terminate the string when the Enter key is pressed.

DirectInput Keyboard Input

As one web page puts it, with DirectInput the keyboard is like a game controller with a whole bunch of buttons that you can push. One advantage of using DirectInput is that you want the behavior of the game to depend on the time that the key is down. In that case, you can have a certain action continue as long as IsKeyDown returns TRUE. Also, you might want to check the status of more than one key. For instance, if you are pressing the up arrow to move forward and the space bar to jump at the same time, then you need to know about both keys.

Chapter 14 of the textbook explains how to use DirectInput with the keyboard. In particular, the CZenKeyboard class is described on pages 597 to 604. If you use that class, probably the only function you will use is IsKeyDown which is described on pages 602 and 603. Page 604 gives a good summary of how to set up and use the CZenKeyboard class.