Sign in to follow this  

Direct Input test failed

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

Hello, I am new to DirectX and I was trying to run some test using DirectInput. I copyed the following code almos exactly form "Core Techniques and Algorithms in Game Programming" just to see how it works all I am trying to do is to output 1 when "W" is pressed but It doesn't work. I tryed a lot of variations but I can't find the problem. Here is the code
int main()
{
        LPDIRECTINPUT8 g_pDI=NULL;
        LPDIRECTINPUTDEVICE8 g_pKeyboard=NULL;
        BYTE diks[256];
        HWND hWnd = GetConsoleHwnd();

        ZeroMemory(diks, sizeof(diks));

        HRESULT hr= DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL);

        hr= g_pDI->CreateDevice(GUID_SysKeyboard, &g_pKeyboard, NULL);
        hr= g_pKeyboard->SetDataFormat(&c_dfDIKeyboard);
        hr= g_pKeyboard->SetCooperativeLevel(hWnd,DISCL_FOREGROUND | DISCL_EXCLUSIVE);
	
	while(true)
	{
		hr= g_pKeyboard->Acquire();
		hr= g_pKeyboard ->GetDeviceState(sizeof(diks),diks);
		if(FAILED(hr))
		{
			hr= g_pKeyboard->Acquire();
			while(hr==DIERR_INPUTLOST || hr==DIERR_OTHERAPPHASPRIO)
				hr= g_pKeyboard->Acquire();

			hr= g_pKeyboard ->GetDeviceState(sizeof(diks),(LPVOID)&diks);
		}
		
		
		cout<<((diks[DIK_W] & 0x80)!=0);
			
		
	}
	if(g_pKeyboard) g_pKeyboard->Unacquire();
	SAFE_RELEASE(g_pKeyboard);
	SAFE_RELEASE(g_pDI);
}



The only output I am getting is 0's. Thanks a lot for your Help!

Share this post


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