April 2024 - This site, and Kamaelia are being updated. There is significant work needed, and PRs are welcome.

Kamaelia.UI.Pygame.KeyEvent

Pygame keypress event handler

A component that registers with a Pygame Display service component to receive key-up and key-down events from Pygame. You can set up this component to send out different messages from different outboxes depending on what key is pressed.

Example Usage

Capture keypresses in pygame for numbers 1,2,3 and letters a,b,c:

fom pygame.locals import *

Graphline( output = ConsoleEchoer(),
           keys = KeyEvent( key_events={ K_1 : (1,"numbers"),
                                         K_2 : (2,"numbers"),
                                         K_3 : (3,"numbers"),
                                         K_a : ("A", "letters"),
                                         K_b : ("B", "letters"),
                                         K_c : ("C", "letters"),
                                       },
                            outboxes={ "numbers" : "numbers between 1 and 3",
                                       "letters" : "letters between A and C",
                                     }
                          ),
           linkages = { ("keys","numbers"):("output","inbox"),
                        ("keys","letters"):("output","inbox")
                      }
         ).run()

The symbols K_1, K_2, etc are keycodes defined in defined in pygame.locals.

How does it work?

This component requests a zero sized display surface from the Pygame Display service component and registers to receive events from pygame.

Whenever a KEYDOWN event is received, the pygame keycode is looked up in the mapping you specified. If it is there, then the specified message is sent out of the specified outbox.

In addition, if the allKeys flag was set to True during initialisation, then any KEYDOWN or KEYUP event will result in a ("DOWN",keycode) or ("UP",keycode) message being sent to the "allkeys" outbox.

If you have specified a message to send for a particular key, then both that message and the 'all-keys' message will be sent when the KEYDOWN event occurs.

If this component receives a shutdownMicroprocess or producerFinished message on its "control" inbox, then this will be forwarded out of its "signal" outbox and the component will then terminate.


Kamaelia.UI.Pygame.KeyEvent.KeyEvent

class KeyEvent(Axon.Component.component)

KeyEvent([allkeys][,key_events][,outboxes]) -> new KeyEvent component.

Component that sends out messages in response to pygame keypress events.

Keyword arguments:

  • allkeys -- if True, all keystrokes send messages out of "allkeys" outbox (default=False)
  • key_events -- dict mapping pygame keycodes to (msg,"outboxname") pairs (default=None)
  • outboxes -- dict of "outboxname":"description" key:value pairs (default={})

Inboxes

  • control : Shutdown messages: shutdownMicroprocess or producerFinished
  • callback : Receive callbacks from Pygame Display
  • inbox : Receive events from Pygame Display

Outboxes

  • outbox : NOT USED
  • signal : Shutdown signalling: shutdownMicroprocess or producerFinished
  • allkeys : Outbox that receives *every* keystroke if enabled
  • display_signal : Outbox used for communicating to the display surface

Methods defined here

Warning!

You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers.

__init__(self[, allkeys][, key_events][, key_up_events][, outboxes])

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

main(self)

Main loop.

waitBox(self, boxname)

Generator. yields 1 until data is ready on the named inbox.

Feedback

Got a problem with the documentation? Something unclear that could be clearer? Want to help improve it? Constructive criticism is very welcome - especially if you can suggest a better rewording!

Please leave you feedback here in reply to the documentation thread in the Kamaelia blog.

-- Automatic documentation generator, 05 Jun 2009 at 03:01:38 UTC/GMT