Advertisement
Guest User

QWOP keys in Linux

a guest
Aug 8th, 2012
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python
  2. # dependancy: https://launchpad.net/python-uinput/
  3.  
  4. # http://i.imgur.com/KwU6s.png
  5.  
  6. import uinput
  7.  
  8. from time import sleep
  9.  
  10. class Keyboard:
  11.     """Creates a virtual keyboard which can emulate a real keyboard"""
  12.  
  13.     def __init__(self, cs):
  14.         self.events = dict((i, self.c2key(i)) for i in cs)
  15.         self.keyboard = uinput.Device(tuple(self.events.values()))
  16.  
  17.         # There is a delay before the keyboard starts working
  18.         # At an order of magnitude of -2
  19.         sleep(1e-1)
  20.  
  21.     def c2key(self, c):
  22.         return eval('uinput.KEY_' + c.upper())
  23.  
  24.     def press(self, c):
  25.         self.keyboard.emit(self.events[c], 1)
  26.  
  27.     def release(self, c):
  28.         self.keyboard.emit(self.events[c], 0)
  29.  
  30. def qwop():
  31.     keyboard = Keyboard(["q", "w", "o", "p"])
  32.  
  33.     while 1:
  34.         keyboard.press("w")     # Press         'W'
  35.         sleep(1e-1)             #   Wait        100 ms
  36.         keyboard.press("o")     # Press         'O'
  37.         sleep(1e-1)             #   Wait        100 ms
  38.         keyboard.release("o")   #   Release     'O'
  39.         keyboard.release("w")   #   Release     'W'
  40.         sleep(1e-1)             #   Wait        100 ms
  41.         keyboard.press("q")     # Press         'Q'
  42.         keyboard.release("p")   #   Release     'P'
  43.         sleep(2e-1)             #   Wait        200 ms
  44.         keyboard.press("p")     # Press         'P'
  45.         keyboard.release("q")   #   Release     'Q'
  46.         sleep(1e-1)             #   Wait        100 ms
  47.         keyboard.release("p")   #   Release     'P'
  48.         keyboard.press("p")     # Press         'P'
  49.         sleep(1e-1)             #   Wait        100 ms
  50.  
  51. if __name__ == '__main__':
  52.     qwop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement