Two Device Input..

Started by
4 comments, last by Tano 18 years, 3 months ago
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?
1pad4life7
Advertisement
In my projects (although they are OpenGL) i use one dinput device for mouse and keyboard. It may be the error in your code.
-----"Master! Apprentice! Heartborne, 7th Seeker Warrior! Disciple! In me the Wishmaster..." Wishmaster - Nightwish
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; }
1pad4life7
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
-----"Master! Apprentice! Heartborne, 7th Seeker Warrior! Disciple! In me the Wishmaster..." Wishmaster - Nightwish
You need only one DirectInput-object (LPDIRECTINPUT8), but the devices (LPDIRECTINPUTDEVICE8) are as many as you need. In your case, 2.
Thank you to everyone!
1pad4life7

This topic is closed to new replies.

Advertisement