how to detect when a key is pressed in pygame

Solutions on MaxInterview for how to detect when a key is pressed in pygame by the best coders in the world

showing results for - "how to detect when a key is pressed in pygame"
Paul
06 Sep 2017
1import pygame
2events = pygame.event.get()
3for event in events:
4    if event.type == pygame.KEYDOWN:
5        if event.key == pygame.K_LEFT:
6            location -= 1
7        if event.key == pygame.K_RIGHT:
8            location += 1
9
Lya
03 Sep 2016
1# Outside loop
2pressed = False
3
4# Inside loop
5if event.type == pygame.KEYDOWN:
6	if event.key == pygame.K_a and not pressed: #K_a can be replaced by any key
7		# Do something
8		pressed = True
9	elif event.key != pygame.K_a:
10    	pressed = False
Naima
12 Feb 2020
1import pygame
2
3keys = pygame.key.get_pressed()
4if keys[pygame.K_LEFT]:
5    x -= 1
6if keys[pygame.K_RIGHT]:
7	x += 1
queries leading to this page
pygame check if key is wkeydown event in pygamepygame key releasepress event for button pygamepygame keydown oncepygame key inputpygame event keyup not workngpygame test if anykey pressedpygame get if a key is pressedhow to enable keyboard in pygamepygame if button is pressedwnter key detection in pygamepygame is key pressedpygame get keys pressedpygame check if a key is pressedget keys pressed pygamehow to know which key is pressed pygamekey pygamepygame test if ctrl presshow to use keys in pygamekey press pygamehow to get keyboard input with pygamehow to check your key in pygamehow to check if a key has been pressed in pygamepygame store key pressed onceup arrow input in pygamedetect when no key is pressed pygamehow to check if a key is pressed pygamepygame test for keyspygame keyspygame get any key presshow to check for a key pressed pygamehow to check in pygame key tilden in pressedpygame get key pressed maintanedpygame when key is pressed do a movementget key press in pygameevent key pygameis key pressed pygamehow to check if the mosue button is pressed in pygamepygame keypressedif w key pressed pygamehow to give input for 5c keyboard in pygame key 3d wasdpygame key only press oncepygame detecting key presses programpygame keyboard inputhow to check if a certain key has been pressed pygamepygame keydownpygame key get pressed 28 29how to check when a key is pressed and released in pygamepygame pressed key for certain timewhile pressed pygameif any key is pressed pygamepygame key press releasedpygame test for ctrl 2bqpygame enter buttopygame keyboard events pressing downhow to test for key press in pygamekey r pygamepygame pressed keyshow to deterct arow key presses pygamepygame how to check if b key is pressedhow to allow only 1 key pressed in pygamecheck key pressed pygamehow to get key pressed pygamecheck if key pressed pygamedetect keyboard pygamehow to see which key is being pressed pygamekey press and click in pygamepygame a key pressedhow to get input from the keyboard in pygameleft control key pygamehow to disable a keys in pygamepygame error 3a posted event keycode must be intpygame key get pressedhow to do key detection in pygamehow to get keypress event for 21 in pygamekey press in pygameget pressed keys pygamecheck if no keys pressed pygamehow to check which key is pressed pygamecheck if z key is pressed pygamecheck if key is being pressed pygamehow to check if key is pressed pygamepygame while key is pressedpygame how to test if a key is clickedpygame keys pressed specific keypython pygame key get pressed 28 29pygame event enter key downwhat is pygame key get pressed 3fpygame pressed key infinitelyhow to know if the key is continuouly pressed in pygamepygame detect command key presshow to display th keys pressed in pygamepygame key leftpygame event keyhow to check if any key is clicked pygamehow to get keys pressed in pygamehow to check for single key press pygamedetect which number key pressed pygamekeys in pygamecheck if key 27w 27 is pressed pygameif key pressed pygamehow to get input from the keyup in pygamepygame not return key pressedpygame key get pressed 28 29 waiting time until worksget pressed 28 29check if a key is pressed not held pygamepygame send arrow key eventpygame key is pressedget keypress pygamehow to detect if w key is pressed in pygamepygame check event type keyhow to detect if a cirtain key was pressed in pygamekeys pygamepygame arrow keyskey presses in python pygameonly do something on press pygamepygame check if key is downpygame checking if key pressedhow to make a thing work while user presses a key in pygamepygame get pressed keys neampygame if key is being pressedpygame y key pressedpygame arrowup arrow key in pyagmehow to get keys from pygame pythonpygame key pressedpygame inputpygame key akey pressed event pygamepygame a key is pressedhow to detect key presses in pygameshow something key press pygamepygame keys pressed numberhow to check key pressed in pygamepygame how to check if a key is pressedpygame how to detect keypress and not key holdhow to chec if a key is pressed in pygame pythonhow to check for keypress in pygamepygame key pressedpygame key pressed oncepygame check if key pressedpygame get keyboard inputpygame enter keyhow to get key pressed in pygamehow to use keys in python pygameget pressed pygamepygame keydown eventget input with pygamepygame key downpygame kew w inputpygame check key pressedpygame detect up key presspygame keypygame check for keypresspygame check button pressedpygame get number of key pressedkeystroke detction in pygamepygame event left keydetecting keypress in pygamehow to use pygame key get pressedpygame get key statehow to detect when a key is pressed in pygameppygmae key codepygame keyup not triggeringpygame detect keypresskey get pressed pygamecheck if key is pressed pygamepygame keyall event k in pygamekeys pressed name pygamehow to check if the space bar is pressed in pygameif key left pressed pygamehow to check if any key is pressed in pygamelisten for key input in pygamepygame event keyholddetect keys pressed pygamecheck if key is pressed 22pygame menu 22event keys pygamecheck for clicked key pygamepygame how to detect keypresspygame on key pressif key is pressed pygamecheck for key presses pygamedetect key press python pygamekeys letter pygamehow to detect key presses pygamehow to check if a key is pressed in pygamepygame keyboard input exampleskey clicked event pygamedetect if a key is holded pygamepygame event keysprint which key was pressed pygamedetect key pygamearow keys in pygamekey enter in pygamepygame key pressed releasedget key pressed pytgamehow to use pygame key set mods 28 29 c2 b6 to returnif event type 3d 3d pygame right arrowpygame detect key uppygame key detectionhow to check which key is pressed in pygamepygame keys pressedcheck if a key was pressed pygamekey pressed pygamehow to record key presses in pygamepygame key presshow to detect how many keys are pressed in pygamepygame button presshow to make pygame detect which keypygame key pressed eventkeys 3d pygame key get pressed 28 29 if keys 5bpygame k a 5d 3apygame evnet keyarow keys pygamepygame function keyskeydown python pressed pygamehow to check if a kay is pressed and hold pygamehow to get input from the key up in pygamepygame find if w key is pressedif space bar pressed pygamepygame keys pressedif key up pressed pygamepygame get key pressed exampleget key pressed to string pygamepygame k arrowhow to detect all keypresses in pygamepygame get key pressedpygame event arrowpygame check if w key is pressedevent type pygamehow to use keydown pygameget key pressed pygamecheck if any key is pressed pygamepygame pressed keyevent pygame keyboardpython pygame how to check which key was pressedhow to check for a key press in pygamedetect key press in pygamehow to know if no keys are pressed in pygamepygame accept button presseshow to detect mouse pressed in pygamepygame detect keyhow to check that no key is pressed in pygamepygame check key aget keys pygamewhat is pygame key get pressed 28 29 3fpygame key pressed keydownkeyboard pygamepygame key press oncehow to know if keypad has been pressed pygamepygame detect key presseddetect when the mosue is pressed in pygamehow to detect keys in pygamepygame key presseshow to get which key was pressed pygamepygame check key pressed letterkeyboard map pygamepygame keydownkeypress pygamepygame check if a particular key is pressedpygame check if key pressed wasdhow to check if left arrow is pressed in pygameget key name from event key pyganeknow key pressed pygamepygame keydown pressedcheck if key is pressed pygame menupygame keys how to see if key is pressed twicepygame check if key is pressedpressed keys pygameget key presses in pygameget keydown event pygamepygame how to know a letter key is pressedkeydown pygamehow to say if a key is pressed in pygamepygame keyboard pressedpygame send key eventpython pygame get key pressedpygame detect key releasecheck if a key is pressed in pygamehow to check if a certain key os pressed pygamepygame detect keyboard inputdetect keypress pygamehow to know if the keyboard is clicked in pygamepygame get key eventhow to check if any key is pressed pygamepygame while key pressed do thispygame keypresspygame keybord press leftpygame get button pressedpygame get hotkeyhow to say if w is pressed in pygamepython pygame key pressdetect when key releases pygamecheck if s is pressed in pygamekey pressed keys pygamepygame keys pressed timepygame chek if pressed mpusepygame handle key pressespygame input from enter keyif key pressed down pygamekey is pressed in pygame event maintainhow to execute event only once pygamepygame key get pressed 28 29 mousekeyboard buttons names in pygamepygame keydown works one at timepygame key get pressedpygame get pressed keyuhow to make an object move when key is pressed pygamekeyboard input pygamepygame event key holdkey key event pyfgame one timecheck if a key is pressed python pygamedetect key presses pygamehow to detect key press in pygamehow to use pygame key get pressed 28 29pygame key event pressedpygame if letter key is pressedhow to use a key in pygamright arrow input pygameup and down key in pygamepygame press oncepygame if key is presseddetect key press pygamecheck if w pressed in pygameget key press pygamepygame get mouse key pressedevent key 13 pygamepython on key down oncepython pygame left key pressedpygame key pressed listpygame key press durationhow to check if key is pressed and hold down in pygamedo event while key is pressed pygamepygame simulate key presspygame check if keypressedpygame if press long timeif event key 3d 3d pygame k leftevent keys pygaamepygame if key pressedpygame keyboardpygame long presspygame event key pressedpygame event key code 119pygame escape keypygame get on key press and key releasepygame while key pressedhow to see if a key is being pressed in pygame keyboard input with pygamecheck for key press in pygamehow to make pygame detect keypresshow to detect key pygamepygame get pressed keyspygame detect key presspygame check if key is pressed continuouslyspecific key pressed pygamehow to check for keypress pygamekey p pressed pygamehow to detect when a key is pressed in pygame