Jump to content
  • Advertisement
Sign in to follow this  
giantmung

DirectInput problem

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

Okay, so I'm having a directInput problem, code is as follows, but I can't seem to get any keyboard input:
void CGraphicMud::FreeDirectInput( void )
{
    // Unacquire the device one last time just in case
    // the app tried to exit while the device is still acquired.
    if( g_pKeyboard )
        g_pKeyboard->Unacquire();

    // Release any DirectInput objects.
    SAFE_RELEASE( g_pKeyboard );
    SAFE_RELEASE( lpdi );
}

HRESULT CGraphicMud::InitDirectInput( )
{
	HRESULT hr;

	FreeDirectInput( );

	if( FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&lpdi, NULL ) ) )
		return hr;
	if( FAILED( hr = lpdi->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ) ) )
		return hr;
	if( FAILED( hr = g_pKeyboard->SetDataFormat( c_dfDIKeyboard ) ) )
        return hr;
    hr = g_pKeyboard->SetCooperativeLevel( hwnd, DISCL_NOWINKEY |
                                                 DISCL_NONEXCLUSIVE |
                                                 DISCL_FOREGROUND );

    if( hr == DIERR_UNSUPPORTED )
    {
        FreeDirectInput();
        MessageBox( hwnd, TEXT("SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n")
            TEXT("For security reasons, background exclusive keyboard\n")
            TEXT("access is not allowed."), TEXT("PrototypeX"), MB_OK );
        return S_OK;
    }

    if( FAILED(hr) )
        return hr;

    // Acquire the newly created device
    g_pKeyboard->Acquire();

    return S_OK;
	
}

HRESULT CGraphicMud::GameMain()
{
	this->AnimationTimer += this->ElpasedTime;

	if( this->AnimationTimer >= 0.016 )
	{
		this->AnimationTimer = 0.0;
	}
	else
		return S_OK;

	
	if( KEYDOWN( keyBuf, DIK_UP ) )
	{
		bMoving = false;
		MessageBox( 0, "A!", "A!", MB_OK );
	}
        //other stuff depended on keys
}

Got a helper function as so:
#define KEYDOWN(name,key) (name[key] & 0x80)
Variables defined as:
		LPDIRECTINPUT8 lpdi;
		LPDIRECTINPUTDEVICE8 g_pKeyboard;
		LPCDIDATAFORMAT c_dfDIKeyboard;
I don't quite understand why this code won't get keys?? Oh yeah, and in my initialization of DirectX I do a:
	if( FAILED( InitDirectInput( ) ) )
		return E_FAIL;
	g_pKeyboard->GetDeviceState(sizeof(keyBuf),(LPVOID)&keyBuf);
	loadFont( );
[Edited by - Coder on May 30, 2005 7:47:29 AM]

Share this post


Link to post
Share on other sites
Advertisement
I think you need to do g_pKeyboard->GetDeviceState(sizeof(keyBuf),(LPVOID)&keyBuf); every frame (i.e., at the start of your loop) so you always update which keys are currently being pressed.
Oh, and you want to use "[ source ] [/ source]" not "code" to post source code.
Hope that helps.

Share this post


Link to post
Share on other sites

HRESULT CGraphicMud::GameMain()
{
//have you updated the ElepsedTime variable with new data from
//QueryPerformanceCounter or similar function?
this->AnimationTimer += this->ElpasedTime;

if(this->AnimationTimer >= 0.016 )
this->AnimationTimer = 0.0;
return S_OK;

//GetDeviceState must be called every frame.
g_pKeyboard->GetDeviceState(sizeof(keyBuf),(LPVOID)&keyBuf);

if(KEYDOWN(keyBuf, DIK_UP))
{
bMoving = false;
MessageBox( 0, "A!", "A!", MB_OK );
}
}


Share this post


Link to post
Share on other sites
Yes, I update my elapsed time in my winmain by my message pump before I call 'game main' - your method seems to work, thanks!

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!