Console Window Input

Hi, I was wondering if there's a way to limit the amount of characters a user can input WHILE they are typing in the console window?

Cheers.
Yes, but it requires some effort and the answer will vary depending on the platform(s) you wish to support.

How badly do you want to do it?
Bad.. I'm using Visual Studio 2005 and it has to run on an XP console window.

Thanks!
Google "msdn console functions" and hit "I'm feeling lucky."

You'll want to
1
2
  HANDLE hConsole = GetStdHandle( STD_INPUT_HANDLE );
  SetConsoleMode( hConsole, GetConsole( hConsole ) & ~ENABLE_LINE_INPUT );

to turn off line-buffering, and read input one character at a time with ReadConsoleInput, and just discard unwanted characters.

I've never done this: but if you want to be tricky, you can then useWriteConsoleInput to reload the input buffer with the desired input and use std::cin to read the characters normally.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.