Python Forum
Read a single event (keypress) from asyncio
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read a single event (keypress) from asyncio
#1
I'm using asyncio library to read input from a Infra Red remote control.
This is python 3.7 branch on an Orange Pi Zero, using the built in IR receiver as
the event loop.

The problem is asyncio is so fast that a single keypress will often read multiple times.
Is there any way to read a single event, or perhaps wait 50 milliseconds ?

Below is the python Code

import asyncio
from evdev import InputDevice
irr = InputDevice('/dev/input/event0')

#  Receive keypress from IRR and store as 'code'
async def rec_code():
    await asyncio.sleep(0.1)
    async  for event in irr.async_read_loop():
        if event.type == 4:
            global code
            await asyncio.sleep(0.2)	# small delay to prevent two keypresses 
            code = event.value
            await asyncio.sleep(0.2)	# small delay to prevent two keypresses 
            print("IRC Code ",code)

async def delay5():
    while True:
        await asyncio.sleep(5)
        print("5 sec delay")

#  Create asyncio loop run two independent timer loops and  IR receiver function
loop= asyncio.get_event_loop()
loop.create_task(delay5())
loop.create_task(rec_code())
loop.run_forever()
Below is the output:
IRC Code 5
IRC Code 5
IRC Code 2
IRC Code 4

So sometimes a single keypress i.e. 2 or 4 is displayed, but mostly it will report multiple
reads as button 5 has been reported twice.
Wanted Output:
IRC Code 5
IRC Code 2
IRC Code 4

I would like some way to catch just one keypress.
I've tried to slow down event loop reading with multiple
await asyncio.sleep(0.1)

lines, but Im still reading same key twice.

Thanks in advance for any help
Reply


Messages In This Thread
Read a single event (keypress) from asyncio - by cygnus_X1 - May-26-2021, 10:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [GPX] Read all trkpt, and merge into single track? Winfried 0 1,580 Jan-30-2020, 04:08 PM
Last Post: Winfried
  Asyncio StreamReader read method doesn't respect timeout when using SSL dukadahake 0 3,529 Jul-24-2019, 11:55 AM
Last Post: dukadahake
  Keypress when running a python app in a console on windows Stephen 6 9,130 Apr-16-2019, 12:38 PM
Last Post: Stephen
  Using asyncio to read text file and load GUI QueenSvetlana 1 4,844 Nov-09-2017, 02:55 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020