Sign in to follow this  

Direct Input query

This topic is 3741 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 have a Dialog based application that, among other things, contains Listview Controls listing files. When the application starts, a second 'Preview' dialog window is also created and DirectX is set up so that when a user clicks on a file in a Listview control a 3D rendering of the file (the files are 3D models) appears in this 2nd dialog 'Preview' window and slowly rotates around so the user can see it nicely. However, many of the models are large rooms and buildings and I have decided that it would be nice if the user could move around them in 1st person shooter style and so I am trying to implement Direct Input but I am having a problem which I cant seem to figure out. I create my direct input device like so:
  if(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI,NULL)!=DI_OK) 
And then I create my mouse and keyboard and Acquire them and all is fine. But my problem is that when I set the cooperative level I pass the HWND of the Preview window and I also pass
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE
so that GetDeviceState will only accept input when the Preview window specifically is in focus; but nothing happens. GetDeviceState never returns DI_OK. It doesnt even return DI_OK when the main dialog window is in focus either. I assume that the window doesnt need to be in fullscreen for it to be 'in focus' since I have played many DirectX games in window mode. If I change DISCL_FOREGROUND to DISCL_BACKGROUND then it all works fine so the code is working its just that it seems as if DirectInput doesnt detect when my application is in focus. I must be missing something somewhere and I am not sure which bits of code I should post here but I will post what I feel to be the important bits. The Preview window is created like so:
 	HWND Pic = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_IMAGE),hwnd, ImageDialogProc );
	SetWindowPos(Pic, NULL, NULL, NULL, 600, 600, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);// | SWP_SHOWWINDOW);
	if(Pic != NULL)
	{
		p_Input->Init(Pic);		//And create DirectInput Devices
		ShowWindow(Pic, SW_SHOW);
		return TRUE;
	} 
p_Input is a pointer to the class that handles the Direct Input. The user can destroy and recreate the preview window and each time they do I assume that the DirectInput devices need to be destroyed when they close the window and recreated when they open it again which is why that line p_Input->Init(Pic); is there. Here is my code for the DInput and Keyboard devices being created. No need to post both the keyboard and mouse since they are createdin the same way I assume:
	//***************Direct Input Initialization******************
	if(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,
						(void**)&g_pDI,NULL)!=DI_OK)
	{
		MessageBox(NULL,L"Direct Input Create Failed",NULL,NULL);
	}
	//***********End of Direct Input Initialisation***************

	//Test for Keyboard and create Keyboard Device
	if(g_pDI->CreateDevice(GUID_SysKeyboard,&g_pKeyboard,NULL)!=DI_OK)
	{
		MessageBox(NULL,L"Create Keyboard Device Failed",NULL,NULL);
	}
	if(g_pKeyboard->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)
	{
		MessageBox(NULL,L"Set Keyboard Data Format Failed",NULL,NULL);
	}
	if(g_pKeyboard->SetCooperativeLevel(hWnd,DISCL_FOREGROUND| DISCL_NONEXCLUSIVE)!=DI_OK)
	{
		MessageBox(NULL,L"Keyboard Cooperative Level Failed",NULL,NULL);
	}
	g_pKeyboard->Acquire();
	//****************End of Create Keyboard**********************
Then I just got a bit of test code to detect keyboard input:
	if(g_pKeyboard->GetDeviceState(256,(LPVOID)diKey)==DI_OK)
	{
		if(diKey[DIK_UP]&0x80)
		{
			int sdfsdf=0;
		}
		if(diKey[DIK_W]&0x80)
		{
			int sdfsdf=0;
		}
	}
So, as I say, I am stuck :( and I cant seem to figure out a good way of debugging this kind of problem. Thank you in advance for any help you may offer :)

Share this post


Link to post
Share on other sites
First off, I am obligated to point out that Microsoft recommends that you do not use DirectInput for mouse and keyboard support. Take a look at this post for more information about it.

Secondly, I am thinking that your "Preview" window is never receiving focus. You can use the GetFocus and SetFocus functions to deal with the focus aspect.

Share this post


Link to post
Share on other sites
o.O interesting. The SetFocus did not solve the problem but it doesnt really matter because I'm about to go change over to WM_INPUT after reading that =) This will not only (hopefully) solve my problem, but will also save poor Evil Steve's fingers from having to come here and type out his reasons why I should not be using Direct Input.

Share this post


Link to post
Share on other sites

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