Format of /dev/input/event*

前端 未结 5 2266
孤街浪徒
孤街浪徒 2020-11-28 04:54

What is the \"format\" of the character devices located in /dev/input/event*?

In other words, how can I decode the character stream? A Python example w

5条回答
  •  自闭症患者
    2020-11-28 05:23

    A simple and raw reader can be just done using:

    #!/usr/bin/python
    import struct
    import time
    import sys
    
    infile_path = "/dev/input/event" + (sys.argv[1] if len(sys.argv) > 1 else "0")
    
    """
    FORMAT represents the format used by linux kernel input event struct
    See https://github.com/torvalds/linux/blob/v5.5-rc5/include/uapi/linux/input.h#L28
    Stands for: long int, long int, unsigned short, unsigned short, unsigned int
    """
    FORMAT = 'llHHI'
    EVENT_SIZE = struct.calcsize(FORMAT)
    
    #open file in binary mode
    in_file = open(infile_path, "rb")
    
    event = in_file.read(EVENT_SIZE)
    
    while event:
        (tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)
    
        if type != 0 or code != 0 or value != 0:
            print("Event type %u, code %u, value %u at %d.%d" % \
                (type, code, value, tv_sec, tv_usec))
        else:
            # Events with code, type and value == 0 are "separator" events
            print("===========================================")
    
        event = in_file.read(EVENT_SIZE)
    
    in_file.close()
    

提交回复
热议问题