dirtywords

portable text interface framework  https://pypi.python.org/pypi/dirtywords
git clone https://git.ce9e.org/dirtywords.git

commit
84d086f423c2aac2b7e46737412b5ca631db72fb
parent
b67025338a4806b8a28c3a8c7c884e08bcf04f1a
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-09 07:27
map pygame keys to our own

Diffstat

M dirtywords/pygame_core.py 24 +++++++++++++++++++++---

1 files changed, 21 insertions, 3 deletions


diff --git a/dirtywords/pygame_core.py b/dirtywords/pygame_core.py

@@ -1,7 +1,10 @@
   -1     1 import string
   -1     2 
    1     3 import pygame
    2     4 from pygame.locals import KEYDOWN, KEYUP
    3     5 
    4     6 import base
   -1     7 from constants import KEYS
    5     8 
    6     9 
    7    10 class Screen(base.Screen):
@@ -17,24 +20,39 @@ class Screen(base.Screen):
   17    20         self.pygame_screen = pygame.display.set_mode(
   18    21             (self.fontwidth * width, self.fontheight * height))
   19    22 
   -1    23     def _convert_ch(self, ch):
   -1    24         # key constants
   -1    25         for key, value in KEYS.iteritems():
   -1    26             if ch == getattr(pygame, 'K_' + key.upper()):
   -1    27                 return value
   -1    28 
   -1    29         if 0 <= ch < 256:
   -1    30             if chr(ch) in string.letters:
   -1    31                 # capital letters on shift
   -1    32                 if pygame.key.get_mods() & pygame.KMOD_SHIFT:
   -1    33                     ch ^= 32
   -1    34             return ch
   -1    35 
   20    36     def getch(self, blocking=True):
   21    37         while blocking or pygame.event.peek(KEYDOWN):
   22    38             event = pygame.event.wait()
   23    39             if event.type == KEYDOWN:
   24    -1                 return event.key
   -1    40                 ch = self._convert_ch(event.key)
   -1    41                 if ch is not None:
   -1    42                     return ch
   25    43 
   26    44     def get_key_events(self):
   27    45         for event in pygame.event.get():
   28    46             if event.type == KEYDOWN:
   29    47                 yield {
   30    48                     'type': 'keydown',
   31    -1                     'key': event.key,
   -1    49                     'key': self._convert_ch(event.key),
   32    50                     'modifier': event.mod,
   33    51                 }
   34    52             elif event.type == KEYUP:
   35    53                 yield {
   36    54                     'type': 'keyup',
   37    -1                     'key': event.key,
   -1    55                     'key': self._convert_ch(event.key),
   38    56                     'modifier': event.mod,
   39    57                 }
   40    58