[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

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!
0
lwinkenb
Asked:
lwinkenb
  • 4
  • 4
  • 4
  • +1
1 Solution
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now