When I am trying to catch keys pressed, they are printed in Terminal, but not caught by pygame and the script. Script is executed as follows:
>>>import scriptname
>>>scriptname.wa()
scriptname file:
import pygame
from pygame.locals import *
def wa():
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
alive_key = True
while alive_key:
for event in pygame.event.get():
if event.type == QUIT:
alive_key = False
elif event.type == KEYDOWN and event.key == K_q:
print '\nThis is not happening\n'
screen.fill((0, 0, 0))
if pygame.mouse.get_pressed()[0]:
pygame.event.post(
pygame.event.Event(KEYDOWN, key=K_q, mod=0, unicode=u'q'))
pygame.display.update()
pygame.quit()
If events are created on mouse press (as presented in code), they work.
I am using OS X 10.8.5, python 2.7, pygame2.7 1.9.1. Everything works perfectly in Windows 7 with similar configuration.
Thanks!
change your code to:
elif event.type == KEYDOWN or event.type == pygame.KEYDOWN:
# for testing purpose
print event
if event.key == pygame.K_q:
print '\nThis is not happening\n
I'm using OS X 10.9.5, python 2.7.7, pygame-1.9.2pre-py2.7-macosx10.7. Had the similar problem as yours earlier. I found this solution here: http://content.gpwiki.org/index.php/Python:Pygame_keyboard_input
I think this is not mac's fault, possibly.
Related
Okay, so I am writing a script that controls the motors using a sbc motor shield board with my raspberry pi 3b.
The issue I am having is that if I just run the script a window will pop up for a split second allowing to press the a key to move the motor forward or any other keys that I have already defined within the script but it only last for a second before the window auto exits and I am returned to the terminal.
Now when I attempt to add a loop to keep the code running the window will stay open however it no longer recognizes when I press a key defined in the script.. I have spent hours researching and modifying the script I have wrote and have not been able to find a solution. I am very new to python and I appreciate any input given.
Also I am running python 3.6 thanks in advance
import pygame
import sys
import pygame.locals
import PiMotor
import time
m1 = PiMotor.Motor("MOTOR1",1)
m2 = PiMotor.Motor("MOTOR2",1)
pygame.init()
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Johnny, motor controls!')
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.locals.QUIT:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
m1.forward(100), time.sleep(0)
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
m1.stop()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
m1.forward(100)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
m1.stop()
You just need to add a while True: when you get the events and check when to quit.
If you tried it and it didn't work maybe you had your indentation wrong? I see that it is wrong now in the code that you posted.
Something like:
while True:
for event in pygame.event.get():
if event.type == pygame.locals.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
m1.forward(100), time.sleep(0) # BTW, do you really need this sleep?
[... Rest of your code ...]
I'm using pygame and pyautogui to move the mouse around the screen in python 2.7. My code looks like:
import pyautogui
import pygame
pygame.init()
pygame.display.set_mode()
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
pyautogui.moveRel(-50,0)
My code moves the mouse to the left when I press "a" but I have to repeatedly press the button when I want to move the mouse across the screen. Is there a way to be able to press and hold a and move the mouse across the screen? I've looked at other tutorials on this topic but they seem very project specific.
Basically, what you want to do is set a variable indicating whether the key is down on keydown and update it once key is up.
Here, I updated your code to do just that as it might be easier to understand.
import pyautogui
import pygame
loop = True
a_key_down = False # Added variable
while loop:
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
a_key_down = True # Replaced keydown code with this
if event.type == pygame.KEYUP: # Added keyup
if event.key == pygame.K_a:
a_key_down = False
if a_key_down: # Added to check if key is down
pyautogui.moveRel(-50,0)
I am trying to change the mouse position programmatically, in PyGame, using its mouse API:
from pygame import mouse
mouse.set_pos(0, 0)
This seems to work fine in windowed mode, but in fullscreen mode, nothing happens. I've tried forcefully making the cursor invisible then showing it again, in vain hope that it would reset the thing, but no luck.
EDIT In the interest of full disclosure, I'm not using PyGame directly. I'm using OpenSesame, with its "Legacy" backend (which is essentially a wrapper over PyGame). However, I do have access to PyGame primitives, if needs be.
https://www.pygame.org/docs/ref/mouse.html#comment_pygame_mouse_get_pos
Test with a print to see if 0,0 coords are on the screen
Python 2.7
#!/usr/bin/python
import pygame
from pygame.locals import *
def main():
pygame.init()
pygame.display.set_mode((300,200))
pygame.display.set_caption('Testing')
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
if event.type == MOUSEBUTTONDOWN:
#print event.button
print pygame.mouse.get_pos()
pygame.display.quit()
if __name__ == '__main__':
main()
Python 3.4
#!/usr/bin/python
import pygame
from pygame.locals import *
def main():
pygame.init()
pygame.display.set_mode((300,200))
pygame.display.set_caption('Testing')
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
if event.type == MOUSEBUTTONDOWN:
#print event.button
print(pygame.mouse.get_pos())
pygame.display.quit()
if __name__ == '__main__':
main()
I am using a function to wait until a controller is present, but i ran into a problem. I wanted the function to stop when I press a key. Usually these events are handled just fine, but in this part it works terrible. The event seems to be added to the event queue very late or sometimes not at all. My guess is that it is because of the uninitialisation and reinitialisation of pygame.joystick. I just don't know how to solve it. I used this program to create the error:
import pygame, sys
from pygame.locals import *
pygame.init()
SCREEN = pygame.display.set_mode((500,500))
ChangeRun = False
pygame.joystick.init()
if pygame.joystick.get_count() == 0:
while pygame.joystick.get_count() == 0:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type in [KEYUP, KEYDOWN]:
if event.key == K_ESCAPE:
ChangeRun = True
if ChangeRun == True:
break
pygame.joystick.quit()
pygame.joystick.init()
pygame.quit()
sys.exit()
I wanted to use the escape key to end the program, but only after a lot of pressing it works. The same happens with the QUIT event. When printing the events I found that most of the time no event was found.
I use windows 8.1 and python 3.4 and pygame 1.9.2
I have no joystick so I can't test it but normally I would do this similar to mouse and I wouldn't wait for controller:
import pygame
from pygame.locals import *
pygame.init()
pygame.joystick.init()
screen = pygame.display.set_mode((500,500))
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == JOYBUTTONDOWN:
print 'joy:', event.joy, 'button:', event.button
# the end
pygame.joystick.quit()
pygame.quit()
I'm using pygame to draw a line:
import pygame
from pygame.locals import*
import sys
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Lines")
while True:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
sys.exit()
screen.fill((0,80,0))
color = 100,255,200
width = 8
pygame.draw.line(screen, color, (100,100), (500,400), width)
pygame.display.update()
for some reason, I can't get this is work:
while True:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
sys.exit()
It doesnt show any error, it just doesnt work. I want to be able to press the down key and have it quit the program but it doesnt do that. I have to quit the idle. Any help will do. thanks.
This is because KEYDOWN is just the event that a key is pressed down, not that the down key has been pushed. To fix this, you have to first check if a KEYDOWN event has happened, and if it has, check what key that was pushed.
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
sys.exit()
Check out the docs on this subject to learn more.