On Wed, 17 Jun 2015 at 02:23 Michael Torrie <torr...@gmail.com> wrote:
> On 06/16/2015 02:49 PM, Grant Edwards wrote: > > On 2015-06-16, John McKenzie <dav...@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
-- https://mail.python.org/mailman/listinfo/python-list