Jump to content
  • Advertisement
Sign in to follow this  
mvtapia

Keyboard doesn't return data

This topic is 4864 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 everyone, I've been having trouble retrieving data from the keyboard using direct input, this is the code:
{
	hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer); 
	if FAILED(hr) 
	{ 
		return; 
	} 
if (buffer[DIK_ESCAPE] & 0x80)
{
	exit(NULL);
	return;
}

// I also tried this

	if (KEYDOWN(buffer, DIK_ESCAPE))
	{
		exit(NULL);
		return;
	}


this is the KEYDOWN definition #define KEYDOWN(name, key) ((name[key] & 0x80) ? true : false) char buffer[256]; but no responce on either one. it does not get in the cycle. Any help is welcome. Thanks [Edited by - mvtapia on April 26, 2005 7:33:20 PM]

Share this post


Link to post
Share on other sites
Advertisement
Why aren't you trying to reacquire the device if GetDeviceState fails? Something like this:


{
hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);
if FAILED(hr)
{
hr = g_lpDIDevice->Acquire();
while( hr == DIERR_INPUTLOST )
hr = g_lpDIDevice->Acquire();

return false; //or wotever the return type of the function is
}



HTH.

Share this post


Link to post
Share on other sites
Here is an example of my key test
BOOL CKeyboard::IsKeyDown(int Key)
{
HRESULT r = 0; // error testing

if (!m_bInitialized) // see if keyboard was initialized
return FALSE; // if not, bail

if (g_hmain_window != GetActiveWindow())
return FALSE;

// get the state of the keyboard into the key buffer
r = m_pKeyDev->GetDeviceState(sizeof(m_KeyBuffer),&m_KeyBuffer);

if (FAILED(r))
{
if(r == DIERR_INPUTLOST) // if the device is not acquired
{
while (r == DIERR_INPUTLOST) // if not acquired
r = m_pKeyDev->Acquire(); // get it back

if (SUCCEEDED(r))
m_pKeyDev->GetDeviceState(sizeof(m_KeyBuffer),&m_KeyBuffer);
else
return FALSE; // bail
} // end r == DIERR_
else // otherwise it was some other error
return FALSE; // bail
} // end if (FAILED(r))

if( m_KeyBuffer[Key] & 0x80) // check if key was set
return TRUE; // return sucess
else
return FALSE; // return failure
} // end CKeyboard::IsKeyDown



If this doesn't help, can you post your startup code for your keyboard device?

Share this post


Link to post
Share on other sites
I'm sorry to say but I've tryed everything and I can't think of another reason. You code was very helpfull. I implemented some stuff that I did not have. but still nothing.
any more thougths... just for kicks... hehe

Share this post


Link to post
Share on other sites
Hmm... post your entire device creation code AND keyboard read code as it stands now (after the changes you've made so far). I'll try and duplicate the error at my end and see if I can come up with something.

Keyboard input is actually quite a simple thing to implement, so I dunno what the problem could be without seeing the relevant code in full. Also, which version of DX are u using? If you have DX9, have a look at the keyboard sample. It could help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You wrote:
if (g_pMouse)
{
g_pMouse->Acquire();
KeyInit=true;
}


When it should read
if (g_pMouse)
{
g_pMouse->Acquire();
MouseInit=true;
}



But other then that, to get the keyboard to work you will need to move the keyboard test. It IS working, however you linked them to the WM_PAINT call, so it will ONLY perform a keyboard test IF you are repainting the window (like holding down the key you want to hold down while dragging the window off the screen and releasing)

I did a bit of reworking, I don't know if you like it but I found this works:

First changing your message loop to be more... well less Windows I guess.

I changed this
			// Enter the message loop
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}

to this:
			// Enter the message loop
MSG msg;

while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT) // if message = quit
break;

TranslateMessage(&msg);

DispatchMessage(&msg);
} // end if

//**non-message related information here
// perform keyboard test
ProcessKBInput(hWnd);
} // end while
}


Then I took out the call to ProcessKBInput(hWnd) which was lodged in your WM_PAINT message case statement, and then you had a bug in your KEYDOWN macro:

You had:
#define KEYDOWN(name, key) ((name[key] & 0x0800) ? true : false)

But you should have had
#define KEYDOWN(name, key) ((name[key] & 0x80) ? true : false)


With those changes, the keyboard should work fine... I might suggest moving some of your code around to have the non-windows/handle based code (eg: directX or game code) at or around the area I placed your ProcessKBInput(hWnd) call.

Good luck (BTW it worked for me after the changes, let me know if you have any more trouble)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
You wrote:
*** Source Snippet Removed ***

When it should read
*** Source Snippet Removed ***


But other then that, to get the keyboard to work you will need to move the keyboard test. It IS working, however you linked them to the WM_PAINT call, so it will ONLY perform a keyboard test IF you are repainting the window (like holding down the key you want to hold down while dragging the window off the screen and releasing)

I did a bit of reworking, I don't know if you like it but I found this works:

First changing your message loop to be more... well less Windows I guess.

I changed this
*** Source Snippet Removed ***
to this:
*** Source Snippet Removed ***

Then I took out the call to ProcessKBInput(hWnd) which was lodged in your WM_PAINT message case statement, and then you had a bug in your KEYDOWN macro:

You had:
#define KEYDOWN(name, key) ((name[key] & 0x0800) ? true : false)

But you should have had
#define KEYDOWN(name, key) ((name[key] & 0x80) ? true : false)


With those changes, the keyboard should work fine... I might suggest moving some of your code around to have the non-windows/handle based code (eg: directX or game code) at or around the area I placed your ProcessKBInput(hWnd) call.

Good luck (BTW it worked for me after the changes, let me know if you have any more trouble)



Whoops, that was me that posted that, guess it logged me out because I was taking too long typing the post. Let me know if you have any more trouble

Share this post


Link to post
Share on other sites
It worked... I even got the mouse to respond... It's true what you say about having the routines in the paint loop. I tryed before with a break point on before the getdevicestate routine and kept the escape key down when I press F5 and i recognized the key.

One question what is the difference between

while( GetMessage( &msg, NULL, 0, 0 ) )

and

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

I don't really get it.
Thank you, my headace is gone.. hehe

Share this post


Link to post
Share on other sites
Quote:
Original post by mvtapia
It worked... I even got the mouse to respond... It's true what you say about having the routines in the paint loop. I tryed before with a break point on before the getdevicestate routine and kept the escape key down when I press F5 and i recognized the key.

One question what is the difference between

while( GetMessage( &msg, NULL, 0, 0 ) )

and

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

I don't really get it.
Thank you, my headace is gone.. hehe


Very welcome, glad it's gone
when you do
"while( GetMessage( &msg, NULL, 0, 0 ) )", you kind of just loop for the messages, when you do peekmessage, you process one if it's there, otherwise do what's after it. Works much better when your game isn't message driven (DirectX instead of MFC)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!