12

I keep finding ways to map the backspace key differently, but that's not what I'm after.

I'm in a program writing a python code, and basically I want to write a line of code that causes the program to think someone just hit the Backspace key in the GUI (as the backspace key deletes something)

How I would code in a backspace key stroke?

spelchekr
  • 933
  • 3
  • 11
  • 19
aescript
  • 1,775
  • 4
  • 20
  • 30
  • What context are the keystrokes taking place in? A Tkinter Text widget? Some other GUI framework? – Brionius Aug 19 '13 at 18:22
  • maybe i should specify a bi more. Im not actually deleting a character here using \b. Basically in this program think of it as image tracking, and it places a mark on high contrast points of an image. When you adjust the error threshold it hilights points with a high error. Pressing backspace on the GUI viewer deletes the selected points. Thats what i want. – aescript Aug 19 '13 at 19:45
  • Ok, then you need an event listener. For example, in Tkinter, you would bind a function to [the `` event](http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm). It's hard to help you any more unless you tell us what framework you've written your GUI in. – Brionius Aug 20 '13 at 00:30

5 Answers5

8

The character for backspace is '\b' but it sounds like you want to affect the GUI.

if your program changes the GUI, then simply delete the last character from the active input field.

Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
  • maybe i should specify a bi more. Im not actually deleting a character here using \b. Basically in this program think of it as image tracking, and it places a mark on high contrast points of an image. When you adjust the error threshold it hilights points with a high error. Pressing backspace on the GUI viewer deletes the selected points. Thats what i want. – aescript Aug 19 '13 at 19:45
  • delete_points() is what? Theres no actual script command for deleting the points that i can call out. I need the keystroke automated. I dont mean i want to cause backspace to have an action. I want to make it so as my script cycles through it will analyze, then delete points, move to next frame, analyze, delete etc - But again - theres no script that it gives me access to to cause the delete_points() action. what i want would be something like: blahblah['addAnalysysKey'].execute() pythonCommandToCauseKeysroke['\b'].stroke() etccc – aescript Aug 19 '13 at 21:31
  • So you're trying to inject a backspace into the program's keystrokes? That depends on your program and environment. If you're talking about a Windows app, you need to ask Windows GUI people. – Brent Washburne Aug 19 '13 at 21:38
8

and i got it !

import sys

print('\b ', end="", flush=True) 
sys.stdout.write('\010')

it backspace !

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
  • This worked for me!! Example: trying to erase the final comma and place a period. ======= def fibonacci(f): x = z = 0 y = 1 while z <= f: if z == 0: z = z+x else: z = x+y if z <= f: print("{}, ".format(z), end="") x = y y = z else: print('\010\010.') print("fibonacci") fibonacci(30) – Mohammad Aug 15 '21 at 16:07
5
foo = "abc"
foo = foo + "\b" + "xyz"
print foo
>> abxyz
print len(foo)
>> 7

if key == '\b': delete_selected_points()
Jiminion
  • 5,080
  • 1
  • 31
  • 54
  • maybe i should specify a bi more. Im not actually deleting a character here using \b. Basically in this program think of it as image tracking, and it places a mark on high contrast points of an image. When you adjust the error threshold it hilights points with a high error. Pressing backspace on the GUI viewer deletes the selected points. Thats what i want. – aescript Aug 19 '13 at 19:47
  • What Brent said, unless I'm missing something. – Jiminion Aug 19 '13 at 20:03
4

As other answers have said, use '\b' to backspace. The trick in your case is to use sys.stdout.write instead of print to not get a newline appended. Then wait and print the appropriate number of backspace characters.

import time
import sys

print("Good morning!")
while True:
    time_fmt = "It's %I:%M:%S %p on %A, %b %d, %Y"
    time_str = time.strftime(time_fmt)
    sys.stdout.write(time_str)
    sys.stdout.flush()
    time.sleep(1)
    sys.stdout.write("\b"*len(time_str))
Joseph Sheedy
  • 6,296
  • 4
  • 30
  • 31
0

Updating since this still pops up in search. In Python3 print() can and does work if you use the \<end\> parameter. Meaning sys.stdout.write() and .flush() aren't needed.

eg. print("\b"*len(time_str),end='')

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
th aw
  • 1