fdr4prez
Posts: 17
Joined: Fri May 13, 2016 8:06 pm

uinput and F# keyboard buttons

Fri May 13, 2016 10:00 pm

Please let me start off by saying that I have zero Python programming experience, and I've not really done any programming in 20+ years since some programming classes in college, so please bare with me here...

I've searched around to help get me some Python code to monitor the GPIO and trigger a keyboard event based on some IOs being triggered.

This is working good for me, but I'd like to have F1, F12 and Pause keyboard button events being triggered from the GPIO and the uinput doesn't seem to support these keyboard button events.

Is there another way in Python to trigger these keyboard buttons events? Or is uinput the only method of triggering keyboard events? Is there a way in C to get these keyboard events?

Here is my current code:

Code: Select all

import uinput
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP) 

device = uinput.Device([uinput.KEY_P, uinput.KEY_R, uinput.KEY_Q])

# Booleans to keep track of the buttons 
Pause = False
Reset = False
Quit = False

while True:
    if (not Pause) and (not GPIO.input(26)):  # Pause button pressed 
       Pause = True 
       device.emit(uinput.KEY_P, 1)           # press P keyboard button
 
    if Pause and GPIO.input(26):              # Pause button released 
       Pause = False 
       device.emit(uinput.KEY_P, 0)           # P keyboard button released

    if (not Reset) and (not GPIO.input(20)):  # Reset button pressed 
       Reset = True 
       device.emit(uinput.KEY_R, 1)           # press R keyboard button
 
    if Reset and GPIO.input(20):              # Reset button released 
       Reset = False 
       device.emit(uinput.KEY_R, 0)           # R keyboard button released

    if (not Quit) and (not GPIO.input(21)):   # Quit button pressed 
       Quit = True 
       device.emit(uinput.KEY_Q, 1)           # press Q keyboard button
 
    if Quit and GPIO.input(21):               # Quit button released 
       Quit = False 
       device.emit(uinput.KEY_Q, 0)           # Q keyboard button released
    time.sleep(0.2)

Return to “Python”