Pygame pressing key to move a Rect
<p>I am trying to keep pressing a key and move the square automatically. I've tried to change <code>pygame.key.get.pressed()</code> to <code>pygame.key.get.focused()</code>, but still nothing.</p>

<pre><code>import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
x_speed = 0
y_speed = 0
cordX = 10
cordY = 100


def desenha():
    screen.fill((0, 0, 0))
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
    pygame.display.flip();    


while JogoAtivo:
    for evento in pygame.event.get():
        print(evento)
    #verifica se o evento que veio eh para fechar a janela
        pressed_keys = pygame.key.get_pressed()
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if pressed_keys[pygame.K_SPACE]:
               print('GAME BEGIN')
               GAME_BEGIN = True
               desenha();        
        if pressed_keys[pygame.K_LEFT] and GAME_BEGIN:   
                   speedX=-3
                   cordX+=speedX
                   desenha()
        if pressed_keys[pygame.K_RIGHT] and GAME_BEGIN:
                   speedX=3
                   cordX+=speedX
                   desenha()
</code></pre>

<p>UPDATED THE CODE, BUT STILL THE SAME PROBLEMS (included KEYDOWN event).</p>

<pre><code>import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
x_speed = 0
y_speed = 0
cordX = 10
cordY = 100


def desenha():
    screen.fill((0, 0, 0))
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
    pygame.display.flip();



while JogoAtivo:
    for evento in pygame.event.get():
        print(evento)
    #verifica se o evento que veio eh para fechar a janela
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_SPACE:
                   print('GAME BEGIN')
                   GAME_BEGIN = True
                   desenha();        
        if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_LEFT:
                   speedX=-3
                   cordX+=speedX
                   desenha()
        if evento.type == pygame.KEYDOWN:
             if evento.key == pygame.K_RIGHT:
                   speedX=3
                   cordX+=speedX
                   desenha()
</code></pre>

<p>You can't use <code>get_pressed()</code> in <code>for evento</code> loop because when you keep pressed key then key doesn't generate events and <code>pygame.event.get()</code> returns empty list so <code>for</code> does nothing.</p>

<p>When you start pressing key then system generates single even <code>KEYDOWN</code>, when you stop pressing key then system generates single even <code>KEYUP</code> but system doesn't generate <code>KEYDOW</code> events between this two moments (when you keep pressed key).</p>

<p>You have to use <code>get_pressed()</code> (and other code) after <code>for</code> loop.</p>

<pre><code>for evento in pygame.event.get():
    print(evento)
    if evento.type == pygame.QUIT:
          JogoAtivo = False
          pygame.quit();

# after for loop

pressed_keys = pygame.key.get_pressed()

if pressed_keys[pygame.K_SPACE]:
           print('GAME BEGIN')
           GAME_BEGIN = True
           desenha();        
if pressed_keys[pygame.K_LEFT] and GAME_BEGIN:   
               speedX=-3
               cordX+=speedX
               desenha()
if pressed_keys[pygame.K_RIGHT] and GAME_BEGIN:
               speedX=3
               cordX+=speedX
               desenha()
</code></pre>

<p>or (more or less)</p>

<pre><code>for evento in pygame.event.get():
    print(evento)
    #verifica se o evento que veio eh para fechar a janela
    if evento.type == pygame.QUIT:
           JogoAtivo = False
           pygame.quit();
    elif evento.type == pygame.KEYDOWN:
        if evento.key == pygame.K_SPACE:
               print('GAME BEGIN')
               GAME_BEGIN = True
        elif evento.key == pygame.K_LEFT:
               speedX = -3
        elif evento.key == pygame.K_RIGHT:
               speedX = 3
    elif evento.type == pygame.KEYUP:
        if evento.key in (pygame.K_LEFT, pygame.K_RIGHT):
               speedX = 0


# after loop

if GAME_BEGIN:
    cordX += speedX                   
    desenha()
</code></pre>

python keyboard pygame