Python evdev.ecodes.EV_KEY Examples

The following are 21 code examples of evdev.ecodes.EV_KEY(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module evdev.ecodes , or try the search function .
Example #1
Source File: elobau_j6_joystick.py    From pyUSBtin with GNU General Public License v3.0 7 votes vote down vote up
def __init__(self, can_id):
        self.can_id = can_id
        axis_cap = AbsInfo(-32700,32700,0,0,0,0)
        self._ev = UInput(name='vjoy',
            events={
                 ecodes.EV_ABS: [
                     (ecodes.ABS_X, axis_cap),
                     (ecodes.ABS_Y, axis_cap),
                     (ecodes.ABS_Z, axis_cap)
                 ],
                 ecodes.EV_KEY: [
                     ecodes.BTN_TRIGGER, 
                     ecodes.BTN_TOP, 
                     ecodes.BTN_TOP2
                 ]
            }
        ) 
Example #2
Source File: elobau_j6_joystick.py    From pyUSBtin with GNU General Public License v3.0 6 votes vote down vote up
def signal(self, x, y, z, b0, b1, b2):
       self._ev.write(ecodes.EV_ABS, ecodes.ABS_X, x)
       self._ev.write(ecodes.EV_ABS, ecodes.ABS_Y, y)
       self._ev.write(ecodes.EV_ABS, ecodes.ABS_Z, z)
       self._ev.write(ecodes.EV_KEY, ecodes.BTN_TRIGGER, b0)
       self._ev.write(ecodes.EV_KEY, ecodes.BTN_TOP,     b1)
       self._ev.write(ecodes.EV_KEY, ecodes.BTN_TOP2,    b2)
       self._ev.syn() 
Example #3
Source File: keyboard_client.py    From Bluetooth_HID with GNU General Public License v3.0 5 votes vote down vote up
def event_loop(self):
        for event in self.dev.read_loop():
            # only bother if we hit a key and its an up or down event
            if event.type == ecodes.EV_KEY and event.value < 2:
                self.change_state(event)
                try:
                    self.send_input()
                except:
                    print("Couldn't send keyboard input")
                    break

    # forward keyboard events to the dbus service 
Example #4
Source File: device.py    From GPIOnext with MIT License 5 votes vote down vote up
def release( self ):
		self.holdTimer.cancel()
		self.holdTimer = threading.Timer( 0.35, self.hold)
		if self.isPressed:
			self.isPressed = 0
			self.injector.write(e.EV_KEY, self.command, 0)
			self.injector.syn() 
Example #5
Source File: device.py    From GPIOnext with MIT License 5 votes vote down vote up
def hold( self ):
		while self.isPressed:
			self.injector.write(e.EV_KEY, self.command, 2)
			self.injector.syn()
			time.sleep(.03) 
Example #6
Source File: device.py    From GPIOnext with MIT License 5 votes vote down vote up
def press( self ):
		self.isPressed = time.time()
		self.injector.write(e.EV_KEY, self.command, 1)
		self.injector.syn()
		try:
			self.holdTimer.start()
		except:
			pass
		self.waitForRelease() 
Example #7
Source File: device.py    From GPIOnext with MIT License 5 votes vote down vote up
def release( self ):
		if self.isPressed:
			self.isPressed = 0
			self.injector.write(e.EV_KEY, self.command, 0)
			self.injector.syn() 
Example #8
Source File: device.py    From GPIOnext with MIT License 5 votes vote down vote up
def press( self ):
		self.isPressed = time.time()
		self.injector.write(e.EV_KEY, self.command, 1)
		self.injector.syn()
		self.waitForRelease() 
Example #9
Source File: keyboard.py    From button-shim with MIT License 5 votes vote down vote up
def button_r_handler(button, pressed):
    keycode = KEYCODES[button]
    print("Release: {}".format(keycode))
    ui.write(e.EV_KEY, keycode, 0)
    ui.syn() 
Example #10
Source File: keyboard.py    From button-shim with MIT License 5 votes vote down vote up
def button_p_handler(button, pressed):
    keycode = KEYCODES[button]
    print("Press: {}".format(keycode))
    ui.write(e.EV_KEY, keycode, 1)
    ui.syn() 
Example #11
Source File: gaming_console.py    From gpio-game-console with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ButtonEvent(channel):                                                 
    if GPIO.input(JUMP_PIN) == 1:
        ui.write(e.EV_KEY, e.KEY_LEFTALT, 0)  
        ui.syn()
    if GPIO.input(JUMP_PIN) == 0:
        ui.write(e.EV_KEY, e.KEY_LEFTALT, 1) 
        ui.syn()
    if GPIO.input(FIRE_PIN) == 1:
        ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 0)  
        ui.syn()
    if GPIO.input(FIRE_PIN) == 0:
        ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 1) 
        ui.syn() 
Example #12
Source File: devices_client.py    From Bluetooth_HID with GNU General Public License v3.0 5 votes vote down vote up
def combined_event_loop(self):
        print("Starting combined event loop")
        devices = {dev.fd: dev for dev in [self.keyboard, self.mouse] if dev is not None}
        while True:
            r, w, e = select(devices, [], [])
            for fd in r:
                if devices[fd] == self.keyboard:
                    for event in self.keyboard.read():
                        # only bother if we hit a key and its an up or down event
                        if event.type == ecodes.EV_KEY and event.value < 2:
                            self.change_keyboard_state(event)
                            try:
                                self.send_keyboard_input()
                            except:
                                break

                else:
                    for event in self.mouse.read():
                        if event.type == ecodes.EV_KEY and event.value < 2:
                            self.change_state_button(event)
                        elif event.type == ecodes.EV_REL:
                            self.change_state_movement(event)
                        try:
                            self.send_mouse_input()
                        except :
                            break

######################################################################################################################## 
Example #13
Source File: mouse_client.py    From Bluetooth_HID with GNU General Public License v3.0 5 votes vote down vote up
def event_loop(self):
        for event in self.dev.read_loop():
            if event.type == ecodes.EV_KEY and event.value < 2:
                self.change_state_button(event)
            elif event.type == ecodes.EV_REL:
                self.change_state_movement(event)
            try:
                self.send_input()
            except:
                print("Couldn't send mouse input")
                break

    # forward mouse events to the dbus service 
Example #14
Source File: hid.py    From pyLCI with Apache License 2.0 5 votes vote down vote up
def runner(self):
        """Blocking event loop which just calls supplied callbacks in the keymap."""
        try:
            while not self.stop_flag:
                event = self.device.read_one()
                if event is not None and event.type == ecodes.EV_KEY:
                    key = ecodes.keys[event.code]
                    value = event.value
                    if value == 0 and self.enabled:
                        self.send_key(key)
                sleep(0.01)
        except IOError as e: 
            if e.errno == 11:
                #raise #Uncomment only if you have nothing better to do - error seems to appear at random
                pass 
Example #15
Source File: debug_listener.py    From pyLCI with Apache License 2.0 5 votes vote down vote up
def listen_for_events(dev):
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            print dev.name+":  "+str(categorize(event)) 
Example #16
Source File: Key_pin.py    From redeem with GNU General Public License v3.0 5 votes vote down vote up
def _handle_event(self, flags):
    logging.debug("Key_pin_listener handler start")
    if flags != select.POLLIN:
      logging.error("Key_pin_listener read failure: %s", str(flags))
      try:
        events = self.dev.read()
        logging.error("Key_pin_listener still got events: %s ?!?", str(events))
      except IOError as e:
        logging.error("Key_pin_listener read failed: %s", str(e))
      logging.error("Key_pin_listener removing key pin FD")
      self.iomanager.remove_file(self.dev)
    else:
      try:
        for event in self.dev.read():
          logging.debug("key pin event: %s", event)
          if event.type == ecodes.EV_KEY:
            code = int(event.code)
            val = int(event.value)
            if code in self.keys:
              key = self.keys[code]
              if (key.edge == 0xff or val == key.edge) and key.callback:
                logging.debug("sending key pin event to handler %s", str(key))
                key.callback(key, event)
                logging.debug("key pin event handler done")
      except IOError as e:
        logging.error("Key_pin_listener read failed in read event: %s", str(e))
        self.iomanager.remove_file(self.dev)
    logging.debug("Key_pin_listener handler end") 
Example #17
Source File: mouse_client.py    From keyboard_mouse_emulate_on_raspberry with MIT License 5 votes vote down vote up
def change_state(self, event):
        if event.type == ecodes.EV_SYN:
            current = time.monotonic()
            diff = 20/1000
            if current - self.last < diff and not self.change:
                return
            self.last = current
            speed = 1
            self.state[3] = min(127, max(-127, int(self.x * speed))) & 255
            self.state[4] = min(127, max(-127, int(self.y * speed))) & 255
            self.state[5] = min(127, max(-127, self.z)) & 255
            self.x = 0
            self.y = 0
            self.z = 0
            self.change = False
            BluetoothDevice.send_current(self.state)
        if event.type == ecodes.EV_KEY:
            debug("Key event %s %d", ecodes.BTN[event.code], event.value)
            self.change = True
            if event.code >= 272 and event.code <= 276 and event.value < 2:
                button_no = event.code - 272
                if event.value == 1:
                    self.state[2] |= 1 << button_no
                else:
                    self.state[2] &= ~(1 << button_no)
        if event.type == ecodes.EV_REL:
            if event.code == 0:
                self.x += event.value
            if event.code == 1:
                self.y += event.value
            if event.code == 8:
                self.z += event.value 
Example #18
Source File: evdevremapkeys.py    From evdevremapkeys with MIT License 5 votes vote down vote up
def read_events(req_device):
    for device in list_devices():
        # Look in all 3 identifiers + event number
        if req_device in device or \
           req_device == device[0].replace("/dev/input/event", ""):
            found = evdev.InputDevice(device[0])

    if 'found' not in locals():
        print("Device not found. \n"
              "Please use --list-devices to view a list of available devices.")
        return

    print(found)
    print("To stop, press Ctrl-C")

    for event in found.read_loop():
        try:
            if event.type == evdev.ecodes.EV_KEY:
                categorized = evdev.categorize(event)
                if categorized.keystate == 1:
                    keycode = categorized.keycode if type(categorized.keycode) is str \
                        else " | ".join(categorized.keycode)
                    print("Key pressed: %s (%s)" % (keycode, categorized.scancode))
        except KeyError:
            if event.value:
                print("Unknown key (%s) has been pressed." % event.code)
            else:
                print("Unknown key (%s) has been released." % event.code) 
Example #19
Source File: gui.py    From oversteer with GNU General Public License v3.0 4 votes vote down vote up
def read_events(self, device):
        for event in device.read():
            if event.type == ecodes.EV_ABS:
                if event.code == ecodes.ABS_X:
                    if self.emulation_mode != 'G29':
                        value = event.value * 4
                    else:
                        value = event.value
                    self.ui.set_steering_input(value)
                elif event.code == ecodes.ABS_Y:
                    if self.emulation_mode == 'DFGT' or self.emulation_mode == 'DFP':
                        self.ui.set_accelerator_input(event.value)
                    else:
                        self.ui.set_clutch_input(event.value)
                elif event.code == ecodes.ABS_Z:
                    if self.emulation_mode == 'DFGT' or self.emulation_mode == 'DFP':
                        self.ui.set_brakes_input(event.value)
                    else:
                        self.ui.set_accelerator_input(event.value)
                elif event.code == ecodes.ABS_RZ:
                    self.ui.set_brakes_input(event.value)
                elif event.code == ecodes.ABS_HAT0X:
                    self.ui.set_hatx_input(event.value)
                    if event.value == -1:
                        self.on_button_press(100, 1)
                    elif event.value == 1:
                        self.on_button_press(101, 1)
                elif event.code == ecodes.ABS_HAT0Y:
                    self.ui.set_haty_input(event.value)
                    if event.value == -1:
                        self.on_button_press(102, 1)
                    elif event.value == 1:
                        self.on_button_press(103, 1)
            if event.type == ecodes.EV_KEY:
                if event.value:
                    delay = 0
                else:
                    delay = 100
                if event.code >= 288 and event.code <= 303:
                    button = event.code - 288
                if event.code >= 704 and event.code <= 712:
                    button = event.code - 688
                self.ui.set_btn_input(button, event.value, delay)
                self.on_button_press(button, event.value) 
Example #20
Source File: joy.py    From aws-builders-fair-projects with Apache License 2.0 4 votes vote down vote up
def handle_event(device, myMQTTClient):
    async for event in device.async_read_loop():
        for num, joystick in enumerate(joysticks):
            if joystick['device']==device:
                jsindex = num
        categorized = categorize(event)
        if event.type == ecodes.EV_KEY:
            logging.debug(f'button push {joysticks[jsindex]["color"]} {joysticks[jsindex]["path"]}: {categorized.keycode}, {categorized.keystate}')
            logging.info(f'move {joysticks[jsindex]["move"]} locked {joysticks[jsindex]["movelocked"]}')
            if (categorized.keycode[0] == KEY_JOYSTICK or categorized.keycode[0]
                == KEY_BTNA) and categorized.keystate == 1 and not (
                joysticks[jsindex]['move'] == '') and not (
                joysticks[jsindex]['movelocked'] == True):
                #submit move if there is one
                logging.debug(f'move submitted for {joysticks[jsindex]["color"]}, {joysticks[jsindex]["device"]}:{joysticks[jsindex]["move"]}')
                joysticks[jsindex]['movelocked']=True
                message = {}
                message['joystick']=joysticks[jsindex]['color']
                message['move']=joysticks[jsindex]['move']
                jsonmsg = json.dumps(message)
                topic = joysticks[jsindex]['movetopic']
                myMQTTClient.publish(topic, jsonmsg, 0)
                logging.info(f'posted move {jsonmsg}')
                await setLightStatus(jsindex, MOVE_LOCKED)
            elif (categorized.keycode==KEY_THUMB or categorized.keycode==KEY_TL2
                ) and categorized.keystate==1:
                #for debugging, clear move
                joysticks[jsindex]['move'] = ''
                joysticks[jsindex]['movelocked']=False
                await setLightStatus(jsindex, READY_FOR_MOVES)
                logging.info(f'{joysticks[jsindex]["color"]} {joysticks[jsindex]["path"]} unlocked')
                logging.info(f'{joysticks}')
        elif event.type == ecodes.EV_ABS and joysticks[jsindex]['movelocked']==False and (
            event.value == ABS_LEFT or event.value==ABS_RIGHT):
            logging.debug(f'joystick move {joysticks[jsindex]["move"]} {joysticks[jsindex]["path"]} value: {event.value} {event}')
            if event.code == ABS_X:
                if event.value == ABS_RIGHT:
                    joysticks[jsindex]['move'] = MOVE_RIGHT
                    logging.info(f'{joysticks[jsindex]["color"]} {joysticks[jsindex]["path"]} right')
                elif event.value == ABS_LEFT:
                    joysticks[jsindex]['move'] = MOVE_LEFT
                    logging.info(f'{joysticks[jsindex]["color"]} {joysticks[jsindex]["path"]} left')
            elif event.code == ABS_Y:
                if event.value == ABS_UP:
                    joysticks[jsindex]['move'] = MOVE_FORWARD
                    logging.info(f'{joysticks[jsindex]["color"]} {joysticks[jsindex]["path"]} forward') 
Example #21
Source File: evdevremapkeys.py    From evdevremapkeys with MIT License 4 votes vote down vote up
def register_device(device):
    for value in registered_devices.values():
        if device == value['device']:
            return value['future']

    input = find_input(device)
    if input is None:
        return None
    input.grab()

    caps = input.capabilities()
    # EV_SYN is automatically added to uinput devices
    del caps[ecodes.EV_SYN]

    remappings = device['remappings']
    extended = set(caps[ecodes.EV_KEY])

    modifier_groups = []
    if 'modifier_groups' in device:
        modifier_groups = device['modifier_groups']

    def flatmap(lst):
        return [l2 for l1 in lst for l2 in l1]

    for remapping in flatmap(remappings.values()):
        if 'code' in remapping:
            extended.update([remapping['code']])

    for group in modifier_groups:
        for remapping in flatmap(modifier_groups[group].values()):
            if 'code' in remapping:
                extended.update([remapping['code']])

    caps[ecodes.EV_KEY] = list(extended)
    output = UInput(caps, name=device['output_name'])
    print('Registered: %s, %s, %s' % (input.name, input.path, input.phys), flush=True)
    future = asyncio.ensure_future(
        handle_events(input, output, remappings, modifier_groups))
    registered_devices[input.path] = {
        'future': future,
        'device': device,
    }
    return future