Controlling Games with the Keyboard and Mouse - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Controlling Games with the Keyboard and Mouse

Description:

... keys interested have been pressed (since Windows messaging system is very slow) ... a key is up or down at the time the function is called, and whether the key was ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 9
Provided by: kay57
Category:

less

Transcript and Presenter's Notes

Title: Controlling Games with the Keyboard and Mouse


1
Controlling Games with the Keyboard and Mouse
2
Assessing Keyboard Input for Games
  • Win32 API keyboard messages WM_KEYDOWN, WM_KEYUP
  • Game engine constantly check the keyboard to see
    if any of the keys interested have been pressed
    (since Windows messaging system is very slow)
  • If pressed, game springs into motion and
    efficiently responds to the key press
  • If not, run through cycles of the game engine

3
Adding Keyboard Support
  • In WinMain() add HandleKeys()
  • if (iTickCount gt iTickTrigger)
  • iTickTrigger iTickCount
  • GameEngineGetEngine()-gtGetFrameDelay()
  • //game engine actively check key press
  • HandleKeys()
  • GameCycle()

4
GetAsyncKeyState Function
  • determines whether a key is up or down at the
    time the function is called, and whether the key
    was pressed after a previous call to
    GetAsyncKeyState
  • SHORT GetAsyncKeyState(      int vKey )
  • Return value If the function succeeds, the
    return value specifies whether the key was
    pressed since the last call to GetAsyncKeyState,
    and whether the key is currently up or down. If
    the most significant bit is set, the key is down,
    and if the least significant bit is set, the key
    was pressed after the previous call to
    GetAsyncKeyState

5
Virtual-key code
  • vKey one of 256 possible virtual-key code
  • VK_BACK (08) BACKSPACE key
  • VK_TAB (09) TAB key
  • VK_SHIFT (10) SHIFT key
  • VK_CONTROL (11) CTRL key
  • VK_MENU (12) ALT key
  • VK_PAUSE (13) PAUSE key
  • VK_SPACE (20) SPACEBAR
  • VK_PRIOR (21) PAGE UP key
  • VK_NEXT (22) PAGE DOWN key
  • VK_END (23) END key
  • VK_HOME (24) HOME key
  • VK_LEFT (25) LEFT ARROW key
  • VK_UP (26) UP ARROW key
  • VK_RIGHT (27) RIGHT ARROW key
  • VK_DOWN (28) DOWN ARROW key
  • VK_F1 (70) F1 key
  • VK_F2 (71) F2 key
  • VK_F3 (72) F3 key, etc.

6
Tracking the Mouse
  • Mouse messages
  • WM_MOUSEMOVE
  • WM_LBUTTONDOWN
  • WM_LBUTTONUP
  • WM_RBUTTONDOWN
  • WM_RBUTTONUP
  • WM_MBUTTONDOWN
  • WM_MBUTTONUP
  • Unlike Win32 keyboard messages, mouse messages
    can provide efficient input for games
  • Add mouse messages into game engine HandleEvent()
    function

7
Mouse Support in HandleEvent() function
  • HandleEvent() is responsible for handling mouse
    messages
  • case WM_LBUTTONDOWN
  • // Handle left mouse button press
  • MouseButtonDown(LOWORD(lParam),
    HIWORD(lParam), TRUE)
  • return 0
  • case WM_LBUTTONUP
  • // Handle left mouse button release
  • MouseButtonUp(LOWORD(lParam),
    HIWORD(lParam), TRUE)
  • return 0
  • case WM_RBUTTONDOWN
  • // Handle right mouse button press
  • MouseButtonDown(LOWORD(lParam),
    HIWORD(lParam), FALSE)
  • return 0
  • case WM_RBUTTONUP
  • // Handle right mouse button release
  • MouseButtonUp(LOWORD(lParam),
    HIWORD(lParam), FALSE)
  • return 0
  • case WM_MOUSEMOVE

8
Tracking the Mouse Cont.
  • wParam and lParam arguments are sent along with
    every Windows message and contain
    message-specific information
  • lParam contains the XY position of the mouse
    cursor
  • case WM_MOUSEMOVE
  • WORD x LOWORD(lParam)
  • WORD y HIWORD(lParam)
  • return 0
  • wParam includes information about the mouse
    button states, as well as some keyboard
    information using mouse constants
  • MK_LBUTTON left mouse button is down
  • MK_RBUTTON right mouse button is down
  • MK_MBUTTON middle mouse button is down
  • MK_SHIFT shift key is down
  • MK_CONTROL control key is down
  • Use bitwise AND operator () for wParam and a
    mouse constant to see if the flag is present
  • if (wParam MK_RBUTTON)
  • // yep, the right mouse button is down
Write a Comment
User Comments (0)
About PowerShow.com