1

I am trying to use pyautogui to issue a series of commands by a keyboard shortcut.

The script looks like this:

#!/usr/bin/python3
import pyautogui
# Keyboard sequence to delete current line of text
pyautogui.press('end')
pyautogui.hotkey("shift", "home")
pyautogui.press('backspace')

If I by my terminal write sleep 1 && ./pyautoguitest.py and quickly switches the an other window, it works fine and deletes the current line that the courser is on.

However when I goto keyboard shortcuts and bind Shift + Alt + Backspace to the command /usr/bin/python3 /home/user/pyautoguitest.py it fails silently... nothing happens at all.

I am using Pop_OS! which should handle it the same way as on Ubuntu.

Has anyone tried getting pyautogui to work an a keyboard shortcut?

Just to rule out a permission issue with running the script I have given it 777 permisions, so all users should be able to run it

Update ------ I tried to use logging in the python script to see if anything failed

#!/usr/bin/python3
import logging
import traceback
logging.basicConfig(filename='/tmp/myapp.log', level=logging.DEBUG, 
                    format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)

try:
        import pyautogui
        import sys
        pyautogui.press('end')
        pyautogui.hotkey("shift", "home")
        pyautogui.press('backspace')
except Exception as e:
        logger.error(traceback.format_exc())
else:
        logger.error("all clear")

When I execute the file with sleep 2 && ./pyautoguitest.py the log file gets an all clear entry signaling that everything went fine, when I do it by the keyboard shortcut the same thing happens.

Very strange, it's like something is preventing it running the keys when used as a keyboard shortcut

4
  • This sounds a bit like an XY problem - your actual problem is that you want a "kill line" functionality in an editor, I assume. It'd probably help to know what program you want to do this with - so we can suggest alternative ways aswell, instead of fixing this.
    – Panki
    Commented Jun 26, 2020 at 12:52
  • I am actually trying to get a universal kill line shortcut, as I used to use it across all my programs in Mac
    – Alexander
    Commented Jun 26, 2020 at 13:05
  • Why is this question tagged ubuntu then?
    – Panki
    Commented Jun 26, 2020 at 14:31
  • Because its on pop_os which is built from Ubuntu? I am not using mac, I am trying to get the same functionality as I had on the mac with "cmd + backspace" for deleting a line universally
    – Alexander
    Commented Jun 26, 2020 at 17:03

1 Answer 1

0

I recently added a Custom Shortcut to insert an email-address and I used xdotool to insert the keystrokes. Therefore install the package xdotool from your package-manager and in the system settings go to Shortcuts - Custom Shortcuts, and add a new global shortcut (command/URL)

There you insert following command: sleep 0.3 && xdotool key End Shift+Home Clear

If you want to insert an email: sleep 0.3 && xdotool key t e s t at e x a m p l e period c o m

The sleep 0.3 is necessary because when you press your shortcut on the keyboard, the command will execute immediately and the (still) pressed keys on the keyboard will interfere with the keys from the code. Probably this is also the problem in your case?

2
  • Thanks for your answer @Apfelkuacha, I tried this prior to using pyautogui, but it did'nt work for me in my editors, only my mail program and browser. Have you tested this in something like Atom, Pycharm, PhpStorm, WebStorm and is it working for you? btw I tried with you solution and it did'nt work at all on my distro, what distro are you using?
    – Alexander
    Commented Jul 2, 2020 at 0:09
  • I have tested it on Atom and Kate Editors, LibreOffice, the Console, .. for me it is working. I was using Kubuntu and since some weeks I do use Manjaro
    – Coden
    Commented Jul 7, 2020 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .