Sign in to follow this  

Two Device Input..

This topic is 4385 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

In my program i've tried to inizialize two Input devices, the first dedicate to Mouse Input and the second for KeyBoard Input. So: LPDIRECTINPUT8 g_lpDI; // the direct input object LPDIRECTINPUTDEVICE8 g_lpDIDeviceMouse; // the direct input device LPDIRECTINPUTDEVICE8 g_lpDIDeviceKeyboard; // the direct input device LPDIRECTINPUT8 g_lpDIK; // the direct input object I don't have problem when i try to inizialize the Mouse:
bool diManager::initDirectInputMouse(HINSTANCE hInst, HWND wndHandle)
{
	HRESULT hr;

	// Create the DirectInput object. 
    hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, 
                            IID_IDirectInput8, (void**)&g_lpDI, NULL); 

	if FAILED(hr) 
		return FALSE; 

    // Retrieve a pointer to an IDirectInputDevice8 interface 
    hr = g_lpDI->CreateDevice(GUID_SysMouse, &g_lpDIDeviceMouse, NULL); 
	if FAILED(hr)
		return FALSE; 
	
	hr = g_lpDIDeviceMouse->SetDataFormat(&c_dfDIMouse); 
	if FAILED(hr)
		return FALSE;
	 
	// Set the cooperative level 
    hr = g_lpDIDeviceMouse->SetCooperativeLevel(wndHandle, DISCL_FOREGROUND | DISCL_EXCLUSIVE); 
    if FAILED(hr) 
        return FALSE; 

    // Get access to the input device. 
    hr = g_lpDIDeviceMouse->Acquire(); 
    if FAILED(hr) 
        return FALSE; 

	return true; 
}

But when i try to inizializze my Keyboard Device:


bool diManager::initDirectInputKeyboard(HINSTANCE hInst, HWND wndHandle)
{
	HRESULT hr;

	hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, 
                          IID_IDirectInput8, (void**)&g_lpDIK, NULL);    
	if FAILED(hr) 
		return FALSE; 
	
	// Retrieve a pointer to an IDirectInputDevice8 interface 
    hr = g_lpDIK->CreateDevice(GUID_SysKeyboard, &g_lpDIDeviceKeyboard, NULL); 
	hr = g_lpDIDeviceKeyboard->SetDataFormat(&c_dfDIKeyboard); 
	if FAILED(hr) 
		return FALSE; 
	hr = g_lpDIDeviceKeyboard->SetCooperativeLevel(wndHandle, 
                             DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
    if FAILED(hr) 
        return FALSE; 

    // Get access to the input device. 
    hr = g_lpDIDeviceKeyboard->Acquire(); 
    if FAILED(hr) 
        return FALSE; 

}
This return me "FALSE" in the first control of hr. In WinMain i write this two controls:
if (!diMgr.initDirectInputMouse(hInst, wndHandle))
	{
		MessageBox(NULL, "Unable to init DirectInput MOUSE", "ERROR", MB_OK);
		return false;
	}

	if (!diMgr.initDirectInputKeyboard(hInst, wndHandle))
	{
		MessageBox(NULL, "Unable to init DirectInput KEYBOARD", "ERROR", MB_OK);
		return false;
	}
But the second control give me FALSE.. Can i create a only device dedicated to Keyboard and mouse to solve this problem?

Share this post


Link to post
Share on other sites
So i can do this?

bool diManager::initDirectInput(HINSTANCE hInst, HWND wndHandle)
{
HRESULT hr;

// Create the DirectInput object.
hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_lpDI, NULL);

if FAILED(hr)
return FALSE;

// Retrieve a pointer to an IDirectInputDevice8 interface
hr = g_lpDI->CreateDevice(GUID_SysMouse, &g_lpDIDeviceMouse, NULL);
if FAILED(hr)
return FALSE;

hr = g_lpDIDevice->SetDataFormat(&c_dfDIMouse);
if FAILED(hr)
return FALSE;

// Retrieve a pointer to an IDirectInputDevice8 interface
hr = g_lpDIK->CreateDevice(GUID_SysKeyboard, &g_lpDIDeviceKeyboard, NULL);
hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);
if FAILED(hr)
return FALSE;
hr = g_lpDIDevice->SetCooperativeLevel(wndHandle,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if FAILED(hr)
return FALSE;

// Set the cooperative level
hr = g_lpDIDevice->SetCooperativeLevel(wndHandle, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if FAILED(hr)
return FALSE;

// Get access to the input device.
hr = g_lpDIDevice->Acquire();
if FAILED(hr)
return FALSE;

return true;
}

Share this post


Link to post
Share on other sites
There is an error in the code(second post)

hr = g_lpDIK->CreateDevice(GUID_SysKeyboard, &g_lpDIDeviceKeyboard, NULL);

should be

hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDeviceKeyboard, NULL);
cause its only one dinput object

secondly look over g_lpDIDevice, you mixed them all up

Share this post


Link to post
Share on other sites

This topic is 4385 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.

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