Sign in to follow this  
LittleFreak

Direct Input Help

Recommended Posts

Ok so Im having issue getting my mouse to work... Im making an .dll based engine... and I am just trying to get the base down before I start throwing in serious things and start tweaking. All I want for now is basic mouse and keyboard... The keyboard works fine but the mouse is only working 1 feature at a time. If I get the button working the X,Y axis dont work. If I get he X to work the Y axis and buttons dont work. Heres the code: This is the .h
#pragma once
#ifdef KAIGANINPUTS_EXPORTS
#define KAIGANINPUTS_API __declspec(dllexport)
#else
#define KAIGANINPUTS_API __declspec(dllimport)
#endif

#include <dinput.h>

#define    KeyDown(data, n)    ((data[n] & 0x80) ? true : false)
#define    KeyUp(data, n)    ((data[n] & 0x80) ? false : true)

/***************************************************************

class KInput - Handles All atributes of Inputs

***************************************************************/

class KAIGANINPUTS_API KInput
{

public:
	KInput(HWND wHandle);
	~KInput();

	bool KeyboardState(int keyDown);
	bool MouseButtonState(int buttonDown);
	long RelMouseX();
	long RelMouseY();
	long MouseScroll();
	

private:
	HWND wndHandle;
    LPDIRECTINPUT8			pDXInput;
	LPDIRECTINPUTDEVICE8	pDXKeyboard;
	LPDIRECTINPUTDEVICE8	pDXMouse;
    char					keystate[256];
	DIMOUSESTATE			mouseState;

};

This is the .cpp
#include "KaiganInputs.h"

/***************************************************************

class KInput - Handles All atributes of Inputs

***************************************************************/

KInput::KInput(HWND wHandle)
{
	wndHandle = wHandle;

	mouseState.lX = NULL;
	mouseState.lY = NULL;
	mouseState.lZ = NULL;
	
// Create Input Device
	if(FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDXInput, NULL)))
		return;

// Create Keyboard Device
	if(FAILED(pDXInput->CreateDevice(GUID_SysKeyboard, &pDXKeyboard, NULL)))
		return;
	if(FAILED(pDXKeyboard->SetDataFormat(&c_dfDIKeyboard)))
		return;
	if(FAILED(pDXKeyboard->SetCooperativeLevel(wndHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
		return;

// Create Mouse Device
	if(FAILED(pDXInput->CreateDevice(GUID_SysMouse, &pDXMouse, NULL)))
		return;
	if(FAILED(pDXMouse->SetDataFormat(&c_dfDIMouse)))
		return;
	if(FAILED(pDXMouse->SetCooperativeLevel(wndHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
		return;
	if(FAILED(pDXMouse->Acquire()));
		return;


}
KInput::~KInput()
{
	
	if (pDXKeyboard)
		pDXKeyboard->Release();

	if(pDXMouse)
		pDXMouse->Release();
  
	if (pDXInput)
		 pDXInput->Release();


}

bool KInput::KeyboardState(int keyDown)
{
	 
	if(FAILED(pDXKeyboard->Acquire()))
		return false;
	if(FAILED(pDXKeyboard->GetDeviceState(sizeof(char[256]), (LPVOID)&keystate)))
		return false;

	if(keystate[keyDown])
		return true;

    return false;

}

bool KInput::MouseButtonState(int buttonDown)
{

	if(FAILED(pDXMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState)))
		return false;
	
	if(mouseState.rgbButtons[buttonDown])
		return true;

	return false;

}

long KInput::RelMouseX()
{

	if(FAILED(pDXMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState)))
		return -2;

	return mouseState.lX;

}

long KInput::RelMouseY()
{

	if(FAILED(pDXMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState)))
		return -2;

	return mouseState.lY;

}

long KInput::MouseScroll()
{

	if(FAILED(pDXMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState)))
		return -2;

	return mouseState.lZ;

}


Sorry for the messy code...

Share this post


Link to post
Share on other sites
I would say that calling GetDeviceState() for every mouse query is causing the problem. If you call KInput::RelMouseX() followed by KInput::RelMouseY() you could get something like :

KInput::RelMouseX() : mouseState.lX = +2, mouseState.lY = -4 -> Returns +2
KInput::RelMouseY() : mouseState.lX = 0, mouseState.lY = 0 -> Returns 0 (instead of the expected -4)

This is just my opinion, but I would recommend using buffered mode instead of immediate mode to retrieve input data. This way you'll make sure that you won't miss any input, no matter what update rate you are using. If I were to do this, I'd have one KInput::Tick() method, called once per frame, that would query both the mouse and keyboard state and cache the results. That way, KeyboardState(), RelMouseX() & such would only have to return the cached state.

Anyway, if you choose to do something like that, check out the Mouse & Keyboard DirectInput samples, they show how to setup and retrive data in buffered mode.

- Aldenar

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this