Advertisement

Testing for arrow keys..

Started by July 15, 2000 09:58 PM
7 comments, last by supersaiyan 24 years, 5 months ago
How can you check to see if the user has pressed one of the arrow keys using Visual C++ 6.0?
KA...ME...HA...ME...HA!!!
That realy depends on the OS you are using.





#define MY_LIFE BUSY
http://www.crosswinds.net/~druidgames/resist.jpg
Advertisement
If you are programming for directx then use would use direct input, if you are programming for windows then you could use the WM_KEYDOWN message, if you are programming for dos you could start thinking about programming for directx.


/CMN
isn''t MSVC++ a Windows only compiler? if not, what other os''s does it support?

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
quote: Original post by Julio

isn''t MSVC++ a Windows only compiler? if not, what other os''s does it support?



I think he meant to ask whether he was making a console program, a windows program using just windows API calls, or a directX windows program.



Masters Software
If you want to check key asynchronously (i.e. independently of your messageloop you can use

SHORT GetAsyncKeyState(INT vKey)

with a parameter of VK_LEFT, VK_RIGHT, VK_UP or VK_DOWN.
The value returned has the MSB set if the key is pressed and the LSB set if the key has been pressed sometime after the last call to GetAsyncKeyState
Advertisement
In a windows app without using DirectInput I insert the following in my callback message handler.

switch(uMsg)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_UP: break;
case VK_DOWN: break;
case VK_LEFT: break;
case VK_RIGHT: break;
case VK_CLEAR: break;
case VK_F1: break;
case VK_F2: break;
case VK_F3: break;
case VK_F4: break;
case VK_F5: break;
case VK_F6: break;
case VK_F7: break;
case VK_F8: break;
case VK_F9: break;
case VK_ESCAPE: break;
}
default: return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0L;
}

Hope this helps.

IceFire
Just use this macro:

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

then in your prog, just go:

if (KEY_DOWN(VK_RIGHT)
{
// do whatever
}

Hope that helps
Well, yes, the virtual key codes would work for a windows environment, as stated above.

You could also use DirectInput.

But, when it gets down to the nitty-gritty, I believe that you could also test for those keys using their ASCII values, which are 028 through 031.

I think that these are correct:

028 = UP ARROW
029 = DOWN ARROW
030 = RIGHT ARROW
031 = LEFT ARROW

Of course, you might want to double-check those.

- Goblin
"In order to understand the full depth of mankind, you must first seperate the word into its component parts: 'mank' and 'ind'."
- The Goblin (madgob@aol.com)

This topic is closed to new replies.

Advertisement