DirectInput Problem

Started by
10 comments, last by JustSuds 14 years, 2 months ago
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.
Advertisement
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.
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?
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.
'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?
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)
It sounds like lookAtPosX is changing, it's just not having any visible effect on your application.

Try using a debugger to see if this is the case, and if so, to find why changing the variable is not changing the camera as expected.

The tags are [source]...[/source].
The only issue I have with that theory, is that
if (wParam == 'A')   camPosX += 0.1f;

Doesnt work either
Are you calling TranslateMessage as part of your message loop?
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.

This topic is closed to new replies.

Advertisement