Sign in to follow this  

Can't Acquire Direct Input Device

This topic is 4710 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'm having trouble acquiring the keyboard in DirectInput. I just have some standard code init code that works fine, but then when I try to acquire the keyboard it fails.
// globals
LPDIRECTINPUT8          lpDI;
LPDIRECTINPUT8DEVICE    lpDIDevice;


// Initializes DirectInput
HRESULT InitDI(HWND hWnd, HINSTANCE hInst) {
    HRESULT hr; 

    if(FAILED(hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, 
                         IID_IDirectInput8, (void**)&lpDI, NULL))) {
        // Post Message
        FAILED hr;
    }

    if(FAILED(hr = lpDI->CreateDevice(GUID_SysKeyboard, lpDIDevice,
                         NULL))) {
        // Post Message
        return hr; 
    }

    if(FAILED(hr = lpDIDevice->SetDataFormat(&c_dfDIKeyboard))) {
        // Post Message
        return hr; 
    }

    if(FAILED(hr = lpDIDevice->SetCooperativeLevel(hWnd, 
		       DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {
        // Post Message
        return hr;
    }

    if(FAILED(hr = lpDIDevice->Acquire())) {
        // Post Message
        return hr;
    }

    return S_OK;
}

I just stole this off the MSDN website, so I don't know why it won't work! Any ideas? Edited by Coder: Used source tags. Check GDNet Forums FAQ [Edited by - Coder on January 18, 2005 9:18:54 PM]

Share this post


Link to post
Share on other sites
I think the Acquire will fail if your window isn't the current foreground window (based on your coop level). Try doing a ShowWindow before trying to acquire the keyboard.

As a hint to working with DX in general, never use if (FAILED(DXFunction()). Rather, assign the return value to an HRESULT and test that for failure. That way, you can examine the return value to find out why the function failed.

Share this post


Link to post
Share on other sites

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