def receive(self, event):

        if event.etype == 2:    # etype 2: mouse 
            if event.ecode == 8:   # ecode 8: mouse wheel
                dir = event.evalue
                ts = event.timestamp
                tdiff = ts - self.oldtime
                self.oldtime = ts
                if (self.olddir == dir):
                    
                    

                    if (tdiff <= self.threshold):
                            
                        for i in range(self.speedup):
                            self.udev.send_event(InputEvent.new(2, 8, dir))
                            self.udev.send_event(InputEvent.new(0, 0, 0))
                        #print tdiff
                        
                        
                    if (self.speedup == 0): # debug
                        debugstr = str(tdiff * 1000)
                        if (tdiff <= self.threshold):
                            debugstr += " speedup"
                        print debugstr
                        
                self.olddir = dir
Ejemplo n.º 2
0
 def to_input_events(self):
     """
     Return a sequence of InputEvents for this action.
     If it's a keydown event and there are modifiers, press them before
     the event, and release them afterwards.  If it's a keyup, just
     return the single event.
     """
     res = []
     if self.keydown:
         if self.modstate & MOD_CTRL:
             res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL,  1) ]
         if self.modstate & MOD_ALT:
             res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTALT,   1) ]
         if self.modstate & MOD_SHIFT:
             res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 1) ]
         res += [ self.event ]
         if self.modstate & MOD_SHIFT:
             res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 0) ]
         if self.modstate & MOD_ALT:
             res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTALT,   0) ]
         if self.modstate & MOD_CTRL:
             res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL,  0) ]
     else:
         res += [ self.event ]
     res += [ InputEvent.new(0, 0, 0) ]
     return res
Ejemplo n.º 3
0
 def to_input_events(self):
     """
     Return a sequence of InputEvents for this action.
     If it's a keydown event and there are modifiers, press them before
     the event, and release them afterwards.  If it's a keyup, just
     return the single event.
     """
     res = []
     if self.keydown:
         if self.modstate & MOD_CTRL:
             res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL, 1)]
         if self.modstate & MOD_ALT:
             res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTALT, 1)]
         if self.modstate & MOD_SHIFT:
             res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 1)]
         res += [self.event]
         if self.modstate & MOD_SHIFT:
             res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 0)]
         if self.modstate & MOD_ALT:
             res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTALT, 0)]
         if self.modstate & MOD_CTRL:
             res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL, 0)]
     else:
         res += [self.event]
     res += [InputEvent.new(0, 0, 0)]
     return res
Ejemplo n.º 4
0
def test():
    logging.getLogger().setLevel(logging.INFO)
    ts = "KEY_LEFTCTRL-down KEY_P KEY_LEFTCTRL-up"
    logging.info("%s %r" % (ts, make_keyevents(ts)))
    from StringIO import StringIO
    s = StringIO("""
Ctrl-KEY_P-down = wait
Ctrl-KEY_P-down KEY_P-up = echo send Shift-KEY_P; send Shift-KEY_P; clear
Ctrl-Alt-KEY_P-down = wait
Ctrl-Alt-KEY_P-down KEY_P-up = forward; clear
BTN_RIGHT-down = set rightclick time.time()
BTN_RIGHT-up = if (time.time()-rightclick) > 0.3; send BTN_RIGHT
""")
    parser = KeymapParser(s)
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 1), MOD_CTRL))
    assert len(res) == 0
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 0), 0))
    logging.info(res)
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.BTN_RIGHT, 1), 0))
    assert len(res) == 0
    time.sleep(0.35)
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.BTN_RIGHT, 0), 0))
    logging.info(res)
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 1), MOD_CTRL | MOD_ALT))
    assert len(res) == 0
    logging.info("Should be control down, alt down, P down, alt up, control up, sync, P up")
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 0), 0))
    logging.info(res)
Ejemplo n.º 5
0
def test():
    logging.getLogger().setLevel(logging.INFO)
    ts = "KEY_LEFTCTRL-down KEY_P KEY_LEFTCTRL-up"
    logging.info("%s %r" % (ts, make_keyevents(ts)))
    from StringIO import StringIO
    s = StringIO("""
Ctrl-KEY_P-down = wait
Ctrl-KEY_P-down KEY_P-up = echo send Shift-KEY_P; send Shift-KEY_P; clear
Ctrl-Alt-KEY_P-down = wait
Ctrl-Alt-KEY_P-down KEY_P-up = forward; clear
BTN_RIGHT-down = set rightclick time.time()
BTN_RIGHT-up = if (time.time()-rightclick) > 0.3; send BTN_RIGHT
""")
    parser = KeymapParser(s)
    res = parser.process(
        KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 1), MOD_CTRL))
    assert len(res) == 0
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 0), 0))
    logging.info(res)
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.BTN_RIGHT, 1), 0))
    assert len(res) == 0
    time.sleep(0.35)
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.BTN_RIGHT, 0), 0))
    logging.info(res)
    res = parser.process(
        KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 1), MOD_CTRL | MOD_ALT))
    assert len(res) == 0
    logging.info(
        "Should be control down, alt down, P down, alt up, control up, sync, P up"
    )
    res = parser.process(KeyEvent(InputEvent.new(S.EV_KEY, S.KEY_P, 0), 0))
    logging.info(res)
def main(indev, p_thres, p_mul):
    udev = UInputDevice("Virtual Input Device", 0x0, 0x1, 1)
    udev.create()
    udev.send_event(InputEvent.new(0,0,0))
    poll = select.poll()
    fds = {}
    dev = ForwardDevice(udev, float(p_thres)/1000, p_mul, indev, indev)
    poll.register(dev, select.POLLIN | select.POLLPRI)

    fds[dev.fileno()] = dev
    while True:
        for x,e in poll.poll():
            dev = fds[x]
            dev.read()
Ejemplo n.º 7
0
def make_keyevents(keystring):
    res = []
    ks = keystring.split(" ")
    untap = []
    for k in ks:
        l = None
        while True:
            if k.startswith("Alt-"):
                k = k[4:]
                res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTALT, 1) ]
                untap += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTALT, 0) ]
            elif k.startswith("Ctrl-"):
                k = k[5:]
                res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL, 1) ]
                untap += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL, 0) ]
            elif k.startswith("Shift-"):
                k = k[6:]
                res += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 1) ]
                untap += [ InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 0) ]
            else:
                break
        if "-" in k:
            k, l = k.split("-", 1)
        if hasattr(scancodes, k):
            sc = getattr(scancodes, k)
            if l is None or l == "down":
                res += [ InputEvent.new(S.EV_KEY, sc, 1) ]
            if l is None or l == "up":
                res += [ InputEvent.new(S.EV_KEY, sc, 0) ]
        else:
            logging.warning("Unknown key %s" % k)
        if untap:
            untap.reverse()
            res += untap
        if res:
            res.append(InputEvent.new(0, 0, 0))
    return res
Ejemplo n.º 8
0
def make_keyevents(keystring):
    res = []
    ks = keystring.split(" ")
    untap = []
    for k in ks:
        l = None
        while True:
            if k.startswith("Alt-"):
                k = k[4:]
                res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTALT, 1)]
                untap += [InputEvent.new(S.EV_KEY, S.KEY_LEFTALT, 0)]
            elif k.startswith("Ctrl-"):
                k = k[5:]
                res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL, 1)]
                untap += [InputEvent.new(S.EV_KEY, S.KEY_LEFTCTRL, 0)]
            elif k.startswith("Shift-"):
                k = k[6:]
                res += [InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 1)]
                untap += [InputEvent.new(S.EV_KEY, S.KEY_LEFTSHIFT, 0)]
            else:
                break
        if "-" in k:
            k, l = k.split("-", 1)
        if hasattr(scancodes, k):
            sc = getattr(scancodes, k)
            if l is None or l == "down":
                res += [InputEvent.new(S.EV_KEY, sc, 1)]
            if l is None or l == "up":
                res += [InputEvent.new(S.EV_KEY, sc, 0)]
        else:
            logging.warning("Unknown key %s" % k)
        if untap:
            untap.reverse()
            res += untap
        if res:
            res.append(InputEvent.new(0, 0, 0))
    return res
Ejemplo n.º 9
0
				fcntl.ioctl(fd, UI_SET_KEYBIT, v)

	def send_event(self, event):
		if type(event) == str:
			os.write(self._fd, event)
		elif hasattr(event, 'pack'):
			os.write(self._fd, event.pack())
		else:
			print("Don't know what to do to send %r" % (event,), file=sys.stderr)

if __name__ == '__main__':
	udev = UInputDevice("Test", 0x0, 0x1, 1)
	udev.create()
	from pyinputevent import InputEvent
	import sys
	udev.send_event(InputEvent.new(0,0,0))
	i = 100
	while i:
		i = i - 1
		sys.stdout.flush()
		#udev.send_event(InputEvent.new(4,4,20))
		#udev.send_event(InputEvent.new(1,20,1))
		#udev.send_event(InputEvent.new(0,0,0))
		#udev.send_event(InputEvent.new(4,4,20))
		#udev.send_event(InputEvent.new(1,20,0))
		#udev.send_event(InputEvent.new(0,0,0))
		udev.send_event(InputEvent.new(EV_REL, 0, 4))
		udev.send_event(InputEvent.new(EV_REL, 1, 4))
		udev.send_event(InputEvent.new(0,0,0))
		time.sleep(0.005)
	udev.destroy()
Ejemplo n.º 10
0
from pyinputevent import InputEvent, SimpleDevice
from keytrans import *
import scancodes as S
import logging

INP_SYNC = InputEvent.new(0, 0, 0)

class ForwardDevice(SimpleDevice):
    def __init__(self, udev, keymap, *args, **kwargs):
        SimpleDevice.__init__(self, *args, **kwargs)
        self.udev = udev # output device
        self.ctrl = False
        self.alt = False
        self.shift = False
        self.state = None
        self.doq = False # queue keystrokes for processing?
        self.mouseev = []
        self.keyev = []
        self.parser = KeymapParser(keymap)

    def send_all(self, events):
        for event in events:
            logging.debug(" --> %r" % event)
            self.udev.send_event(event)

    @property
    def modcode(self):
        code = 0
        if self.shift:
            code += 1
Ejemplo n.º 11
0
                        ismouse = True
                    if 'event' in h:
                        ev = h
                if ismouse:
                    mouse = ev
                else:
                    kbd = ev
        elif line:
            l, r = line.split(":", 1)
            r = r.strip()
            entry[l] = r
    return mouse, kbd


#
INP_SYNC = InputEvent.new(0, 0, 0)

class ForwardDevice(SimpleDevice):
    def __init__(self, udev, *args, **kwargs):
        SimpleDevice.__init__(self, *args, **kwargs)
        self.udev = udev # output device
        self.ctrl = False
        self.alt = False
        self.shift = False
        self.state = None
        self.doq = False # queue keystrokes for processing?
        self.mouseev = []
        self.keyev = []
        self.parser = KeymapParser("keymap.txt")

    def send_all(self, events):
Ejemplo n.º 12
0
                fcntl.ioctl(fd, UI_SET_KEYBIT, v)

    def send_event(self, event):
        if type(event) == str:
            os.write(self._fd, event)
        elif hasattr(event, 'pack'):
            os.write(self._fd, event.pack())
        else:
            print >>sys.stderr, "Don't know what to do to send %r" % (event,)

if __name__ == '__main__':
    udev = UInputDevice("Test", 0x0, 0x1, 1)
    udev.create()
    from pyinputevent import InputEvent
    import sys
    udev.send_event(InputEvent.new(0,0,0))
    i = 100
    while i:
        i = i - 1
        sys.stdout.flush()
        #udev.send_event(InputEvent.new(4,4,20))
        #udev.send_event(InputEvent.new(1,20,1))
        #udev.send_event(InputEvent.new(0,0,0))
        #udev.send_event(InputEvent.new(4,4,20))
        #udev.send_event(InputEvent.new(1,20,0))
        #udev.send_event(InputEvent.new(0,0,0))
        udev.send_event(InputEvent.new(EV_REL, 0, 4))
        udev.send_event(InputEvent.new(EV_REL, 1, 4))
        udev.send_event(InputEvent.new(0,0,0))
        time.sleep(0.005)
    udev.destroy()