Python code to cause a backspace keystroke?
Asked Answered
R

5

13

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?

Regan answered 19/8, 2013 at 18:18 Comment(3)
What context are the keystrokes taking place in? A Tkinter Text widget? Some other GUI framework?Clothier
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.Regan
Ok, then you need an event listener. For example, in Tkinter, you would bind a function to the <BackSpace> event. It's hard to help you any more unless you tell us what framework you've written your GUI in.Clothier
B
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.

Brunet answered 19/8, 2013 at 18:23 Comment(3)
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.Regan
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() etcccRegan
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.Brunet
U
9

and i got it !

import sys

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

it backspace !

Unmindful answered 22/2, 2016 at 22:55 Comment(1)
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)Forwardlooking
B
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.

Brunet answered 19/8, 2013 at 18:23 Comment(3)
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.Regan
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() etcccRegan
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.Brunet
K
5
foo = "abc"
foo = foo + "\b" + "xyz"
print foo
>> abxyz
print len(foo)
>> 7

if key == '\b': delete_selected_points()
Kimberykimble answered 19/8, 2013 at 18:23 Comment(2)
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.Regan
What Brent said, unless I'm missing something.Kimberykimble
C
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))
Cantara answered 25/1, 2017 at 22:43 Comment(0)
G
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='')

Gaur answered 11/5, 2021 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.