Sign in to follow this  
giantmung

DirectInput problem

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
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

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