1

I want to make a version of cin that works in a graphics display window (it's SDL, but I don't think that matters here): that is, when you type, the characters echo into that window rather than the console window. The solution should be cross-platform.

I have this page to tell me how to get characters NOT to echo to the console window: Reading a password from std::cin

...but what I don't know how to do is to make the characters echo on the new graphics display window.

One thing I could do is intercept keyboard events, and if one happens, print the character. But this wouldn't perfectly echo the actual behavior of the istream buffer, because of repeating keys, backspace, enter, tab, and also the real behavior of cin in that if you are typing before C++ gets to the cin, it will put that stuff you typed on the screen at that point.

I think this echoing is done inside the call to _read (read in the Unix world), and that I'm not sure how to access.

TIA.

Community
  • 1
  • 1
Topological Sort
  • 2,733
  • 2
  • 27
  • 54
  • Graphical applications typically doesn't use `std::cin` for input, instead they *do* intercept key events, and handle buffering and special key themselves. – Some programmer dude Jun 11 '13 at 17:42

2 Answers2

0

The behaviors you're talking about are not done by cin nor read() syscall; buffering and processing of special character is done by the terminal emulator and the shell.

You do need to intercept key events and implement these yourself. Alternatively, some terminal emulators (e.g. VTE widget in Gnome) were designed so they can be embedded into another program. You might want to look at that option.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
0

Considering your application is running in a window you shouldn't read input from the console . It's probably possible but you'll have to switch to the terminal window from where you started the window each time you want to input text. If you take this route you need to disable tty echo: http://man7.org/tlpi/code/online/dist/tty/no_echo.c.html (for Linux). Check out http://www.cplusplus.com/forum/general/12256/ for some solutions for Windows too. I don't think there is a solution that works for both so you'll need to bury some #ifdef's in some utility functions.

Since you're using SDL you should probably use SDL's input functions.

Check out http://www.libsdl.org/docs/html/guideinputkeyboard.html

And, more specific to your needs: http://wiki.libsdl.org/moin.fcg/Tutorials/TextInput

ctn
  • 2,887
  • 13
  • 23