zeth / inputs

Cross-platform Python support for keyboards, mice and gamepads
BSD 3-Clause "New" or "Revised" License
273 stars 88 forks source link

do something when "events" are empty (no user input) #56

Open SmartySmart702 opened 6 years ago

SmartySmart702 commented 6 years ago

Is not an Issue but how can I do something when there was a interval without any user input?

for event in events: do some stuff if not events: #like if events are empty because there was no user input do some other stuff

MFlossmann commented 5 years ago

I have the same issue (and I would call it an issue ;) To talk about it in a more programmatic way: there's no non-blocking way to read input.

trevorablett commented 5 years ago

If you guys are using Linux, I made a fork that handles this issue for gamepads by making the read() call non-blocking. Unfortunately, I don't think it will work on other operating systems (and I haven't tested it with the keyboard or mouse), but the changes I made might be enough to get you started.

https://github.com/trevorablett/inputs/tree/non-blocking-gamepad

frastlin commented 5 years ago

I'm not sure this is the same problem, but it could be related:

while 1:
    events = get_key()
    for event in events:
        print(event.ev_type, event.code, event.state)

freezes on Windows. If events could be a list, this should work. To solve the problem here, you can do what the keyboard example does:

while 1:
    events = get_key()
    if events:
        for event in events:
            print(event.ev_type, event.code, event.state)
    else:
        do some other stuff
redrubymitchell commented 4 years ago

I edited the inputs.py file from the library so it would stream data continuously about the position of the buttons and joysticks on a controller. I only edited the controller section because thats the only part that im using, but the code could be applied to any of the input devices. Basically, I just made it so that an event registers all the time instead of only when something is changed, by commenting out the if oldEvent = newEvent: type lines, and put a deltaTime delay in the sending of the position because if you send all the events without this there is way to many to handle and the program you are reading into will get backed up.

just change it to a .py and it will work like this for gamepads

inputs.txt

fleborne commented 1 year ago

If you guys are using Linux, I made a fork that handles this issue for gamepads by making the read() call non-blocking. Unfortunately, I don't think it will work on other operating systems (and I haven't tested it with the keyboard or mouse), but the changes I made might be enough to get you started.

https://github.com/trevorablett/inputs/tree/non-blocking-gamepad

Thanks a lot @trevorablett , it works like a charm!

wayneworkman commented 1 month ago

I'm having the same issue. My use case involves a small hobby robot that should boot up & start in autonomous mode. If I press the "select" button on my wireless controller, the robot should switch to manual mode. The problem is, if there is no input from the controller, the events = get_gamepad() hangs indefinitely, until any key on the wireless controller is pressed.

I found a workaround that doesn't involve changing the Inputs module. Though this method is too slow for my project because the shortest timer that can be set with this method is one second. Maybe it'll help with someone else's project.

from signal import signal, alarm, SIGALRM
from inputs import get_gamepad
from inputs import devices

def timeout_handler(signum, frame):
    raise Exception("Timeout reached")

signal(SIGALRM, timeout_handler)
alarm(1) # Set the alarm to fire in one second.
try:
    events = get_gamepad()
    alarm(0) # Disable the alarm
    print("Received input within one second")
except Exception as e:
    alarm(0) # Disable the alarm.
    print("No input for 1 second")
print("Complete")

Sample output:

# python3 bypass_if_no_input.py 
No input for 1 second
Complete
# python3 bypass_if_no_input.py 
Received input within one second
Complete

I followed @trevorablett advice and tried out his copy, it works flawlessly for my use case. For anyone reading this, I would suggest to use his fork since there has not been activity on this repository for over 5 years.