Keypress Input

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jun 18 15:35:05 EDT 2015


On Wed, 17 Jun 2015 at 02:23 Michael Torrie <torriem at gmail.com> wrote:

> On 06/16/2015 02:49 PM, Grant Edwards wrote:
> > On 2015-06-16, John McKenzie <davros at bellaliant.net> wrote:
> >
> >> It never occurred to me something so simple as keystrokes would not
> >> be present in Python, a language rated as being terrific by everyone
> >> I know who knows it.
> >
> > Ah, but in reality "keystrokes" is not simple at all.  Keyboards and
> > input handling is a very messy, complicated area.
>
> If you do choose to go with the GPIO route, unless your code for
> accessing the GPIO lines does debouncing, you will have to debounce the
> key.  There are lots of examples out there (most in C on the arduino,
> but still applicable). Most of them check for a button press, then do a
> timer count-down to let things settle out before recording a button
> press.  So it's still complicated even if you talk directly to the
> buttons!  No way around some complexity though.
>

I use the following. I found in testing that when you push the button it
prints 'Button pressed' 10 times a second (in actual use it calls poweroff
so I guess bounce isn't an issue there). Is there some reason it needs to
be cleverer in this case?

 #!/usr/bin/env python

import RPi.GPIO as GPIO
import subprocess
import time

PIN_NUM = 21

GPIO.setmode(GPIO.BCM)

GPIO.setup(PIN_NUM, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    time.sleep(0.1)
    if not GPIO.input(PIN_NUM):
        print('Button pressed')

--
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150618/70f36c0e/attachment.html>


More information about the Python-list mailing list