TechWolf
Posts: 2
Joined: Fri Apr 17, 2015 4:20 pm

RPI2 GPIO mapping a single button

Fri Apr 17, 2015 4:29 pm

Hi,

So first i'm a real raspberry pi and python noob... so apologies if this is way more straight forward then I seem to be making it. Essentially I'm trying to map a single push button so that when it is pushed it mimics the left shift key being pushed on a keyboard. It also will light up an LED.
Button to LED is working without any issue. However I can't seem to get anywhere with mapping to left shift.
After a bit of research I found uinput and got that all setup, and tried including it in a script called Howl.py when I run this I get the following:

Code: Select all

pi@raspberrypi ~/Desktop/Python $ modprobe uinput
pi@raspberrypi ~/Desktop/Python $ sudo python2 Howl.py
Traceback (most recent call last):
  File "Howl.py", line 11, in <module>
    device = uinput.Device(events)
  File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 167, in __init__
    ev_type, ev_code = ev_spec[:2]
TypeError: 'int' object has no attribute '__getitem__'
Is this a problem with how I've got uinput installed? Any guidance here would be really appreciated.
Thanks!

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: RPI2 GPIO mapping a single button

Fri Apr 17, 2015 5:50 pm

Can you post you code as well please, as that may give others a better idea of what the problem may be?
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

TechWolf
Posts: 2
Joined: Fri Apr 17, 2015 4:20 pm

Re: RPI2 GPIO mapping a single button

Fri Apr 17, 2015 5:53 pm

sorry for double post...
so i've managed to work out what was causing the error i think... putting square brackets seems to resolve the issue in part.
However it still does not activate the left shift key... here's my code;

Code: Select all

import RPi.GPIO as GPIO
import time
import uinput

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22,GPIO.IN)

device = uinput.Device([uinput.KEY_LEFTSHIFT])

while True:
    GPIO.output (4,GPIO.input(22))
    device.emit_click(uinput.KEY_LEFTSHIFT,1)
    time.sleep(0.05)

Return to “Python”