BigJeffIE
Posts: 26
Joined: Fri May 11, 2012 7:55 pm

Capturing key press in python

Sun Sep 09, 2012 7:19 pm

Hi folks,

We've written a flashing LED game that we want to be able to hit the space bar when all 3 LEDs are on. So far the only thing we've been able to do is hit <CTRL> - C to stop, is it possible to capture code it so it waits 10 seconds when the SPACE bar is pressed and then resumes.

Thanks.

Code so far...

# Flashing led game:)
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) #Uses the pins as numbers rather than the GPIO numbers

# Set up header pin 11 as an input
print "Setup Pin 11,13,15"
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)

wait=0.006
var=1
print "Start loop"
while var==1:

print "green on"
GPIO.output(15, True)
time.sleep(wait)

print "yellow on"
GPIO.output(13, True)
time.sleep(wait)

print "red on"
GPIO.output(11, True)
time.sleep(wait)

print "red off"
GPIO.output(11, False)
time.sleep(wait)

print "yellow off"
GPIO.output(13, False)
time.sleep(wait)

print "green off"
GPIO.output(15, False)
time.sleep(wait)

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Capturing key press in python

Sun Sep 09, 2012 9:19 pm

I'd use Pygame's event handling stuff to detect keypresses (and handle time), but that's because it's all I know...

Is there any special reason you're wanting to use the keyboard, btw? Wouldn't a button connected to GPIO fit the theme better?

I'm not much help, sorry.
note: I may or may not know what I'm talking about...

Clark
Posts: 1
Joined: Mon Sep 10, 2012 8:22 am

Re: Capturing key press in python

Mon Sep 10, 2012 8:29 am

Hi
I have been trying to do the same thing and after a lot of searching I found this. Not sure how it works but does the job!

Its python 3 though.

Code: Select all

import os
import sys
import termios
import tty

def getKey():
	fd = sys.stdin.fileno()
	old = termios.tcgetattr(fd)
	new = termios.tcgetattr(fd)
	new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
	new[6][termios.VMIN] = 1
	new[6][termios.VTIME] = 0
	termios.tcsetattr(fd, termios.TCSANOW, new)
	key = None
	try:
		key = os.read(fd, 3)
	finally:
		termios.tcsetattr(fd, termios.TCSAFLUSH, old)
	return key

	
while 1:
	x = str(getKey())
	if x == "b'  '":
		print('found')
	else:
		print(x)
the line
if x == "b' '":
is the one to change to look for another key.

Hope that helps

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: Capturing key press in python

Sun Apr 19, 2015 6:05 pm

Hope nobody minds me piggy-backing on this - I'm trying to do exactly the same.
I've written a routine which flashes an led on and off and I want to write some code which will detect a key-press or mouse click and exit the routine.
I know I could do this with a switch wired to a gpio pin and the code to do it looks simple but:
a) I haven't got a switch to hand at the moment and
b) The reason I'm doing this is to learn the software - not hardware.

When I run the above code I get :
"NameError: global name 'termios' is not defined"
I'm wondering if this is because the code above is quite old - could something have changed so that routine doesn't exist anymore?
Any other thoughts?

Thanks

dema86
Posts: 1
Joined: Wed Jun 17, 2015 12:35 pm

Re: Capturing key press in python

Wed Jun 17, 2015 12:39 pm

Clark wrote:{...}

Hope that helps
Thanks a lot, you helped me ;)
P_Monty wrote: When I run the above code I get :
"NameError: global name 'termios' is not defined"
I'm wondering if this is because the code above is quite old - could something have changed so that routine doesn't exist anymore?
Any other thoughts?

Thanks
Are you sure you are executing this code with python3 ? I have a brand new Pi2, and using python3 this code works fine ;)

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: Capturing key press in python

Mon Jul 06, 2015 5:04 pm

Sorry for the delay in replying.
I'm fairly sure - as I'm using 'sudo Python3 name.py' to run it. I'll double check at the weekend though...

Return to “Python”