How to modify EVDEV.py to record Keyboard Key RELEASE times?

Dr. Colombes drcolombes at yahoo.com
Fri Oct 12 20:34:16 EDT 2007


   I'm using a modified EVDEV.py program (see below) to record inter-keystroke times for Keystroke triples and doubles (t2 - t1, t3 -t1).   These times are key PRESS times.   

   How - where can EVDEV.py be modified (without too much trouble) to record Keystroke RELEASE times also ?

   Thanks for your help.

---------------------------------------------------------------------------------------

#!/usr/bin/env python
""" evdev.py

This is a Python interface to the Linux input system's event device.
Events can be read from an open event file and decoded into spiffy
python objects. The Event objects can optionally be fed into a Device
object that represents the complete state of the device being monitored.

Copyright (C) 2003-2004 Micah Dowty <micah at navi.cx>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""

import struct, sys, os, time
from socket import gethostname
from fcntl import ioctl

__all__ = ["Event", "Device"]


def demo():
    """Open the event device named on the command line, use incoming
       events to update a device, and show the state of this device.
       """
    dev = Device(sys.argv[1])
    try:
        while 1:
            dev.poll()
    except:
        dev.log.close()
    return dev.get_filename()



class BaseDevice:
    """Base class representing the state of an input device, with axes and buttons.
       Event instances can be fed into the Device to update its state.
       """
    def __init__(self):
        self.axes = {}
        self.buttons = {}

        self.name = None
        self.next2last = None
        self.next2lasttime = None
        self.next2lastpress = 'NONE'
        self.last = None
        self.lasttime = None
        self.lastpress = 'NONE'


        self.echo = {}
        #make log directory
        if not os.path.isdir("./logs"):
            os.mkdir("./logs")
        #new filename
        hostname = str(gethostname())
        filename = str(hostname) + "_log_" + str(time.asctime()).replace(":", "_").replace(" ", "_")
        #open log file
        self.log = open("./logs/" + filename, "w")
    self.filename = "./logs/" + filename
        #self.log.write("\n\n\n\nHOSTNAME:  " + str(hostname) + "\n" + ("_" * 60) + "\n")

    def get_filename(self):
    return self.filename

    def __repr__(self):
        return "<Device name=%r axes=%r buttons=%r>" % (
            self.name, self.axes, self.buttons)

    def update(self, event):
        f = getattr(self, "update_%s" % event.type, None)
        if f:
            f(event)

    def update_EV_KEY(self, event):
        if event.code not in self.echo:
            self.echo[event.code] = 0
        if self.echo[event.code] >= 1:
            self.echo[event.code] = 0
            return
        else:
            self.echo[event.code] += 1

        now = time.time()

        if self.lasttime == None:
            #self.lasttime = time.time()
            self.lasttime = now
    if self.next2lasttime == None:
        self.next2lasttime = now
        #now = time.time()
        newtime = now - self.lasttime
    new3time = now - self.next2lasttime
        #self.log.write(repr(self.lastpress) + "_" + repr(event.code) + "\t\t" + str(newtime) + "\n")
        self.log.write(self.lastpress + "_" + event.code + "\t\t" + str(newtime) + "\n")

        self.log.write(self.next2lastpress + "_" + self.lastpress + "_" + event.code + "\t\t" + str(new3time) + "\n")
    
    self.next2lastpress = self.lastpress
        self.lastpress = event.code

    self.next2lasttime = self.lasttime
        self.lasttime = now
    

    def update_EV_ABS(self, event):
        self.axes[event.code] = event.value

    def update_EV_REL(self, event):
        self.axes[event.code] = self.axes.get(event.code, 0) + event.value

    def __getitem__(self, name):
        """Retrieve the current value of an axis or button,
           or zero if no data has been received for it yet.
           """
        if name in self.axes:
            return self.axes[name]
        else:
            return self.buttons.get(name, 0)


# evdev ioctl constants. The horrible mess here
# is to silence silly FutureWarnings
EVIOCGNAME_512 = ~int(~0x82004506L & 0xFFFFFFFFL)
EVIOCGID       = ~int(~0x80084502L & 0xFFFFFFFFL)
EVIOCGBIT_512  = ~int(~0x81fe4520L & 0xFFFFFFFFL)
EVIOCGABS_512  = ~int(~0x80144540L & 0xFFFFFFFFL)


class Device(BaseDevice):
    """An abstract input device attached to a Linux evdev device node"""
    def __init__(self, filename):
        BaseDevice.__init__(self)
        self.fd = os.open(filename, os.O_RDONLY)
        self.packetSize = struct.calcsize(Event.format)
        self.readMetadata()

    def poll(self):
        """Receive and process all available input events"""
        while 1:
            try:
                buffer = os.read(self.fd, self.packetSize)
            except OSError:
                print "Could not open event device"
                return
            self.update(Event(unpack=buffer))

    def readMetadata(self):
        """Read device identity and capabilities via ioctl()"""
        buffer = "\0"*512

        # Read the name
        self.name = ioctl(self.fd, EVIOCGNAME_512, buffer)
        self.name = self.name[:self.name.find("\0")]

        # Read info on each absolute axis
        absmap = Event.codeMaps['EV_ABS']
        buffer = "\0" * struct.calcsize("iiiii")
        self.absAxisInfo = {}
        for name, number in absmap.nameMap.iteritems():
            values = struct.unpack("iiiii", ioctl(self.fd, EVIOCGABS_512 + number, buffer))
            values = dict(zip(( 'value', 'min', 'max', 'fuzz', 'flat' ),values))
            self.absAxisInfo[name] = values

    def update_EV_ABS(self, event):
        """Scale the absolute axis into the range [-1, 1] using absAxisInfo"""
        try:
            info = self.absAxisInfo[event.code]    

        except KeyError:
            return
        range = float(info['max'] - info['min'])
        self.axes[event.code] = (event.value - info['min']) / range * 2.0 - 1.0


class EnumDict:
    """A 1:1 mapping from numbers to strings or other objects, for enumerated
       types and other assigned numbers. The mapping can be queried in either
       direction. All values, by default, map to themselves.
       """
    def __init__(self, numberMap):
        self.numberMap = numberMap
        self.nameMap = {}
        for key, value in numberMap.iteritems():
            self.nameMap[value] = key

    def toNumber(self, name):
        return self.nameMap.get(name, name)

    def fromNumber(self, num):
        return self.numberMap.get(num, num)


class Event:
    """Represents one linux input system event. It can
       be encoded and decoded in the 'struct input_event'
       format used by the kernel. Types and codes are automatically
       encoded and decoded with the #define names used in input.h
       """
    format = "LLHHl"

    typeMap = EnumDict({
        0x00: "EV_RST",
        0x01: "EV_KEY",
        0x02: "EV_REL",
        0x03: "EV_ABS",
        0x04: "EV_MSC",
        0x11: "EV_LED",
        0x12: "EV_SND",
        0x14: "EV_REP",
        0x15: "EV_FF",
        })

    codeMaps = {

        "EV_KEY": EnumDict({
        0: "RESERVED",
        1: "ESC",
        2: "1",
        3: "2",
        4: "3",
        5: "4",
        6: "5",
        7: "6",
        8: "7",
        9: "8",
        10: "9",
        11: "0",
        12: "-",
        13: "=",
        14: "BACKSPACE", # "\\b",
        15: "TAB", # "\\t",
        16: "q",
        17: "w",
        18: "e",
        19: "r",
        20: "t",
        21: "y",
        22: "u",
        23: "i",
        24: "o",
        25: "p",
        26: "[",
        27: "]",
        28: "NEWLINE", # "\\n",
        29: "LEFTCTRL",
        30: "a",
        31: "s",
        32: "d",
        33: "f",
        34: "g",
        35: "h",
        36: "j",
        37: "k",
        38: "l",
        39: "SEMICOLON", # ";",
        40: "'",
        41: "`",
        42: "LEFTSHIFT",
        43: "\\",
        44: "z",
        45: "x",
        46: "c",
        47: "v",
        48: "b",
        49: "n",
        50: "m",
        51: "COMMA", # ",",
        52: "PERIOD", # ".",
        53: "/",
        54: "RIGHTSHIFT",
        55: "*",
        56: "LEFTALT",
        57: "BLANK", # " ",
        58: "CAPSLOCK",
        59: "F1",
        60: "F2",
        61: "F3",
        62: "F4",
        63: "F5",
        64: "F6",
        65: "F7",
        66: "F8",
        67: "F9",
        68: "F10",
        69: "NUMLOCK",
        70: "SCROLLLOCK",
        71: "NK7", # "7",
        72: "NK8", # "8",
        73: "NK9", # "9",
        74: "NKMINUS", # "-",
        75: "NK4", # "4",
        76: "NK5", # "5",
        77: "NK6", # "6",
        78: "NK+", # "+",
        79: "NK1", # "1",
        80: "NK2", # "2",
        81: "NK3", # "3",
        82: "NK0", # "0",
        83: "NKPERIOD", # ".", 
        84: "103RD",
        85: "F13",
        86: "102ND",
        87: "F11",
        88: "F12",
        89: "F14",
        90: "F15",
        91: "F16",
        92: "F17",
        93: "F18",
        94: "F19",
        95: "F20",
        96: "NEWLINE", # "\\n",
        97: "RIGHTCTRL",
        98: "/",
        99: "SYSRQ",
        100: "RIGHTALT",
        101: "LINEFEED",
        102: "HOME",
        103: "UP",
        104: "PAGEUP",
        105: "LEFT",
        106: "RIGHT",
        107: "END",
        108: "DOWN",
        109: "PAGEDOWN",
        110: "INSERT",
        111: "DELETE",
        112: "MACRO",
        113: "MUTE",
        114: "VOLUMEDOWN",
        115: "VOLUMEUP",
        116: "POWER",
        117: "=",
        118: "KPPLUSMINUS",
        119: "PAUSE",
        120: "F21",
        121: "F22",
        122: "F23",
        123: "F24",
        124: "COMMA", # ",",
        125: "LEFTMETA",
        126: "RIGHTMETA",
        127: "COMPOSE",
        128: "STOP",
        129: "AGAIN",
        130: "PROPS",
        131: "UNDO",
        132: "FRONT",
        133: "COPY",
        134: "OPEN",
        135: "PASTE",
        136: "FIND",
        137: "CUT",
        138: "HELP",
        139: "MENU",
        140: "CALC",
        141: "SETUP",
        142: "SLEEP",
        143: "WAKEUP",
        144: "FILE",
        145: "SENDFILE",
        146: "DELETEFILE",
        147: "XFER",
        148: "PROG1",
        149: "PROG2",
        150: "WWW",
        151: "MSDOS",
        152: "COFFEE",
        153: "DIRECTION",
        154: "CYCLEWINDOWS",
        155: "MAIL",
        156: "BOOKMARKS",
        157: "COMPUTER",
        158: "BACK",
        159: "FORWARD",
        160: "CLOSECD",
        161: "EJECTCD",
        162: "EJECTCLOSECD",
        163: "NEXTSONG",
        164: "PLAYPAUSE",
        165: "PREVIOUSSONG",
        166: "STOPCD",
        167: "RECORD",
        168: "REWIND",
        169: "PHONE",
        170: "ISO",
        171: "CONFIG",
        172: "HOMEPAGE",
        173: "REFRESH",
        174: "EXIT",
        175: "MOVE",
        176: "EDIT",
        177: "SCROLLUP",
        178: "SCROLLDOWN",
        179: "(",
        180: ")",
        181: "INTL1",
        182: "INTL2",
        183: "INTL3",
        184: "INTL4",
        185: "INTL5",
        186: "INTL6",
        187: "INTL7",
        188: "INTL8",
        189: "INTL9",
        190: "LANG1",
        191: "LANG2",
        192: "LANG3",
        193: "LANG4",
        194: "LANG5",
        195: "LANG6",
        196: "LANG7",
        197: "LANG8",
        198: "LANG9",
        200: "PLAYCD",
        201: "PAUSECD",
        202: "PROG3",
        203: "PROG4",
        205: "SUSPEND",
        206: "CLOSE",
        220: "UNKNOWN",
        224: "BRIGHTNESSDOWN",
        225: "BRIGHTNESSUP",
        0x100: "BTN_0",
        0x101: "BTN_1",
        0x102: "BTN_2",
        0x103: "BTN_3",
        0x104: "BTN_4",
        0x105: "BTN_5",
        0x106: "BTN_6",
        0x107: "BTN_7",
        0x108: "BTN_8",
        0x109: "BTN_9",
        0x110: "BTN_LEFT",
        0x111: "BTN_RIGHT",
        0x112: "BTN_MIDDLE",
        0x113: "BTN_SIDE",
        0x114: "BTN_EXTRA",
        0x115: "BTN_FORWARD",
        0x116: "BTN_BACK",
        0x120: "BTN_TRIGGER",
        0x121: "BTN_THUMB",
        0x122: "BTN_THUMB2",
        0x123: "BTN_TOP",
        0x124: "BTN_TOP2",
        0x125: "BTN_PINKIE",
        0x126: "BTN_BASE",
        0x127: "BTN_BASE2",
        0x128: "BTN_BASE3",
        0x129: "BTN_BASE4",
        0x12a: "BTN_BASE5",
        0x12b: "BTN_BASE6",
        0x12f: "BTN_DEAD",
        0x130: "BTN_A",
        0x131: "BTN_B",
        0x132: "BTN_C",
        0x133: "BTN_X",
        0x134: "BTN_Y",
        0x135: "BTN_Z",
        0x136: "BTN_TL",
        0x137: "BTN_TR",
        0x138: "BTN_TL2",
        0x139: "BTN_TR2",
        0x13a: "BTN_SELECT",
        0x13b: "BTN_START",
        0x13c: "BTN_MODE",
        0x13d: "BTN_THUMBL",
        0x13e: "BTN_THUMBR",
        0x140: "BTN_TOOL_PEN",
        0x141: "BTN_TOOL_RUBBER",
        0x142: "BTN_TOOL_BRUSH",
        0x143: "BTN_TOOL_PENCIL",
        0x144: "BTN_TOOL_AIRBRUSH",
        0x145: "BTN_TOOL_FINGER",
        0x146: "BTN_TOOL_MOUSE",
        0x147: "BTN_TOOL_LENS",
        0x14a: "BTN_TOUCH",
        0x14b: "BTN_STYLUS",
        0x14c: "BTN_STYLUS2",
        }),

        "EV_REL": EnumDict({
        0x00: "REL_X",
        0x01: "REL_Y",
        0x02: "REL_Z",
        0x06: "REL_HWHEEL",
        0x07: "REL_DIAL",
        0x08: "REL_WHEEL",
        0x09: "REL_MISC",
        }),

        "EV_ABS": EnumDict({
        0x00: "ABS_X",
        0x01: "ABS_Y",
        0x02: "ABS_Z",
        0x03: "ABS_RX",
        0x04: "ABS_RY",
        0x05: "ABS_RZ",
        0x06: "ABS_THROTTLE",
        0x07: "ABS_RUDDER",
        0x08: "ABS_WHEEL",
        0x09: "ABS_GAS",
        0x0a: "ABS_BRAKE",
        0x10: "ABS_HAT0X",
        0x11: "ABS_HAT0Y",
        0x12: "ABS_HAT1X",
        0x13: "ABS_HAT1Y",
        0x14: "ABS_HAT2X",
        0x15: "ABS_HAT2Y",
        0x16: "ABS_HAT3X",
        0x17: "ABS_HAT3Y",
        0x18: "ABS_PRESSURE",
        0x19: "ABS_DISTANCE",
        0x1a: "ABS_TILT_X",
        0x1b: "ABS_TILT_Y",
        0x1c: "ABS_MISC",
        }),

        "EV_MSC": EnumDict({
        0x00: "MSC_SERIAL",
        0x01: "MSC_PULSELED",
        }),

    "EV_LED": EnumDict({
        0x00: "LED_NUML",
        0x01: "LED_CAPSL",
        0x02: "LED_SCROLLL",
        0x03: "LED_COMPOSE",
        0x04: "LED_KANA",
        0x05: "LED_SLEEP",
        0x06: "LED_SUSPEND",
        0x07: "LED_MUTE",
        0x08: "LED_MISC",
        }),

        "EV_REP": EnumDict({
        0x00: "REP_DELAY",
        0x01: "REP_PERIOD",
        }),

        "EV_SND": EnumDict({
        0x00: "SND_CLICK",
        0x01: "SND_BELL",
        }),
        }


    def __init__(self, timestamp=0, type=0, code=0, value=0, unpack=None, readFrom=None):
        self.timestamp = timestamp
        self.type = type
        self.code = code
        self.value = value
        if unpack is not None:
            self.unpack(unpack)
        if readFrom is not None:
            self.readFrom(readFrom)

    def __repr__(self):
        return "<Event timestamp=%r type=%r code=%r value=%r>" % (
            self.timestamp, self.type, self.code, self.value)

    def pack(self):
        """Pack this event into an input_event struct in
           the local machine's byte order.
           """
        secs = int(self.timestamp)
        usecs = int((self.timestamp - secs) * 1000000)
        packedType = self.typeMap.toNumber(self.type)
        if self.type in self.codeMaps:
            packedCode = self.codeMaps[self.type].toNumber(self.code)
        else:
            packedCode = self.code
        return struct.pack(self.format, secs, usecs, packedType,
                           packedCode, self.value)

    def unpack(self, s):
        """Unpack ourselves from the given string,, an
           input_event struct in the local byte order.
           """
        secs, usecs, packedType, packedCode, self.value = struct.unpack(self.format, s)
        self.timestamp = secs + (usecs / 1000000.0)
        self.type = self.typeMap.fromNumber(packedType)
        if self.type in self.codeMaps:
            self.code = self.codeMaps[self.type].fromNumber(packedCode)
        else:
            self.code = packedCode

    def readFrom(self, stream):
        """Read the next event from the given file-like object"""
        self.unpack(stream.read(struct.calcsize(self.format)))


if __name__ == "__main__":
    filename = demo()
    print "\nReturned file name = %s " % filename 
    text = open(filename).read()
    L = text.splitlines()
    L.sort()
    os.remove(filename)
    filename_sorted = filename + "_s"
    output = open (filename_sorted, "www")
    #filename_unsorted = filename + "_u"
    #output = open (filename_unsorted, "www")
    for line in L:
    line = line + "\n"
    output.write(line)
    #os.chmod(output, 0777)
    output.close()
    print " Created sorted file %s " % filename_sorted
    #print " Created UNsorted file %s " % filename_unsorted

### The End ###
 
       
---------------------------------
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071012/a7dcd4eb/attachment.html>


More information about the Python-list mailing list