How to add keypress events in event queue for pygame
<p>I have a game made with pygame which runs perfectly okay. I want to create a system which reads keys to press from a file (which will contain codes of keys to press in separate lines) and adds them to the pygame event queue so that the player agent moves on its own without the keys actually being pressed. </p>

<p>After reading the pygame docs I have tried to create a new event object and add it to the queue but the event constructor requires attributes which I could not find anywhere.</p>

<p>Does anyone know which attributes are needed to create an instance of an event or if there is another better approach for what I am trying to do? </p>

<p><strong>UPDATE:</strong> I have successfully added my events to the queue however they do not seem to work even though they are completely identical to the ones created automatically. I am printing the event queue below. Highlighted events are added when I actually press the 'a' key. As you can see my events (the ones above) do not trigger the rest of the events as they should.
<a href="http://i.stack.imgur.com/zpmvq.png" rel="nofollow"><img src="http://i.stack.imgur.com/zpmvq.png" alt=""></a></p>

<p><strong>UPDATE 2:</strong> I added some print statements to the event handling code for the keydown events. These are only executed if I actually press the key on the keyboard and just seem to ignore the events I raise from the code.<br>
       <code>input_list = [pg.K_RETURN, pg.K_a, pg.K_s]
        if self.cursor.state == c.PLAYER1:
            self.cursor.rect.y = 358
            if keys[pg.K_DOWN]:
                print("down")
                self.cursor.state = c.PLAYER2
            for input in input_list:
                if keys[input]:
                    print("button")
                    self.reset_game_info()
                    self.done = True
        elif self.cursor.state == c.PLAYER2:
            self.cursor.rect.y = 403
            if keys[pg.K_UP]:
                print("up")
                self.cursor.state = c.PLAYER1</code>  </p>

<p>I am creating my events like so:<br>
<code>pg.event.post(pg.event.Event(pg.KEYDOWN, {'mod': 0, 'scancode': 30, 'key': pg.K_a, 'unicode': 'a'}))</code><br>
I found these values by printing the event which happens when I actually press the key 'a'. </p>

<p>The <a href="http://www.pygame.org/docs/ref/event.html#pygame.event.Event" rel="nofollow">Event object's</a> first argument is its type, which is an integer between <code>pygame.USEREVENT</code> and <code>pygame.NUMEVENTS</code> (24 to 32 exclusive). This is used to identify your event from other events, such as <code>pygame.KEYDOWN</code>, <code>pygame.MOUSEBUTTONDOWN</code> etc.</p>

<p>The second argument is either a dictionary or keyword arguments. These will be your event's attributes. The dict should contain strings as keys.</p>

<p>Here's a short example demonstrating how it's used:</p>

<pre><code>import pygame
pygame.init()

event1 = pygame.event.Event(pygame.USEREVENT, {"greeted": False, "jumped": 10, "ID": 1})
event2 = pygame.event.Event(pygame.USEREVENT, greeted=True, jumped=200, ID=2)

pygame.event.post(event1)
pygame.event.post(event2)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        raise SystemExit
    elif event.type == pygame.USEREVENT:
        print("Player:", event.ID, "| Greeted:", event.greeted, "| Jumped:", event.jumped)
    elif event.type == pygame.USEREVENT + 1:
        print("Player:", event.ID, "| Greeted:", event.greeted, "| Jumped:", event.jumped)
</code></pre>

<p>This will output:</p>

<pre><code>Player: 1 | Greeted: False | Jumped: 10
Player: 2 | Greeted: True | Jumped: 200
</code></pre>

python pygame keyboard-events