A different approach, as many USB “Air-mouse” devices have 1 to many events, the code will not work. As it is only looking at one event.
This code can be extended to multiple events.
#!/usr/bin/python3
import time
import asyncio
import requests
from evdev import InputDevice, categorize, ecodes # sudo apt-get install python3-evdev
URL='http://localhost:3000/api/v1/commands/?cmd='
# If mouse has more then one event, comment out what is not needed
event0 = InputDevice('/dev/input/event0')
#event1 = InputDevice('/dev/input/event1')
event2 = InputDevice('/dev/input/event2')
#event3 = InputDevice('/dev/input/event3')
STATUS = 0
# Keys see: https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
EV_VAL_PRESSED = 1
EV_VAL_RELEASED = 0
# event0:
KEY_ENTER = 28
KEY_LEFT = 105
KEY_RIGHT = 106
KEY_UP = 103
KEY_DOWN = 108
KEY_PAGEDOWN = 109
KEY_PAGEUP = 104
# event1:
# event2:
KEY_VOLUMEDOWN = 114
KEY_VOLUMEUP = 115
KEY_PLAYPAUSE = 164
# event2:
print(event0)
#print(event1)
print(event2)
#print(event3)
print('=== Start ===')
async def print_events(device):
global STATUS
async for event in device.async_read_loop():
if event.type == ecodes.EV_KEY:
if event.value == EV_VAL_PRESSED:
if event.value == EV_VAL_PRESSED:
if event.code == KEY_PLAYPAUSE:
print('<Play/Pause> Pressed')
url = URL + 'volume&volume=toggle'
elif event.code == KEY_LEFT:
print('<LEFT> Pressed')
url = URL + 'prev'
elif event.code == KEY_RIGHT:
print('<RIGHT> Pressed')
url = URL + 'next'
elif event.code == KEY_VOLUMEUP:
print('<RIGHT> Pressed')
url=URL + 'volume&volume=plus'
elif event.code == KEY_VOLUMEDOWN:
print('<RIGHT> Pressed')
url=URL + 'volume&volume=minus'
if len(url) > 0:
try:
requests.get(url=url, timeout=0.000000001)
except requests.exceptions.ReadTimeout:
pass
print(device.path, categorize(event), sep=': ')
print('---')
for device in event0, event2:
asyncio.ensure_future(print_events(device))
loop = asyncio.get_event_loop()
loop.run_forever()