4

I'm trying to write a small script with Autokey (not regular Python) on Linux Mint which presses a single key and stops after I press another specific key but I can't get it to stop the loop after I press this specific key.

I got the loop working but I can't make it stop.

import time
a = True
b = keyboard.press_key('s')
keyboard.release_key('s')
while a:
    keyboard.send_key("a", repeat=5)
    time.sleep(2)
    if b:
        break

So this outputs the letter "a" indefinitely and after I press "s" it doesn't stop and I don't know what I'm doing wrong

I read about the while function and break but all the examples I found were with a loop stopping after it reached a certain number and these examples with numbers are different than what I try to achieve with this kind of script so I hope someone can help me to figure this out.

Andria
  • 4,712
  • 2
  • 22
  • 38
minionlou
  • 41
  • 2
  • Hi, can you provide your full code with all imports and whatnot? – Andria Aug 19 '21 at 20:14
  • This is the full code, I wrote this to practice – minionlou Aug 19 '21 at 20:16
  • 1
    `b` never changes. – Barmar Aug 19 '21 at 20:17
  • 2
    @minionlou There is no way this is the full code. When I paste this into my IDE and run it, I get `NameError: name 'keyboard' is not defined`. Which means at the bare minimum you are missing an import statement. – blackbrandt Aug 19 '21 at 20:18
  • This code doesn't work then. It says that `keyboard is not defined` when run. – Andria Aug 19 '21 at 20:18
  • It's running for me, I wrote this inside of Autokey not inside python or the terminal. Maybe that's why it's not working for you? – minionlou Aug 19 '21 at 20:20
  • To all: This is the documentation of the `Keyboard` class in AutoKey. https://autokey.github.io/index.html – Barmar Aug 19 '21 at 20:20
  • The AutoKey Keyboard class is for sending keyboard events. It doesn't look like it's for detecting the user typing. – Barmar Aug 19 '21 at 20:22
  • It has `keyboard.wait_for_keypress()`, but it looks like it blocks until the user types something, so it won't be useful here. – Barmar Aug 19 '21 at 20:23
  • @Barmar ```import keyboard``` works fine for this. – Lakshan Costa Aug 19 '21 at 20:45
  • I can't test this on my machine (WSL) but you might be able to use the builtin `threading` library to run `keyboard.wait_for_keypress('s')` on its own thread and when it's done you could use some form of inter-thread communication to change the condition of your while loop to false thus stopping the sending of keypresses. – Andria Aug 21 '21 at 19:20

3 Answers3

4

You will have to use the keyboard module for this, because press_key is used to "press" the keys not to detect.

If you haven't already installed keyboard you can do it by going to cmd, pip install keyboard

after that you can add the code in python as follows, pressing "q" will print "a" 5 times and pressing "s" will stop the program.

import keyboard
while True: 
   if keyboard.is_pressed('q'):  # pressing q will print a 5 times
      for i in range(5):
         print("a")
      break  
   elif keyboard.is_pressed('s'): # pressing s will stop the program
      break
Lakshan Costa
  • 623
  • 7
  • 26
0

You can check if a key is pressed with evdev

Check your InputDevice by looking at python -m evdev.evtest

Then to check if the s key is pressed :

import evdev
from evdev import ecodes as e

device = evdev.InputDevice('/dev/input/event7')

if e.KEY_S in device.active_keys():
    do_something()
DevAb
  • 530
  • 6
  • 12
-2

At first glance your problem is that you never update the value of b and it is only assigned before the loop. You should probably try something like this:

import time
a = True
keyboard.release_key('s')
while a:
    keyboard.send_key("a", repeat=5)
    b = keyboard.press_key('s')
    time.sleep(2)
    if b:
        break

I don't know how "b = keyboard.press_key('s')" affects the code, if it stops it.

  • 2
    This doesn't answer the OPs question. `keyboard.press_key` is documented as pressing a key on the keyboard and not checking if it is pressed or not. Please use comments for things that are not solutions. – Andria Aug 19 '21 at 20:23