^H ^? in python
Asked Answered
G

3

6

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?

Greenheart answered 23/1, 2012 at 18:10 Comment(7)
Is GNU readline available at your target platforms?Orchestrate
yes readline-6.2 package is includedGreenheart
Then you should use this instead of raw_inputEloiseelon
Or more precisely, simply import readline to make raw_input() use it automatically. See the docs for information on how to rebind keys etc.Orchestrate
Thank you, I made as you suggested...it works greatGreenheart
@SvenMarnach can you set it as response so that I can set this question as answered?Greenheart
Lol I got a -1 for this question after 1 year... and why?Greenheart
O
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).

Orchestrate answered 29/11, 2013 at 10:43 Comment(3)
It doesn't seem to automatically detect ^H or ^?, at least on my terminal with raw_input. I am getting raw ^H characters printed.Brittaniebrittany
@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.Orchestrate
@SvenMarnach I was able to fix it by changing the iTerm2 keycode for backspace in the "profiles" menu from ^H to ^?Brittaniebrittany
S
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.

Sporocyst answered 5/5, 2012 at 6:47 Comment(0)
A
0

To fix ^? on erase do stty erase ^H

Aquino answered 27/1, 2019 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.