The BEST and most straightforward way to read key inputs??

Forum members,

I have seen MANY topics on capturing multiple (simultaneous) keystrokes inside a Windows application here and frankly there are many great suggestions==> GetAsyncKeyState(), ReadConsoleInput(), and many others.

We could debate this for a lifetime I'm sure but I wanted to know what you all think what is the BEST or FASTEST (Easiest?) way to do this in a Windows program? I know that BEST & FASTEST are not always necessarily mutual and are more often than not mutually exclusive..... but I wanted to hear the best approach from the learned members here.

I'm merely trying to read a MAXIMUM of only TWO (2) buttons simultaneously pressed at one time using virtual key codes in order to carry out sub-routines when the combinations are analyzed. The keyboards are integrated into the laptops so there are no other protocols happening for USB or what have you.

Let the advice and debate war begin! I'm interested in your guidance to do it right - but then to also not take forever.

Xanadu
sounds like you dont actually want the input, but need to check the state of the keys on the keyboard. Regular input functions wont let you know if keys are up/down but just give you the values like "a" "b" "c".

GetAsyncKeyState() sounds like a fair match
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx



Jay,

I have followed your advice to the "T" sir and you are correct - I want to know the button STATES of "pressed" or "not pressed" and figured this way (GetAsyncKeyState) would be best also. Let me give you some more detail==> Using MSVC 2017 (Community Edition) to make this program. Used a "blank" solution template to start (NO Windows App or Console App template).

I found some very useful advice on here of how to use GetAsyncKeyState() and definitely borrowed the code correctly. Code builds fine BUT when I go to run it - the "window" opens and closes very quickly and then MULTIPLE exceptions are thrown very quickly and there seems to be a large list of "failed to open file" type messages and each of these messages have a .dll file associated with it!!

I am NOT a solid windows programmer by any means - any idea of what I am possibly doing wrong here? Should I have made this program as a Console APP template or is there something else going wrong here?

Xanadu
insert a breakpoint at the end of your code, that will stop the window closing and allow you to see any output that the program generated, this is a normal issue on windows, applications close their window automatically when they finish (for good or bad).

copy and paste your error/exception messages here, there's no telling what they are without us seeing them.

also, as you didn't use an application wizard please post your declaration of main() so we can see that too.

Jaybob,

I found out what the issue was==> it was sloppy programming! I had started passing data to the GetAsyncKeyState() that was NOT const controlled and was NOT following clean C++ protocols......even though the build was "clean" by MSVC standards (LOL) the executable definitely fell apart. I later reasoned that the class's data had NO reason to be changed after initialization by the constructor. So I changed the CONST-ness of the actual objects and the functions that worked with the object data and the problems went away instantly.

I think by using arguments that were not strictly controlled by my class declaration (I was essentially reverting to standard "C" practices) and allowing them to be altered by NOT constructing my functions to just use const MyClass & arguments - I ended up with a memory issue- I think. I can definitely send you the BAD code and then the CLEAN code if you are still wanting to see. I will definitely send the code next time!!!!

If I DO want to poll the keyboard for actual keystroke values (VK values) and point to that value and then evaluate it in a switch statement rather than just know the Boolean state of a particular key- what function would you recommend instead of GetAsyncKeyState which demands a predefined keycode argument ??

Thanks for your help so far Jay.... much appreciated.

Xanadu

you could scan for key_up signals, or key down signals depending whether you want to act when the key is pressed or released.

or

detect when a key is pressed then get it,
1
2
3
#include <conio.h>
if ( kbhit() )
        key_code = getch();
Last edited on
Jaybob,

Awesome! I will consider this topic solved then. I appreciate the insight and I will probably implement the solution you provided in your last post to make sure we can read the specific key and act upon it. I'll probably have other questions for you as I go along since I am not a great Windows programmer as I mentioned - but you obviously have had to deal with Microsoft's operating environments for some time and your knowledge of the pitfalls is much appreciated.

I wanted to ask you before I close this topic out..... does getch() use virtual key codes for the key strokes or does it use Unicode or what have you instead? Just making sure I don't check for a value that is not going to work.

Thanks again Jay for all your help and I appreciate it greatly.

Xanadu
Topic archived. No new replies allowed.