• C

obtaining multiple keys at once

I am programming a simple VR tank simulation and I have run into a rather large bottleneck. Standard key-grabbing functions can only access one key at a time. This makes driving the tank forward and turning impossible. How can I access more than one key at once? I know getch() and cin only grab the current key. Is there anyway to access all currently depressed keys from the key buffer? I am attempting to have my application portable so I wanted to avoid assembly and any MFC calls if possible. None the less if necessary I would gladly use MFC as a solution.
Who is Participating?
jkrConnect With a Mentor Commented:
As you stated you'd be glad to use a MFC solution, I assume your platform is windows.

You can simply use the 'GetKeyState()' API, which does exactly what you want:

(quote from the docs)
"GetKeyState (Win32)

SHORT GetKeyState(nVirtKey)

int nVirtKey;      /* virtual-key code      */

The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the
key is up, down, or toggled (on, off--alternating each time the key is pressed).

Parameter      Description

nVirtKey      Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or
0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must
be a virtual-key code.
      If a non-English keyboard layout is used, virtual keys with values in the range ASCII A
through Z and 0 through 9 are used to specify most of the character keys. For example, for
the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the 'o' key,
whereas VK_OEM_1 refers to the 'o with umlaut' key. "

Feel free to ask if you'd like to have an example!
jean_pierreAuthor Commented:
Edited text of question.
jean_pierreAuthor Commented:
I was really hoping for a non-platform specific response (the application runs on IRIX SGIs) but this will definitely do the trick on the windows boxes. Is it possible to see an example of its usage? Where can I find the API and its documentation?

Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

Sorry for the delay, but it's Europe here and - so to say - in the middle of the night ;-)

The API docs can be found in the MS SDK documentation, but I'll post a link to the MSDN article if you need it...
jean_pierreAuthor Commented:
that would be great thanks. I have also been told that the function getch()ekey could possibly achieve the same result, but unfortunately I have not been able to find any info on that either. So at this point there isn't really any standard c or c++ method of multi-key grabbing?

thanks again
Sorry for the delay, had to visit customers yesterday :-(

Here's the MSDN documentation:


>>So at this point there isn't really
>>any standard c or c++ method of multi-
>>key grabbing?

No, this is highly platform specific...

BTW: Using 'getche()' would mean 'polling', and this is a no-no nowadays.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.