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>From what I understand (correct me if I'm wrong, I may not be quite understanding what you asked), but it looks like what you are trying to do is press a key, keep the key held down, and have pygame continually process KEYDOWN events as you are holding the key down.  In pygame, this does not work, but you <em>can</em> handle holding a key down in a different way.  Just think:  if you start holding a key down, it creates a KEYDOWN event.  When you let go of that key, it generates a KEYUP event.  So therefore, the key is being held down <em>after</em> you push the key down and <em>before</em> you let it go.  The following code explains the concept through example:</p>

<pre><code>import pygame, sys, time
pygame.init()
screen = pygame.display.set_mode([640, 480])
a_pressed = False

while 1:
    time.sleep(.2)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                a_pressed = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                a_pressed = False
    if a_pressed == True:
        print '"A" is currently being pressed down.'
</code></pre>

<p>I haven't been able to test the code (using school computer), but this should print the above message while you are holding the "A" key, at approximately 5 times per second (this is a lazy way of limiting fps).  The same applies not only for all keys, but also dragging with the mouse.</p>

python keyboard pygame