Getting Mouse Input


Getting input from the mouse is a lot like communicating with the keyboard. Directlnput is very consistent between the two. The same process is used in creating the device; we just specify the mouse in the enumerations rather than the keyboard. The code to create the device looks like this:

 MouseDev = new Device( SystemGuid.Mouse );  MouseDev.SetCooperativeLevel(m_form, CooperativeLevelFlags.Exclusive   CooperativeLevelFlags.Foreground);  MouseDev.SetDataFormat(DeviceDataFormat.Mouse); 

The biggest difference with working with the mouse comes with working with the mouse data. Instead of the KeyboardState class representing the possible keyboard key states, we have the MouseState class. This class contains the mouse button states as well as three integers for the X, Y, and Z movement values for the mouse. I know that you might be scratching your head and wondering, Z! Since when is a mouse three dimensional? Microsoft has dubbed the scroll wheel that comes on many modern mice as Z. This mouse data structure is declared as follows :

 MouseState m_mousedata = new MouseState(); 

Just like with the keyboard, we must call the device s Acquire method to attach the device to the hardware and Poll to read the latest data from the hardware. After polling the mouse device we can get a copy of the data with this line:

 m_mousedata = MouseDev.CurrentMouseState; 

The button data works just like the keyboard data. The most significant bit gets set if the button is pressed. Button 0 is the primary button. Although this defaults to the left mouse button, don t assume that will always be the case. It is possible through Windows to designate the right mouse button as the primary button (think left-handed users).

The axis data returned in the mouse data structure represents the number of counts that the mouse has moved in each direction since the last time the mouse was polled. Following the usual Microsoft axis definitions, the X value is positive to the right and negative to the left. The Y values are positive forward and negative backward. The Z values are positive as the scroll wheel is spun forward and negative when spun backward. It is important to make sure that we poll the mouse only once per frame. Otherwise we will be wondering why our mouse axis data is always zero. Since the axis data is relative to the last time it was polled, the second poll will almost always return zeros unless the mouse is moving very fast during the polling.

Just like with the keyboard, we must also remember to unacquire the device when we are done with it using the Unacquire method. This informs the operating system that we are done with the device.




Introduction to 3D Game Engine Design Using DirectX 9 and C#
Introduction to 3D Game Engine Design Using DirectX 9 and C#
ISBN: 1590590813
EAN: 2147483647
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net