Sign in to follow this  

Problem with DirectInput Keyboard Input

This topic is 4547 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 am having a problem with DirectInput using Dev-Cpp. My program does not recognize any of the keys I have chosen to represent directional movement (adws). I am using Dev-Cpp 4.9.9.2 and MinGW 3.7. I am linking to libdinput.a, libdinput8.a, libdxguid.a. There was no 'dinput.h' file in Dev-Cpp's local includes directory, so I copied one from the June 2005 DirectX 9 SDK. Everything compiles with neither warning nor error. I think my problem is with the 'GetDeviceState' procedure. It appears that the char buffer[256] is not filled with any values. Here are the most important bits: - Initialization hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); hr = g_lpDIDevice->SetCooperativeLevel(Wnd.getWindow(), hr = g_lpDIDevice->Acquire(); // *** this returns DIERR_OTHERAPPHASPRIO - Processing input #define KEYDOWN(name, key) (name[key] & 0x80) char buffer[256]; hr = g_lpDIDevice->GetDeviceState(sizeof(buffer), (LPVOID)&buffer); /*** error code returned here is DIERR_NOTACQUIRED if (KEYDOWN(buffer, DIK_D)) { nRightRect += 1; nLeftRect += 1; } ... conditional statements handling other keys. Any thoughts? ...Stip

Share this post


Link to post
Share on other sites
Quote:
Original post by stipperdoo

- Initialization

hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION,
hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL);
hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);
hr = g_lpDIDevice->SetCooperativeLevel(Wnd.getWindow(),
hr = g_lpDIDevice->Acquire(); // *** this returns DIERR_OTHERAPPHASPRIO

...Stip


It would help if you'd posted the entire code with all the lovely parameters. :)

Anyhow, make sure the flags for the co-operative level are set to:
SetCooperativeLevel(Wnd.getWindow(), DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

Acquire will fail if the app window switches to the background (some other app is in the foreground at a higher priority) with the above parameters. You could try DISCL_BACKGROUND (or DISCL_EXCLUSIVE as Syranide suggested)too but I'm not sure if that's the correct answer to this problem.

Share this post


Link to post
Share on other sites

Wow, I can't believe I left out the parameters. Sorry... Here are the complete functions...

hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_lpDI, NULL);
hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL);
hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);
hr = g_lpDIDevice->SetCooperativeLevel(Wnd.getWindow(),
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
hr = g_lpDIDevice->Acquire();

...Stip

Share this post


Link to post
Share on other sites
As Specchum pointed out, Acquire will fail if the app window switches to the background. I think this also happens during initialization if all you've done is call CreateWindow and haven't called ShowWindow yet. Since the window isn't shown, it's not the foreground window and, therefore, cannot acquire the device.

Share this post


Link to post
Share on other sites

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