Keyboard limits or such in C++

Okay, I don't know how exactly I can explain this, but I've been searching here and there, and didn't find what I wanted.

Do you know how can I allow pressing some keys on the keyboard in C++? I mean, when you are in the console and you are only allowed to press numbers before pressing enter, for example.
Last edited on
You want to know how to tell when a user presses a key, such as right arrow?
Or are you trying to restrict the user from pressing some keys?
The second one, I want to restrict the user from pressing some keys, such as the spacebar.
It looks like there may be some OS specific ways of doing this, but I'm not sure how effective they are. It would be easier to let the user type whatever they want, and just ignore everything you don't want, like spaces.
What Yay295 said.

It's by far more user friendly to tell him what he did wrong instead of suppressing some keys.
Yeah, I know that it's easier that way, but, that's not what I need to do. And I also know that using "GetAsyncKeyState" and "SetKeyboardState" I could do that, but I don't understand how to.
Er, what exactly are you trying to accomplish?

If it is just something like:

Please enter a number> _

and only letting number keys get passed through:

Please enter a number> 1234_

then what you want to do is enable unbuffered input and simply ignore keys other than 0 through 9 (and break keys else your users will hate you).

You can SetConsoleMode() to do this on Windows. It isn't quite as simple on *nix (but for a different reason than you may think -- you must take care to handle input in a much more involved way than you would otherwise).

Another useful possibility is to use NCurses/PDCurses.

Keep in mind that either option will defeat the general use of your program by requiring a human to be present to use it.


If you are trying to mess with other running processes then I won't help. It can be done, mind you, but you shouldn't.

Hope this helps.
Topic archived. No new replies allowed.