Search code examples
python-3.xconsolekivyhidevdev

Python: get input from HID device outside the active window?


DESCRIPTION

I use the Kivy framework and run a script in Python 3 that reads the input from a HID device. The device is a bar code scanner and simulates a keyboard.

PROBLEM

The script opens a popup and the bar reading procedure in background, both at the same time. Every attempt to read the bar code I have to be in the active window. In my case this is the prompt (console). This means I havo to click on the console window and only then the reader works. This happens regardless of how I call the reading procedure from the main script (method, thread, subprocess, Clock.schedule_once). How can I get the input outside the prompt window e.g. in the main kivy gui outside the console?

These other posts did help but did not provide an answer:

  1. How can I get a String from HID device in Python with evdev?
  2. python :Read from a USB HID device
  3. How can I get a String from HID device in Python with evdev?
  4. Read HID input while window is "out of focus" in Python

CODE

Using str = input("") as subprocess:

#!/usr/bin/python3.5 python3.5
# -*- coding: utf-8 -*-

# This script reads the output from HID device e.g. bar/rfid scanner

import sys
import inspect
import csv

# return the current line number
def lineno():
    return inspect.currentframe().f_back.f_lineno
    
str = input("Enter your input: ")

Using evdev.ecodes.EV_KEY as subprocess:

import evdev
from evdev import InputDevice, categorize, ecodes
device = evdev.InputDevice('/dev/input/event20')
while True:
    try:
        for event in device.read_loop():
            if event.type == evdev.ecodes.EV_KEY:
                print(evdev.categorize(event))
    except:
        print("Keyboard Interrupt")
        break

Solution

  • I find a solution following this post here https://khanhicetea.com/post/read_input_from_usb_keyboard_in_linux/. I call the input detection file as a subprocess. Since I did not found a quick substitute for the enter key in the CODE_MAP_CHAR{}, I added the scancodes{} with a different type of code for the enter key. To find out the names and other attributes of different devices use cat /proc/bus/input/devices. Below is the working code.

    #!/usr/bin/python3.5 python3.5
    # -*- coding: utf-8 -*-
    # This script reads the output from HID device e.g. ECCO bar/rfid scanner
    # Make sure evdev is installed or install it with: pip3 install evdev.
    # The Kernel on the Pi creates an input device which resides in /dev/input/.
    # You can find out the names and other attributes of different devices using:
    # cat /proc/bus/input/devices
    
    import sys
    import inspect
    import serial
    import time
    import random
    import csv
    
    import evdev
    from evdev import InputDevice, categorize, ecodes, list_devices
    
    device = evdev.InputDevice('/dev/input/event20')
    print(device)
    
    # reserve the device
    device.grab()
    
    #setup vars
    eccoOut=[]
    x = ''
    caps = False
    
    
    CODE_MAP_CHAR = {
        'KEY_MINUS': "-",
        'KEY_SPACE': " ",    
        'KEY_U': "U",
        'KEY_W': "W",
        'KEY_BACKSLASH': "\\",
        'KEY_GRAVE': "`",
        'KEY_NUMERIC_STAR': "*",
        'KEY_NUMERIC_3': "3",
        'KEY_NUMERIC_2': "2",
        'KEY_NUMERIC_5': "5",
        'KEY_NUMERIC_4': "4",
        'KEY_NUMERIC_7': "7",
        'KEY_NUMERIC_6': "6",
        'KEY_NUMERIC_9': "9",
        'KEY_NUMERIC_8': "8",
        'KEY_NUMERIC_1': "1",
        'KEY_NUMERIC_0': "0",
        'KEY_E': "E",
        'KEY_D': "D",
        'KEY_G': "G",
        'KEY_F': "F",
        'KEY_A': "A",
        'KEY_C': "C",
        'KEY_B': "B",
        'KEY_M': "M",
        'KEY_L': "L",
        'KEY_O': "O",
        'KEY_N': "N",
        'KEY_I': "I",
        'KEY_H': "H",
        'KEY_K': "K",
        'KEY_J': "J",
        'KEY_Q': "Q",
        'KEY_P': "P",
        'KEY_S': "S",
        'KEY_X': "X",
        'KEY_Z': "Z",
        'KEY_q': "q",
        'KEY_w': "w",
        'KEY_e': "e",
        'KEY_r': "r",
        'KEY_t': "t",
        'KEY_z': "z",
        'KEY_u': "u",
        'KEY_i': "i",
        'KEY_o': "o",
        'KEY_p': "p",
        'KEY_a': "a",
        'KEY_s': "s",
        'KEY_d': "d",
        'KEY_f': "f",
        'KEY_g': "g",
        'KEY_h': "h",
        'KEY_j': "k",
        'KEY_l': "l",
        'KEY_y': "y",
        'KEY_x': "x",
        'KEY_c': "c",
        'KEY_v': "v",
        'KEY_b': "b",
        'KEY_n': "n",
        'KEY_m': "m",
        'KEY_KP4': "4",
        'KEY_KP5': "5",
        'KEY_KP6': "6",
        'KEY_KP7': "7",
        'KEY_KP0': "0",
        'KEY_KP1': "1",
        'KEY_KP2': "2",
        'KEY_KP3': "3",
        'KEY_KP8': "8",
        'KEY_KP9': "9",
        'KEY_5': "5",
        'KEY_4': "4",
        'KEY_7': "7",
        'KEY_6': "6",
        'KEY_1': "1",
        'KEY_0': "0",
        'KEY_3': "3",
        'KEY_2': "2",
        'KEY_9': "9",
        'KEY_8': "8",
        'KEY_LEFTBRACE': "[",
        'KEY_RIGHTBRACE': "]",    
        'KEY_COMMA': ",",
        'KEY_EQUAL': "=",    
        'KEY_SEMICOLON': ";",
        'KEY_APOSTROPHE': "'",
        'KEY_T': "T",
        'KEY_V': "V",
        'KEY_R': "R",
        'KEY_Y': "Y",
        'KEY_TAB': "\t",
        'KEY_DOT': ".",
        'KEY_SLASH': "/",
    }
    
    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',
        10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
        20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
        30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
        40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
        50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
    }
    
    def parse_key_to_char(val):
        return CODE_MAP_CHAR[val] if val in CODE_MAP_CHAR else ""
    
    if __name__ == "__main__":
        print("list of devices:")
        devices = [InputDevice(fn) for fn in list_devices()]
        for device in devices:
            print("\t{}\t{}".format(device.fn, device.name))
            
        event_id = 20   # check your events to set the correct value
        exclusive_access = 1
    
        device = InputDevice('/dev/input/event{}'.format(event_id))
        if int(exclusive_access) == 1:
            device.grab()
    
        eccoBarcodeList = []
        for event in device.read_loop():
            if event.type == evdev.ecodes.EV_KEY:
                e = categorize(event)
                if e.keystate == e.key_up:
                    sys.stdout.write(parse_key_to_char(e.keycode))
                    cifra = parse_key_to_char(e.keycode)
                    eccoBarcodeList.append(cifra)
                    print(eccoBarcodeList)
                    sys.stdout.flush()
                if e.scancode == 28:
                    print("return key detected, exiting...")
                    break