AshleyC
Posts: 26
Joined: Sat Jun 16, 2012 1:34 pm
Location: Derby

Keyboard Input

Sat Jun 16, 2012 1:39 pm

Hi all,

Currently waiting for my raspberry pi to arrive (about 7weeks) and until then i have installed python on my windows based pc to get to grips with it.

I would like a little help with getting keystrokes into my program
example "if up arrow=true then do this or that"

so any examples would be great, thanks for your time :)

sharpapotheosis
Posts: 58
Joined: Thu May 24, 2012 6:47 pm

Re: Keyboard Input

Sat Jun 16, 2012 3:56 pm

If you're running a console program and want this functionality, you might want to look into pygame. http://www.karoltomala.com/blog/?p=679 gives a pretty good example of how to get it up and running, and then you can use the pygame commands to grab the key inputs (have a look at the manual if you need help with that)

AshleyC
Posts: 26
Joined: Sat Jun 16, 2012 1:34 pm
Location: Derby

Re: Keyboard Input

Sat Jun 16, 2012 4:04 pm

sharpapotheosis wrote:If you're running a console program and want this functionality, you might want to look into pygame. http://www.karoltomala.com/blog/?p=679 gives a pretty good example of how to get it up and running, and then you can use the pygame commands to grab the key inputs (have a look at the manual if you need help with that)
Thanks for that, shall check that out :)

AshleyC
Posts: 26
Joined: Sat Jun 16, 2012 1:34 pm
Location: Derby

Re: Keyboard Input

Sun Jun 17, 2012 2:44 pm

Still struggling with this, let me start from the beginning and explain what im trying to do :)

When my Raspberry Pi arrives i intend to use it as a home security system incorporating ready made boards from maplins such as "infra-red beam" to use across the driveway and standard door/window sensors.

I would also like to add a lawn watering system again using ready made boards such as moisture and temperature sensors to make it a bit more hitec than just turning on the sprinkler system whether its already been raining or not.

At the moment im using Python for windows and just getting to grips with the commands ready for when my Raspberry Pi arrives, so need to be able to load whatever i have done on the windows based program and transfer to the Pi.

I have limited knowledge of programming but used to be great at getting my old spectrum to do what i wanted and have also used Dark Basic for windows.

Before i start diving in deeper with all the code to get information from/to the GPIO on the Pi to work with everything i attach to it, i want to emulate the state of the sensors being activated by a single key press so i can make sure the main loop of the program works.

Im stuck at this point lol and have only just started the project, on the spectrum it would be something like...

10 rem alarm system
20 if inkey$="a" then gosub 100
30 if inkey$="b"then gosub 200
40 goto 20

100 print" Infra-red beam broken"
110 return

200 print"window open"
210 return

So how can i achieve the above using python using least amount of code and without having to install other software?

Thanks again

P.S Just wait until i get to the point of coding the GPIO pins, i think i will be living on here :)

KCarscadden
Posts: 56
Joined: Sat Jun 09, 2012 9:00 pm
Location: Canada

Re: Keyboard Input

Sun Jun 17, 2012 5:53 pm

This is more or less equivalent to your example. To get out of the loop, use cntrl/c or some such. Note that this causes an exception, which is not trapped, so the program exits, it doesn't get to - print "Out of loop".

Code: Select all

# alarm system

def InfraRedBeamBroken():
	print "Infra-red beam broken"
	return ()
	
def WindowOpen():
	print "window open"
	return ()

	
# main 

while 1:
	
	inkey = raw_input()
	if inkey == "a":
		InfraRedBeamBroken()
	elif inkey == "b":
		WindowOpen()
	else:
		print "Other input"
	
print "Out of loop"

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Keyboard Input

Sun Jun 17, 2012 7:18 pm

You could always add a capture for "q" or somesuch to get you out of the loop. :)

AshleyC
Posts: 26
Joined: Sat Jun 16, 2012 1:34 pm
Location: Derby

Re: Keyboard Input

Mon Jun 18, 2012 6:04 am

KCarscadden wrote:This is more or less equivalent to your example. To get out of the loop, use cntrl/c or some such. Note that this causes an exception, which is not trapped, so the program exits, it doesn't get to - print "Out of loop".

Code: Select all

# alarm system

def InfraRedBeamBroken():
	print "Infra-red beam broken"
	return ()
	
def WindowOpen():
	print "window open"
	return ()

	
# main 

while 1:
	
	inkey = raw_input()
	if inkey == "a":
		InfraRedBeamBroken()
	elif inkey == "b":
		WindowOpen()
	else:
		print "Other input"
	
print "Out of loop"
Thank you, exactly how i wanted it to work :)

Return to “Python”