5

Some terminals will send ^? as backspace, some other terminals will send ^H. Most of the terminals can be configured to change their behavior. I do not want to deal with all the possible combinations but I would like to accept both ^? and ^H as a backspace from python.

doing this

os.system("stty erase '^?'")

I will accept the first option and with

os.system("stty erase '^H'")

I will accept the second one but the first will be no longer available. I would like to use

raw_input("userinput>>")

to grab the input.

The only way I was able to figure out is implementing my own shell which works not on "raw based input" but on "char based input".

Any better (and quicker) idea?

Community
  • 1
  • 1
Jekyll
  • 1,434
  • 11
  • 13

3 Answers3

5

The built-in function raw_input() (or input() in Python 3) will automatically use the readline library after importing it. This gives you a nice and full-feautured line editor, and it is probably your best bet on platforms where it is available, as long as you don't mind Readline having a contagious licence (GPL).

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • It doesn't seem to automatically detect `^H` or `^?`, at least on my terminal with `raw_input`. I am getting raw `^H` characters printed. – ashgromnies Mar 06 '14 at 05:28
  • 1
    @ashgromnies: If you have a correctly configured terminal and a correctly installed version of GNU Readline, this shouldn't happen. Unfortunately, there are so many things that could possibly cause this behaviour that it is alomst impossible to debug remotely. – Sven Marnach Mar 06 '14 at 11:32
  • 1
    @SvenMarnach I was able to fix it by changing the iTerm2 keycode for backspace in the "profiles" menu from `^H` to `^?` – ashgromnies Mar 06 '14 at 18:46
0

I don't know your question exactly. IMO, you need a method to read some line-based text(including some special character) from console to program.

No matter what method you use, if read this character have special mean in different console, you should confront a console(not only system-specific, but also console-specific) question, all text in console will be store in buffer first, and then show in screen, finally processed and send in to your program. Another way to surround this problem is to use a raw line-obtaining console environment.

You can add a special method(a decorator) to decorate the raw_input() or somewhat input method to process special word.

After solved that question

using this snippet can deal with input,:

def pre():
    textline=raw_input()
    # ^? should replace to the specific value.
    textline.replace("^?","^H")
    return textline

To be faster, maybe invoke some system function depend on OS is an idea. But in fact, IO in python is faster enough for common jobs.

chao787
  • 1,760
  • 3
  • 18
  • 20
0

To fix ^? on erase do stty erase ^H

73214
  • 9
  • 1