Jump to content
  • Advertisement
Sign in to follow this  

About Mice

This topic is 4420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am starting directX still. I have managed to setup a graphics core, but it is mostly a copy of code from a book. I am customizing more of it, but I want to be able to get input first. I can read the keyboard, but the mouse is more complex. This is what I have:
bool eInput::InitMouse()
  // Create the device object
  if(FAILED(iDI->CreateDevice(GUID_SysMouse, &iDIMouse, NULL)))
	return false;

	mdatastruct.diph.dwSize       = sizeof(DIPROPDWORD);
    mdatastruct.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    mdatastruct.diph.dwObj        = 0;
    mdatastruct.diph.dwHow        = DIPH_DEVICE;
    // the data
    mdatastruct.dwData            = 32;

	if(FAILED(iDIMouse->SetProperty(DIPROP_BUFFERSIZE, &mdatastruct.diph)))

  // Set the data format
  if(FAILED(iDIMouse->SetDataFormat(&c_dfDIMouse))) {
	return false;

This basically sets the mouse, correct? I need to read from mdatastruct. I have a function mpros for that, but it is empty... I have read the DXSDK docs and a book that I have, but they both are confusing, and the source doesn't make sense for my context. I would be most greatful (grateful?) if someone could explain how to read from my nice little buffer :) Oh, here is the eInput class:
class eInput
	HWND hwnd;
	IDirectInput8 *iDI;
	IDirectInputDevice8 *iDIKeyboard;
	IDirectInputDevice8 *iDIMouse;
	HANDLE mevent;
	// IDirectInputDevice8 *iDIJoy;
	char kdatabuffer[256];
	DIPROPDWORD mdatastruct;
	long mY, mX, mZ;

	bool KRead();
	bool MRead();
	bool Kpros();
	bool Mpros();
	bool InitKeyboard();
	bool InitMouse();
	// bool InitJoy();
	// bool JRead();
	bool Init(HWND hWnd, HINSTANCE hinst);
    bool Shutdown();

Share this post

Link to post
Share on other sites
here is an example of how to do it written by LaMothe:
(error testing removed for clarity)

Add this to your init function (after SetDataFormat)

//Make a global variable to read the mouse:
DIMOUSESTATE mouse_state;
// read the current mouse state like this:
iDIMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)mouse_state); // (the last argument should probably be (LPVOID)&mouse_state. Just a typo in the book I guess)

// move the object (or what ever...)
object.x += mouse_state.lX;
if(mouse_state.rgbButtons[0] & 0x80)
// left button pressed...

I have seen other ways to do this but this is the simplest way to do it that I know of.
(Also note that right button = 1 and middle button = 2)

If you do it this way, there is no need for either the SetProperty function or the mdatastruct at all.
Just replace the call to SetProperty with
iDIMouse->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);// or some other flags appropriate for your application

[Edited by - pulpfist on May 17, 2006 2:15:53 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!