Sign in to follow this  

DirectInput Problem

This topic is 2852 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've been playing with DirectInput, and I've been through a few tutorials, however, I get stuck at this level with the code: char buffer[256]; m_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer); if(buffer[DIK_X]/128) { TestVariable += 0.1f; } Where that doesn't increment TestVariable at all. THIS on the other hand increments it EVERY frame (for obvious reasons): char buffer[256]; m_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer); //if(buffer[DIK_X]/128) //{ TestVariable += 0.1f; //} The tutorials I've looked at are: http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut12.php http://msdn.microsoft.com/en-us/library/ee418877%28VS.85%29.aspx Both of them leave me stuck at the same line. What I believe is happening, is "buffer[DIK_X]/128" is simply equal to 0 at all times. I'm not sure if its in the code itself, or maybe my keyboard's scancodes are all weird? (just a wireless logitech QWERTY). I had the exact same issue when set up like this too: char buffer[256]; m_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer); if(KEYDOWN(buffer, DIK_RIGHT)) { TestVariable += 0.1f; } using this macro (which I honestly don't understand): #define KEYDOWN(name, key) (name[key] & 0x80) Does anyone have any insight as to why this isnt working? The rest of my code is set up essentially exactly same as the tutorials, which appear to be essentially the same as eachother too.

Share this post


Link to post
Share on other sites
This doesn't really fix your problem, but you really, really, really shouldn't be using DirectInput for keyboard input. It's slow, difficult to use, doesn't handle "exotic" formats like Chinese characters, and a whole host of other reasons. Just use WM_KEYUP / WM_KEYDOWN and save your sanity.

Share this post


Link to post
Share on other sites
Thanks so much! After a bit of tweaking, I managed to work WM_KEYDOWN into my app. I just have a quick question, I've successfully implemented tests for VK_LEFT, VK_RIGHT etc etc. But when I try to implement a WASD system, i get VK_A 'undeclared identifier'.

I looked it up in the WinUser.h file, to see the scan codes. It says "VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)" Which I understand to mean they ARENT defined as VK_A through VK_Z; suggesting that I need to use the lower level scan code. If I substitute 'a' or 0x41 into my if statement, I dont get any results.

In short, if I want to use WASD with WM_KEYDOWN, what do I refer to them as?

Share this post


Link to post
Share on other sites
Quote:
Original post by JustSuds
Thanks so much! After a bit of tweaking, I managed to work WM_KEYDOWN into my app. I just have a quick question, I've successfully implemented tests for VK_LEFT, VK_RIGHT etc etc. But when I try to implement a WASD system, i get VK_A 'undeclared identifier'.

I looked it up in the WinUser.h file, to see the scan codes. It says "VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)" Which I understand to mean they ARENT defined as VK_A through VK_Z; suggesting that I need to use the lower level scan code. If I substitute 'a' or 0x41 into my if statement, I dont get any results.

In short, if I want to use WASD with WM_KEYDOWN, what do I refer to them as?
You need to use upper case for 'A' - 'Z'.

If you want to record text input though, you probably want WM_CHAR - that'll take care of capital letters, detecting if shift or caps lock is on, etc.

Share this post


Link to post
Share on other sites
'W', 'S', 'A' and 'D' should work.

Example:

case WM_KEYDOWN:
switch(wParam)
{
case VK_SPACE:
g_run = !g_run;
SetState(g_state);
break;

case 'Q':
SetState(MOVE);
break;




Can you post your WM_KEYDOWN handler?

Share this post


Link to post
Share on other sites
Its probably not very efficient, as I'm just experimenting/testing/learning at this point.

My message loop (in my APIENTRY _tWinMain method) contains this line:

if(msg.message == WM_KEYDOWN)
g_Game->DoInput(msg.wParam);

Then in my game class i have an if cycle in an otherwise empty DoInput(WPARAM wParam) method:

if (wParam == VK_RIGHT)
camPosX += 0.1f;
if (wParam == VK_LEFT)
camPosX -= 0.1f;
if (wParam == VK_UP)
camPosY += 0.1f;
if (wParam == VK_DOWN)
camPosY -= 0.1f;
if (wParam == VK_HOME)
camPosZ += 0.1f;
if (wParam == VK_END)
camPosZ -= 0.1f;

if (wParam == 'A')
lookAtPosX += 0.1f; //Bit that isnt working!

The purpose of this code block is just to test that I can use keyboard input to modify variables. I have a DX9 app set up with a cube in the centre of my blue window (thats what i call it, but mine's grey :-)).

The camPos variables are just float values that are plugged straight into my view matrices. I've also tested with camPosX in place of LookAtPosX, just to be sure that its not the variable.

I just cant seem to get any combination of wParam == 'A', 'a', 0x41, VK_A to respond in any way yet.

Thanks for your help so far too btw. And, whats the code for a code block on these forums? (I'm gunna go head on over to the rules now to see if i can find it)

Share this post


Link to post
Share on other sites
Yeah, I have this block about 8 lines above my call to DoInput()


while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}




I just don't quite understand what this does.

Share this post


Link to post
Share on other sites
Quote:
Original post by JustSuds
My message loop (in my APIENTRY _tWinMain method) contains this line:

if(msg.message == WM_KEYDOWN)
g_Game->DoInput(msg.wParam);
That should be in your window procedure, not in your main loop. That may well be why it's not working correctly.

Quote:
Original post by JustSuds
Then in my game class i have an if cycle in an otherwise empty DoInput(WPARAM wParam) method:

[snip]

if (wParam == 'A')
lookAtPosX += 0.1f; //Bit that isnt working!
You probably want to store an array of bools, one for each key, which you set to true when you get WM_KEYDOWN for that key, and set to false when you get a WM_KEYUP for that key. That way, you can tell at any time what keys are pressed by looking at the array. If you just move when you get a WM_KEYDOWN message, your movement will be determined by the keyboard repeat rate (Since Windows sends a WM_KEYDOWN message every time a key repeat happens).

Quote:
Original post by JustSuds
And, whats the code for a code block on these forums?
[ source ]Code here[ /source ]

Share this post


Link to post
Share on other sites

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