0

I'm looking to capture single USB keyboard events within a "while True" loop which includes a timer function as well. Evdev is close to what I need but it's device.read_loop does not allow for inclusion of a clock function - it's a closed loop. Any ideas as to how to capture a single USB keyboard event which I can control when it is checked? I'm using Python 3.4 so asyncio is not an option. Thank you.

  • What have you tried so far? – Stedy Apr 30 '19 at 03:12
  • The application is a RPi being used as an internet radio with a sleep timer. My timing loop uses time.time to count seconds and it needs to be checked at least once a second. I've tried using "break" to exit the read_loop (exits the program instead); inserting my timing code into the read_loop(doesn't work); asyncio, keyboard, and select(s) libraries (RPi won't load libraries). Many hours spent on this. Any other ideas? Thanks! – Jefferson90 May 01 '19 at 10:25
  • Can you please post the code you have written so far? Ideally in the form of an [mcve] – Stedy May 01 '19 at 13:53
  • Will post it as soon as I can - life is getting in the way of coding. May take a few days until I can post. Thanks – Jefferson90 May 02 '19 at 01:10

1 Answers1

0

Threading will help you here. As Python Module of the Week (PyMOTW) says:

Using threads allows a program to run multiple operations concurrently in the same process space.

In your case you can still read the keyboard input in a blocking loop in its own thread and let the sleep function check the time in another thread without getting blocked by the read_loop of evdev. Just set the radio_sleep_time to the amount of seconds you want to wait until the radio sleeps (you can use minutes and radio_sleep_time = 4 * 60 instead to get 4 minutes).

from time import time
from threading import Thread
from evdev import *

radio_sleep_time = 4  # sleep time for radio in seconds
device = InputDevice('/dev/input/event3')  # pick the right keyboard you want to use


def read_kb():
    for event in device.read_loop():
        # only use key events AND only key down can be changed depending on your use case.
        if event.type == ecodes.EV_KEY and event.value == 1:
            keyevent = categorize(event)  # just used here to have something nice to print
            print(keyevent)  # print key pressed event


def wait_loop():
    pass  # whatever radio is doing when its waiting to sleep if anything.


class Time1(Thread):
    def run(self):
        while True:
            read_kb()


class Time2(Thread):
    def run(self):
        t0 = time()  # starting time
        # time() (current time) - t0 (starting time) gives us the time elapsed since starting
        while not time() - t0 > radio_sleep_time:  # loop until time passed is greater than sleep_time
            wait_loop()  # do sleep stuff
        print(time() - t0, ">", radio_sleep_time)
        print("SLEEP")
        # sleep radio here
        # but continue to listen to keyboard


Time1().start()
Time2().start()
FoxSam12
  • 179
  • 1
  • 9