Created
March 28, 2018 21:08
-
-
Save myxoh/d819a24183b41f4d71524af30dda28bd to your computer and use it in GitHub Desktop.
Key Event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import evdev | |
from evdev import KeyEvent, InputDevice | |
class KeyListener: | |
pressed_keys = [False] * 256 | |
UP = evdev.ecodes.KEY_UP | |
DOWN = evdev.ecodes.KEY_DOWN | |
LEFT = evdev.ecodes.KEY_LEFT | |
RIGHT = evdev.ecodes.KEY_RIGHT | |
SPACE = evdev.ecodes.KEY_SPACE | |
ESC = evdev.ecodes.KEY_ESC | |
LEFTCTRL = evdev.ecodes.KEY_LEFTCTRL | |
LEFTSHIFT = evdev.ecodes.KEY_LEFTSHIFT | |
def __init__(self): | |
self.initialize_device() | |
def press(self, key): | |
self.pressed_keys[key] = True | |
def release(self, key): | |
self.pressed_keys[key] = False | |
def react(self, keyEvent): | |
if keyEvent.keystate == keyEvent.key_down: | |
self.press(keyEvent.scancode) | |
if keyEvent.keystate == keyEvent.key_up: | |
self.release(keyEvent.scancode) | |
def initialize_device(self): | |
devices = [InputDevice(fn) for fn in evdev.list_devices()] | |
keyboard_port = filter(lambda device: "keyboard" in device.name, devices)[0].fn | |
self.device = InputDevice(keyboard_port) | |
def read(self): | |
event = self.device.read_one() | |
if not event is None and event.type == evdev.ecodes.EV_KEY: | |
self.react(evdev.categorize(event)) | |
return self.pressed_keys | |
class Pepe: | |
def is_shutdown(self): | |
return False | |
rospy = Pepe() | |
keyListener = KeyListener() | |
import time | |
isLaunched = False | |
while not rospy.is_shutdown(): | |
pressed_keys = keyListener.read() | |
if(pressed_keys[keyListener.SPACE] and not isLaunched): | |
isLaunched = True | |
print("Take Off!") | |
if(pressed_keys[keyListener.ESC] and isLaunched): | |
isLaunched = False | |
print("Landing!") | |
if(pressed_keys[keyListener.UP]): | |
print("We are going forward!") | |
if(pressed_keys[keyListener.DOWN]): | |
print("We are going backward") | |
if(pressed_keys[keyListener.LEFT]): | |
print("You spin me round baby (LEFT)") | |
if(pressed_keys[keyListener.RIGHT]): | |
print("You spin me round baby (RIGHT)") | |
if(pressed_keys[keyListener.LEFTSHIFT]): | |
print("We are going UP!") | |
if(pressed_keys[keyListener.LEFTCTRL]): | |
print("May day may day we are going DOWN!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment