What are the possible reasons why my directinput code can recognise normal keys like A-Z but it just doesnt recognise arrow keys like DIK_UP,DIK_LEFT,DIK_RIGHT,DIK_DOWN.
What are the possible reasons why my directinput code can recognise normal keys like A-Z but it just doesnt recognise arrow keys like DIK_UP,DIK_LEFT,DIK_RIGHT,DIK_DOWN.
change the keystate array to a 'char' instead of 'unsigned char' array.
I usually do it with:
the 'keyboarddev' should be your LPDIRECTINPUTDEVICECode:BYTE tec[256]; m_keyboarddev->GetDeviceState(256,&tec); if(tec[DIK_UP] & 0x80) {/*is the up arrow key*/} /*do similar with the other keys*/
Niara
This is my code. It just can't recognize arrow keys, home,end, delete,page up and page down. Maybe there are more.Code:class Keyboard : public Controller { public: Keyboard(LPDIRECTINPUT8 pDI, HWND hwnd); ~Keyboard(void); bool shutdown(); bool read(); void clear(); bool buttonDown(char key){ return (m_keys[key] & 0x80) ? true : false; } bool buttonUp(char key){ return (m_keys[key] & 0x80) ? false : true; } private: char m_keys[256]; LPDIRECTINPUTDEVICE8 m_pDIDev; };
A little observation, sometimes the easyest part is missed: you should call the button down function, hope that it won't execute for itself .
I do the next: first process the keyboard and mouse operations, then render all; it is in the game's loop. something like:
and the procKey() should be (the same as before):Code:while(msg.message!=WM_QUIT) { fMessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE); if(fMessage) { TranslateMessage(&msg); DispatchMessage(&msg); } else { yourkeyboardclasspointer->procKeyb(); Render(); } }
if you call that function from the WM_ messages I think that will get a little delay. it works for me.Code:void yourkeyboardclass::procKeyb() { BYTE tec[256]; m_keyboarddev->GetDeviceState(256,&tec); if(tec[DIK_UP] & 0x80) {/*is the up arrow key*/} /*do similar with the other keys*/ }
Niara
I had almost the exact same problem a couple days ago. You need an array of unsigned chars (and make sure the function also takes an unsigned char)
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Thanks guys.
I forgot to mention that I did call the function in my game loop.
And indeed, it is the unsigned char problem.