Python code to trigger a keystroke?

I keep looking for ways to map the backspace key in a different way, but that is not what I want.

I am in a program that writes python code and basically want to write a line of code that makes the program think that someone just hit the backspace key in the GUI (since the backspace key removes something)

How will I write code in the opposite step?

+4
source share
4 answers

The symbol for backspace is '\b' , but it looks like you want to influence the GUI.

if your program changes the GUI, then just remove the last character from the active input field.

+3
source
 foo = "abc" foo = foo + "\b" + "xyz" print foo >> abxyz print len(foo) >> 7 if key == '\b': delete_selected_points() 
+4
source

and i got it!

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

this is backspace!

+4
source

As the other answers said, use '\ b' to return. The trick in your case is to use sys.stdout.write instead of printing so as not to add a new line. Then wait and type in the appropriate number of inverse characters.

 import time import sys print("Good morning!") while True: time_fmt = "It %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)) 
+2
source

Source: https://habr.com/ru/post/1497861/


All Articles