PC Gaming
  Home arrow PC Gaming arrow Page 4 - Controlling Games with the Keyboard an...
Dev Hardware Forums 
Computer Cases  
Computer Processors  
Computer Systems  
Digital Cameras  
Flat Panels  
Hardware Guides  
Hardware News  
Input Devices  
Memory  
Mobile Devices  
Motherboards  
Networking Hardware  
Opinions  
PC Cooling  
PC Gaming  
PC Speakers  
Peripherals  
Power Supply Units  
Software  
Sound Cards  
Storage Devices  
Tech Interviews  
User Experiences  
Video Cards  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PC GAMING

Controlling Games with the Keyboard and Mouse
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 30
    2004-10-11

    Table of Contents:
  • Controlling Games with the Keyboard and Mouse
  • Taking a Look at User Input Devices
  • Mouse and Joystick
  • Tracking the Mouse
  • Revamping the Game Engine for Input
  • Sprucing Up the Bitmap Class
  • Building the UFO Example
  • Testing the Finished Product

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Controlling Games with the Keyboard and Mouse - Tracking the Mouse


    (Page 4 of 8 )

    When you move the mouse, a series of events is set off that is very similar to those set off by the keyboard. In fact, the Win32 API includes a series of mouse messages that are used to convey mouse events, similar to how keyboard messages convey keyboard events. In the previous section, you learned that the Win32 keyboard messages aren't up to the task of providing efficient input for games. Fortunately, the same cannot be said of the mouse messages. It turns out that the built-in Win32 approach to handling mouse events via messages works out just fine for games. The following are the mouse messages used to notify Windows programs of mouse events:

    • WM_MOUSEMOVE—Any mouse movement

    • WM_LBUTTONDOWN—Left mouse button pressed

    • WM_LBUTTONUP—Left mouse button released

    • WM_RBUTTONDOWN—Right mouse button pressed

    • WM_RBUTTONUP—Right mouse button released

    • WM_MBUTTONDOWN—Middle mouse button pressed

    • WM_MBUTTONUP—Middle mouse button released

    The first mouse message, WM_MOUSEMOVE, lets you know whenever the mouse has been moved. The remaining messages relay mouse button clicks for the left, right, and middle buttons, respectively. Keep in mind that a mouse button click consists of a button press followed by a button release; you can implement a mouse dragging feature by keeping track of when a mouse button is pressed and released and watching for mouse movement in between.


    Note - A WM_MOUSEWHEEL message is also sent whenever the user rotates the wheel that is located on the top of some mouse devices. Although the mouse wheel could certainly prove useful in some game scenarios, I opted to focus on the core mouse input messages in this discussion. By all means, explore adding support for the WM_MOUSEWHEEL message to the game engine if you have a game in mind that would benefit from it.


    Regardless of whether you're interested in mouse movement or a mouse button click, the important factor regarding the mouse is where the mouse cursor is located. Fortunately, the mouse cursor position is provided with all the previously mentioned mouse messages. It's packed into the lParam argument that gets sent to the GameEngine::HandleEvent() method. The following is the prototype for this method, just in case you forgot:

    LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam,
    LPARAM lParam);

    If you recall, the wParam and lParam arguments are sent along with every Windows message and contain message-specific information. In the case of the mouse messages, lParam contains the XY position of the mouse cursor packed into its low and high words. The following is an example of a code snippet that extracts the mouse position from the lParam argument in a WM_MOUSEMOVE message handler:

    case WM_MOUSEMOVE:
    WORD x = LOWORD(lParam);
    WORD y = HIWORD(lParam);
    return 0;

    The wParam argument for the mouse messages includes information about the mouse button states, as well as some keyboard information. More specifically, wParam lets you know if any of the three mouse buttons are down, as well as whether the Shift or Control keys on the keyboard are being pressed. In case you don't see it, the wParam argument—used in conjunction with WM_MOUSEMOVE— provides you with enough information to implement your own Doom strafing feature! The following are the constants used with the mouse messages to interpret the value of the wParam argument:

    • 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

    You can check for any of these mouse constants to see if a button or key is being pressed during the mouse move. The constants are actually flags, which means that they can be combined together in the wParam argument. To check for the presence of an individual flag, you must use the bitwise AND operator (&) to see if the flag is present. The following is an example of checking wParam to see if the right mouse button is down:

    if (wParam & MK_RBUTTON)
    // Yep, the right mouse button is down!

    This code shows how to use the bitwise AND operator (&) to check for individual flags. This is a technique you use in Chapter 7 to check the state of a joystick.

    SamsThis chapter is from Beginning Game Programming, by Michael Morrison (Sams, ISBN: 0672326590). Check it out at your favorite bookstore today.

    Buy this book now.

    More PC Gaming Articles
    More By Sams Publishing


     

    Recommended by Dev Hardware

    PC GAMING ARTICLES

    - Age of Conan Review
    - Harnessing Video Game Power for Good
    - Grand Theft Auto IV Review
    - PC Games, a Dying Breed?
    - The Art and Psychology of Gaming
    - Halo 3 Hands On
    - GTA IV: Going Too Far?
    - FEAR Combat Review
    - Prey Review
    - Elder Scrolls IV Oblivion Review
    - PS3: Playing at a Whole New Level
    - F.E.A.R. Video Game Review
    - They Don`t Have to See It to Frag It
    - Do Violent Games Make Violent People?
    - Xbox 360: Before Next Gen






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway