views:

123

answers:

2

Hi.

I'm writing a curses application in Python under UNIX. I want to enable the user to use C-Y to yank from a kill ring a la Emacs.

The trouble is, of course, that C-Y is caught by my shell which then sends SIGTSTP to my process. In addition, C-Z also results in SIGTSTP being sent, so catching the signal means that C-Y and C-Z are not distinguishable (though even without this the only solutions I can think of are extremely hackish).

I know what I'm asking is possible (in C if not in Python), since Emacs does it. How can I disable the shell's special handling of certain control characters sent from the keyboard and have the characters in question appear on the process' stdin?

A: 

For basic functionality, use tty. For example, calling tty.setraw(sys.stdin) will put standard input's terminal into raw mode.

For the more general case, Python comes with a termios library, but you probably need some experience with termios to know how to use it.

Alternatively, a cheap way is to shell out to stty, which is a command-line interface to termios.

Chris Jester-Young
+2  A: 

See the termios module, and the termios(3) man page.

Ignacio Vazquez-Abrams