-1

I would like to start a python script by using the keyboard shortcut Ctrl+Shift+H.

When the script starts, the keys Ctrl and Shift may still be pressed (depending on how quickly I remove my fingers).

I would like to be sure that these two keys are released before executing the rest of the script. To do that I need to test if the key are pressed.

Note that triggering an event when the key are released would not be enough as the keys may not be pressed when the script starts.

I found several references to detect when a key is pressed or released in python but I did not find one to test if a key is pressed.

Do you know how I could do that?

EDIT: Here is a minimal example. You can find here a file in the cnee format which enables to write "azerty" in the window which has the focus. If you type the following command in a terminal, "azerty" is typed in it as expected:

cnee --replay  -sp 0 -f ./recorded_macro.xnl

In order to write "azerty" in any window (not just terminals), I create a keyboard shortcut Ctrl+Shift+H which executes this command. If I use the shortcut, the keys Ctrl and Shift are likely to be pressed when the command is executed (except if I use the shortcut very quickly) and instead of typing "azerty", the window with the focus will get Ctrl+Shift+a, Ctrl+Shift+z, Ctrl+Shift+e, Ctrl+Shift+r, Ctrl+Shift+t and Ctrl+Shift+y which will trigger actions in the window but will not write "azerty".

So, I would like instead to start a python script with this shortcut which waits for Ctrl and Shift to be non-pressed before executing the cnee command. Note again that waiting for the keys to be released with a listener is not a solution as the keys Ctrl and Shift may not be pressed at the start of the python script if I use the shortcut quickly (and so I will have to press again Ctrl and Shift before executing the cnee command which I do not want).

Zach
  • 600
  • 5
  • 16
  • 1
    What do you mean "I found several references to detect when key is pressed or released.." *yet* "but I did not find one to test if a key is pressed" - what's the difference? – Collin Heist Nov 02 '20 at 19:54
  • 2
    Without a [example] it's hard to help However you could use Python libraries **keyboard** and **pynput** like [already answered here](https://stackoverflow.com/questions/24072790/detect-key-press-in-python). – hc_dev Nov 02 '20 at 20:05
  • @CollinHeist If the keys `Ctrl` and `Shift` are not pressed when the script starts and if the script waits for them to be released, it will require me to press these buttons again (I do not want that). However, if I can test if the key are pressed, I can wait if they are pressed and I can go on if they are not. – Zach Nov 03 '20 at 07:18
  • 1
    @hc_dev Thank you for the link. However, it detects when a key is released or pressed but not IF a key is pressed. You can see in my edit why it is important to me. The edit also provides a minimal example. – Zach Nov 03 '20 at 07:40

1 Answers1

2

With pynput

Controller().release() from the pynput.keyboard library enables to release buttons:

import subprocess
import time 
from pynput.keyboard import Key, Controller

time.sleep(1)

keyboard = Controller()
keyboard.release(Key.shift)
keyboard.release(Key.ctrl)

subprocess.call(["cnee",  "--replay", "-sp",  "0", "-f", "/home/user/recorded_macro.xnl"])

This solution is great as it does not even require to wait for the buttons to be released.

With keyboard (requires root privileges)

keyboard.is_pressed() from the keyboard library enables to test if the buttons are pressed:

import time 
import keyboard

while keyboard.is_pressed('Shift') or keyboard.is_pressed('Ctrl'):
    time.sleep(0.1)

This library however requires to be root and I am not sure I can trigger a root command with a shortcut.

Thanks @hc_dev for the pointers.

Zach
  • 600
  • 5
  • 16