Skip to content

Commit

Permalink
working dual keyboard logic
Browse files Browse the repository at this point in the history
  • Loading branch information
marleyjaffe committed Dec 30, 2023
1 parent c4ae7d3 commit 45a734f
Showing 1 changed file with 61 additions and 22 deletions.
83 changes: 61 additions & 22 deletions keyboard_input.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio, evdev
kbd_logi = evdev.InputDevice('/dev/input/by-id/usb-Logitech_USB_Receiver-if02-event-kbd')
kbd_wired = evdev.InputDevice('/dev/input/by-id/usb-SIGMACH1P_USB_Keykoard-event-kbd')
from evdev import InputDevice, categorize, ecodes
kbd_outside = evdev.InputDevice('/dev/input/by-id/usb-Logitech_USB_Receiver-if02-event-kbd')
kbd_inside = evdev.InputDevice('/dev/input/by-id/usb-SIGMACH1P_USB_Keykoard-event-kbd')

kbd_outside.grab()
kbd_inside.grab()

kbd_logi.grab()
kbd_wired.grab()

keypad_string = ''

inside_keypad_string = ''
outside_keypad_string = ''
all_scancodes = {
# Scancode: ASCIICode
0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
Expand All @@ -18,12 +18,18 @@
50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}

scancodes = {
inside_scancodes = {
# Scancode: ASCIICode
2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8', 10: u'9', 11: u'0',
28: u'ENTR', 105: u'OPEN', 106: u'CLOSE'
}

outside_scancodes = {
# Scancode: ASCIICode
2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8', 10: u'9', 11: u'0',
28: u'ENTR', 105: u'call1', 106: u'call2'
}

async def print_events(device):
async for event in device.async_read_loop():
if event.type == evdev.ecodes.EV_KEY:
Expand All @@ -32,32 +38,65 @@ async def print_events(device):
print(device.path, data, sep=': ')
print(data.scancode)

async def create_string(device):
global keypad_string
async def inside_keypad(device):
global inside_keypad_string
async for event in device.async_read_loop():
global keypad_string
global inside_keypad_string
if event.type == evdev.ecodes.EV_KEY:
data = evdev.categorize(event)
if data.keystate == 0: # up events only
keypressed = scancodes.get(data.scancode)
keypressed = inside_scancodes.get(data.scancode)
if keypressed == 'ENTR':
print(keypad_string)
keypad_string = ''
print(inside_keypad_string)
inside_keypad_string = ''
elif keypressed == 'OPEN':
print(keypressed)
keypad_string = ''
inside_keypad_string = ''
elif keypressed == 'CLOSE':
print(keypressed)
keypad_string = ''
inside_keypad_string = ''
else:
# print(keypressed)
keypad_string += keypressed
try:
inside_keypad_string += keypressed
except TypeError:
print("scancode not added")
except:
print("other error")

async def outside_keypad(device):
global outside_keypad_string
async for event in device.async_read_loop():
global outside_keypad_string
if event.type == evdev.ecodes.EV_KEY:
data = evdev.categorize(event)
if data.keystate == 0: # up events only
keypressed = outside_scancodes.get(data.scancode)
if keypressed == 'ENTR':
print(outside_keypad_string)
outside_keypad_string = ''
elif keypressed == 'call1':
print(keypressed)
outside_keypad_string = ''
elif keypressed == 'call2':
print(keypressed)
outside_keypad_string = ''
else:
# print(keypressed)
try:
outside_keypad_string += keypressed
except TypeError:
print("scancode not added")
except:
print("other error")

for device in kbd_logi, kbd_wired:
# asyncio.ensure_future(print_events(device))
# keypad_string = ''
asyncio.ensure_future(create_string(device))
asyncio.ensure_future(inside_keypad(kbd_inside))
asyncio.ensure_future(outside_keypad(kbd_outside))

loop = asyncio.get_event_loop()
loop.run_forever()
try:
loop.run_forever()
except KeyboardInterrupt:
print("exiting nicely via keyboard interrupt")
except:
print("other exit")

0 comments on commit 45a734f

Please sign in to comment.