Python Forum
Simulate Undo keyboard command in code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simulate Undo keyboard command in code
#1
Hello,

Is there a way in python to simulate a keyboard command within the code.

For example:

I created an Icon button for Undo. Each time I press the button with a mouse click, I want it to execute the Conrol + Z keyboard press which would represent the Undo command.

Is there a way to simulate this with code?

Thanks.

s.
Reply
#2
Hi
Your question is very vague

(May-10-2019, 09:31 PM)sgskge1 Wrote: Is there a way in python to simulate a keyboard command within the code.

Within what code ?

Quote:I created an Icon button for Undo. Each time I press the button with a mouse click, I want it to execute the Conrol + Z keyboard press which would represent the Undo command.

What did you use to create a button, where do you want it to execute the Control + Z keyboard
Reply
#3
(May-10-2019, 09:55 PM)Yoriz Wrote: Hi
Your question is very vague

(May-10-2019, 09:31 PM)sgskge1 Wrote: Is there a way in python to simulate a keyboard command within the code.

Within what code ?

Quote:I created an Icon button for Undo. Each time I press the button with a mouse click, I want it to execute the Conrol + Z keyboard press which would represent the Undo command.

What did you use to create a button, where do you want it to execute the Control + Z keyboard

Thank you for your response, and my apologies for not being more specific in the original post.

I added items to an existing context menu using python code for a text editor. The items are Undo and Redo. Each time I select Undo from the context menu, I want it to execute the Undo command. That would be the equivalent of typing 'Control + Z' on the keyboard. My question is there a way to simulate 'Control + Z' command using python code. Right now, I've set it up to print "hello world" each time I select Undo from the context menu. Ultimately, I want it to execute the Undo edit command ('Control + Z') , each time I select Undo from the context menu.

s.
Reply
#4
It is still unclear if the text editor is an independent application or one thats created by using one of the GUI frameworks.
If its a GUI framework the text editor will most likely have a undo/redo methods.
If its an independent application you can use a GUI automator like PyAutoGUI
Reply
#5
(May-11-2019, 08:05 AM)Yoriz Wrote: It is still unclear if the text editor is an independent application or one thats created by using one of the GUI frameworks.
If its a GUI framework the text editor will most likely have a undo/redo methods.
If its an independent application you can use a GUI automator like PyAutoGUI


I'm new to Python, so let me try explain as best as I can.

I'm using a text editor called Pluma.

Pluma was coded in C (which I'm also unfamiliar with.) However, python plugins have been created that have been added to the Pluma text editor. Within one of the plugins I'm editing, I'm trying to add Undo and Redo functions to the context menu (pops-up on right-click). I've added the labels, but the only thing I can do is select them to print an output statement to screen.

Now, the python plugin I'm working with uses GTK. I can add items to the UI manager using Gtk.Action.add_action, etc. within the python plugin code. Now these items like Undo, Cut,Paste with the UI Menu manager, execute upon selection with a mouse click within the Edit drop-down menu. I want to add Undo and Redo functions to the context menu. Each time I select the Undo or Redo items from the context menu, I want it to execute the same way as if I selected the same Undo or Redo items from the drop-down Edit menu.

I hope this clarifies things.

s.
Reply
#6
As stated for an independent application use a GUI automator.
Reply
#7
Thanks for the feedback. Will look into it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,034 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Help Switching between keyboard/Mic input in my code Extra 1 1,045 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,046 Dec-06-2020, 10:10 PM
Last Post: Aizou
  Undo interation to make a single list? DustinKlent 2 2,133 Nov-29-2020, 03:41 AM
Last Post: DustinKlent
  Help with isinstance command (very simple code) Laplace12 2 1,966 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  What are the available softwares used to edit and simulate network routing protocols? leemao 1 1,882 Dec-25-2019, 05:36 PM
Last Post: Larz60+
  code works at command line but not in .py ridgerunnersjw 4 2,516 Sep-12-2019, 11:09 PM
Last Post: ridgerunnersjw
  Is there any way to simulate clicks/typing to an inactive window using Python? Nwb 1 5,852 Jun-11-2018, 08:56 AM
Last Post: Larz60+
  Trying to simulate different file write speed tbaror 4 4,376 Apr-19-2017, 02:45 PM
Last Post: tbaror

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020