dox
Posts: 6
Joined: Sat Feb 14, 2015 3:11 pm

keyboard recognition by pygame

Sat Feb 14, 2015 3:34 pm

Hi, I am new to programming a raspberry pi (version B+), but I have used Basic in the past.
I am having trouble with pygame recognising keyboard key presses.
I am using identical code to that used in the "Snake" game, but it does not work.
I have searched the internet for solutions, but none of the suggestions or code variations have worked.

I am trying to control the GPIO ports with the keyboard and wondered if there is a conflict between these ports and pygame.
Also, I wondered if others had had similar issues with a recent new raspberry pi unit and software.
I have tried updating the software with no change to the situation.

Any advice would be appreciated.
Regards,
Derek

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: keyboard recognition by pygame

Tue Feb 17, 2015 12:44 pm

Maybe try dividing your issue to smallest possible blocks...

Does your keyboard reading work by itself (without GPIO involved) (make pygame show something at key press or log them into a file)?

Does the GPIO code work by itself (without pygame and keyboard involved)?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

dox
Posts: 6
Joined: Sat Feb 14, 2015 3:11 pm

Re: keyboard recognition by pygame

Tue Feb 17, 2015 6:57 pm

Hi rst, Many thanks for your reply.
Yes the GPIO code works fine on its own; it is just pygame detecting the key presses that does not work.
I will try your suggestion of a separate short code with just the key press part and without the GPIO section.
I will let you know how I progress.
Regards, Derek

jimallyn
Posts: 51
Joined: Wed Nov 12, 2014 9:13 am

Re: keyboard recognition by pygame

Tue Feb 17, 2015 7:11 pm

Are you using that code in a loop? You have to run a loop in pygame, and look for keypresses every time you go through the loop. Or at least that's the way it's shown in the Python book I'm working through.
“So you say you love the poor? Name them.” - Gustavo Gutierrez

dox
Posts: 6
Joined: Sat Feb 14, 2015 3:11 pm

Re: keyboard recognition by pygame

Wed Feb 18, 2015 10:17 am

Hi, I have tried removing the GPIO section of the code in a simple example below:
----------------------------------------------------------------
# !/usr/bin/env python
# Test Program - A key prints out a message

import pygame, sys
from pygame.locals import *

pygame.init()

print ("To switch light ON prees 'y' key ")
print ("To switch light OFF press? 'n' key ")

while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_y:
print ("light on")
elif event.type == pygame.KEYDOWN and event.key == pygame.K_n:
print ("light off")
----------------------------------------------
When run this still does not cause pygame to recognise key presses and print out the simple messages.
Assuming there is not an obvious error in the above code I am wondering if the pygame program is faulty in some way!
Much appreciation for your help.
Derek

DirkS
Posts: 9900
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: keyboard recognition by pygame

Wed Feb 18, 2015 10:22 am

Please put Python code in a 'code block'.
http://www.raspberrypi.org/forums/viewt ... 32&t=84477

PiGraham
Posts: 3575
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: keyboard recognition by pygame

Wed Feb 18, 2015 10:24 am

Are you running this with a directly connected USB keyboard, or via SSH/VNC?

gordon77
Posts: 4111
Joined: Sun Aug 05, 2012 3:12 pm

Re: keyboard recognition by pygame

Wed Feb 18, 2015 10:43 am

It appears you have to have a pygame window open and active

Code: Select all

import pygame, sys
from pygame.locals import *

pygame.init()
windowSurfaceObj = pygame.display.set_mode((640,480),1,16)

print ("To switch light ON prees 'y' key ")
print ("To switch light OFF press? 'n' key ")

while True:
   x = 1
   for event in pygame.event.get():
      if event.type == KEYDOWN and event.key == K_y:
          print ("light on")
      elif event.type == KEYDOWN and event.key == K_n:
          print ("light off")

dox
Posts: 6
Joined: Sat Feb 14, 2015 3:11 pm

Re: keyboard recognition by pygame

Wed Feb 18, 2015 11:42 am

Hi Gordon,
Many, many thanks for the solution to this problem.
I can't quite see the logic behind having to have an unused pygame window open and active.
Maybe because pygame always assumes you want to use the key presses in a game window rather than for any other purpose.

Where did you find the solution to this as I had searched and searched the internet and the pygame website for an answer?

Thanks again,
Derek

gordon77
Posts: 4111
Joined: Sun Aug 05, 2012 3:12 pm

Re: keyboard recognition by pygame

Wed Feb 18, 2015 11:50 am

I tried your code and found the same issue. As I use this in my programs I couldn't understand why it didn't work so I tested with my program and found if you don't have the pygame window open it doesn't work.

Gordon77

dox
Posts: 6
Joined: Sat Feb 14, 2015 3:11 pm

Re: keyboard recognition by pygame

Wed Feb 18, 2015 12:03 pm

Hi Gordon,
Thanks for solving this by trial and error for me.
Just for your interest I have bought the raspberry-pi and I am refreshing my programming experience so that I can automate a fireworks display for the grandchildren next Nov 5th. I have most of the hardware sorted out, but now need to get the program sorted.
Regards
Derek

gordon77
Posts: 4111
Joined: Sun Aug 05, 2012 3:12 pm

Re: keyboard recognition by pygame

Wed Feb 18, 2015 1:17 pm

dox wrote:Hi Gordon,
Thanks for solving this by trial and error for me.
Just for your interest I have bought the raspberry-pi and I am refreshing my programming experience so that I can automate a fireworks display for the grandchildren next Nov 5th. I have most of the hardware sorted out, but now need to get the program sorted.
Regards
Derek
I'd prefer to call it a process of elimination ;)

Good Luck with the fireworks :D

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: keyboard recognition by pygame

Wed Feb 18, 2015 5:49 pm

This might be worth a look - you can use pygame without a visible window: http://www.pygame.org/wiki/DummyVideoDriver

I haven't tested this with trying to capture keyboard inputs though...
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

dox
Posts: 6
Joined: Sat Feb 14, 2015 3:11 pm

Re: keyboard recognition by pygame

Thu Feb 19, 2015 9:05 am

Hi elParaguayo,
I will try this out and see if it works.
Thanks Derek

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: keyboard recognition by pygame

Thu Feb 19, 2015 10:15 am

I've used the dummy video driver in my PiPlayBox but that doesn't have a display (other than the LCD). The script, however, does contain an event loop so I'd be hopeful that you could get it to work.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.


User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: keyboard recognition by pygame

Thu Feb 19, 2015 3:12 pm

Well, I only took a look at the last link and it seems pretty clear - keyboard events are not caught when using dummy driver.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”