Reading in control keys from the console

I have a c++ application which reads in commands from the user.  The problem is that I want to read control characters.  Some of the control characters are the same as other keys.

For instance, ctrl-j is the same as ENTER, and ctrl-h is the same as backspace.  How can I make it so these keys read differently?  I know it has to be possible since programs like vi handle ctrl-j differently than the enter key.

Thanks!
LVL 5
lwinkenbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahoffmannCommented:
man getc
man tgetstr
man curses
man termcap
man terminfo

which API to use may depend on your application ...
0
lwinkenbAuthor Commented:
I have read all those man pages, and unless I missed something obvious, they don't tell me how to solve my problem.

I'm using ncurses in my application to get keyboard input, but there are no library functions which help me.  I've tried reading keyboard input in raw() mode, but they still come in the same.

I've also tried using 4 different terminal types with no luck (linux, xterm, xterm2, debian).
0
ahoffmannCommented:
are you running your application within X? Then you have to sheck the keysym table too
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

lwinkenbAuthor Commented:
My application is sometimes run in X, but usually it is run from an SSH session or from the command line without X running.
0
Duncan RoeSoftware DeveloperCommented:
Enter sometimes looks like ctrl-J and sometimes like Ctrl-M, it depends on terminal settings (termios). No ascii character-based program can tell which keys you actually pressed. A GUI X-based program can, by handling keypress events, as alluded to by ahoffman
0
lwinkenbAuthor Commented:
>> No ascii character-based program can tell which keys you actually pressed

How does vi do it?
0
Duncan RoeSoftware DeveloperCommented:
Does vi know the difference between Enter and Ctrl-M?
0
lwinkenbAuthor Commented:
I just ran a test and Ctrl-M and Enter were the same.  So how did vi move Ctrl-J to Ctrl-M?  If I could do the same thing, then it would solve my problem.
0
Duncan RoeSoftware DeveloperCommented:
Enter is always the same as Ctrl-M. Sometimes crtl-M gets converted to Ctrl-J. That is achieved by the terminal setting ICRNL (Translate carriage return to newline on input (unless IGNCR is set)). You can do this in your program using tcsetattr(). You should save your terminal settings on entry (use tcgetattr()) and restore them on exit using tcsetattr() (if you use atexit() to set it up, no need to restrict where you call exit()).
see "man termios" or "man tcsetattr" (actually same manual page on my system).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NopiusCommented:
I agree with duncan_roe and ahoffman.
the same ioctl() is done by 'stty' command:

stty eol ^M eol2 ^J

from now ctrl-J and ctrl-M are equivalent in your current tty and can end up your input line.
0
ahoffmannCommented:
> Enter is always the same as Ctrl-M.
disagreed, except "Enter" sends the keycode for CR aka Carriage Return aka Return ;-)

> Sometimes crtl-M gets converted to Ctrl-J.
this is done as i described/alluded, but it is most likely done by either stty or the corresponding termio (termcap/terminfo)

> Does vi know the difference between Enter and Ctrl-M?
vi uses termcap, but only if it contains a special vi entry otherwise it uses a hardcoded dumb setting, it does not use terminfo
Things are totally different if your vim lies to you and anounces itself as vi.
That's why a *real* vi never has problems reading any key defined in US-ASCII :)

Just my 2 pence ...
0
Duncan RoeSoftware DeveloperCommented:
> Enter is always the same as Ctrl-M.
As far as the resultant Ascii - that's all I meant. You agreed with that bit

> Sometimes crtl-M gets converted to Ctrl-J.
Done by the OS (linux) depending on the settings of the serial port. tcsetattr / termio / termios / ioctl are interfaces to the OS to change this behavior

> Does vi know the difference between Enter and Ctrl-M?
I just meant for the asker to try it out and prove to himself that vi couldn't tell the difference (I was fairly confident it couldn't, anyway)
0
ahoffmannCommented:
duncan_roe, no offence meant, you are correct (just one more of my pedantic comments as some things are not that simple, someties, somehow ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.