Pergunta

I'm trying to create a little Python/curses app.

But as far as I can see there's no way to tell whether CTRL+J or Enter have been pressed. Now this may be caused by the fact that they both have the same ascii code (10):

http://en.wikipedia.org/wiki/Control_character#In_ASCII

But how can VIM tell the difference between these two?

Foi útil?

Solução

Enter is usually equivalent to C-m. But, if the icrnl flag is active for the tty (see stty -a), then an input C-m will automatically be translated to C-j (so that it is easy to type Unix-ly terminated lines by just pressing Enter).

In plain C you could use the termios functions tcgetattr(3) and tcsetattr(3) to unset the ICRNL flag in c_iflag so that C-m is not translated to C-j on input. If you want absolute control over the input and output, you would use a “raw” mode (disable all input and output processing). It looks like Python has these termios functions.

The curses library has some higher-level functions for dealing with tty modes: savetty(3), resetty(3), nonl(3), raw(3), cbreak(3), etc. It also looks like Python has these curses functions.

If you are using other bits of the curses library, then it is probably best to also use its functions to adjust the ICRNL flag (e.g. nonl(3)) to avoid breaking any assumptions that the library has made (i.e. it assumes the tty is set one way, but your termios-level calls change things and break that assumption).

Outras dicas

I don't think <ctrl-J> and Enter are same. In vim, <ctrl-j> has keycode 10, but Enter has 13.

There are some equivalent keys, e.g.

<ctrl-M> and Enter
<ctrl-H> and backspace
<ctrl-[> and ESC
...

you can test in your vim, <ctrl-M> and Enter does the same thing, no matter which mode are you in.

Actually, there is another key combination, which is also hard to distinguish with <Enter> and <Ctrl-M>. it is <C-Enter> I don't know what you want to achieve, you said you want to do some terminal development (ncurses), if the terminal even cannot distinguish those keys, you can just leave it. I don't have experience of ncurses, don't be mad if I was wrong. ^_^

if you type :h keycodes you can see more information, the table below was copied from vim help.

notation    meaning         equivalent  decimal value(s)    ~
-----------------------------------------------------------------------
<Nul>       zero            CTRL-@    0 (stored as 10) *<Nul>*
<BS>        backspace       CTRL-H    8 *backspace*
<Tab>       tab         CTRL-I    9 *tab* *Tab*
                            *linefeed*
<NL>        linefeed        CTRL-J   10 (used for <Nul>)
<FF>        formfeed        CTRL-L   12 *formfeed*
<CR>        carriage return     CTRL-M   13 *carriage-return*
<Return>    same as <CR>                *<Return>*
<Enter>     same as <CR>                *<Enter>*
<Esc>       escape          CTRL-[   27 *escape* *<Esc>*
<Space>     space                32 *space*
<lt>        less-than       <    60 *<lt>*
<Bslash>    backslash       \    92 *backslash* *<Bslash>*
<Bar>       vertical bar        |   124 *<Bar>*
<Del>       delete              127
<CSI>       command sequence intro  ALT-Esc 155 *<CSI>*
<xCSI>      CSI when typed in the GUI       *<xCSI>*

<C-J> is equivalent to <NL>, not <CR> (which corresponds to <C-M>):

:echo "\<C-J>" ==# "\<CR>"
0
:echo "\<C-J>" ==# "\<NL>"
1

So your assumption is wrong; even Vim cannot differentiate between them. Due to the way that the keyboard input is handled internally, and because of the long and complex history of terminals and their APIs, it's hard to differentiate between those keys, though (in most modern terminal emulators), this is possible today.

Some people (foremost Paul LeoNerd Evans) want to fix the problem in Vim, and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release. I don't know how to solve this problem in Python, but you may find some nuggets of insight from the linked discussion.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top