Skip to content

Commit 99ed515

Browse files
4B796C65@gmail.com4B796C65@gmail.com
authored andcommitted
KeyEvent.keyname is now KeyEvent.key and MouseButtonEvent.button now behaves like KeyEvent.keyname. Docs are included for both.
1 parent 17d29a8 commit 99ed515

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

tdl/event.py

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
"""
22
This module handles user input.
3-
4-
Here's a quick reference to Event types and their attributes:
5-
- QUIT
6-
- KEYDOWN: keyname key char alt ctrl shift lalt lctrl ralt rctrl
7-
- KEYUP: keyname key char alt ctrl shift lalt lctrl ralt rctrl
8-
- MOUSEDOWN: button pos cell
9-
- MOUSEUP: button pos cell
10-
- MOUSEMOTION: pos cell motion cellmotion
3+
4+
String constants can be found in the variable details of L{KeyEvent.key},
5+
L{MouseButtonEvent.button}, and L{Event.type}.
116
127
You will likely want to use the L{event.get} function or L{event.App}
13-
class but you can still use L{keyWait} and L{isWindowClosed} to control
14-
your entire program.
8+
class but you can still use L{event.keyWait} and L{event.isWindowClosed}
9+
to control your entire program.
1510
"""
1611

1712
import time
@@ -28,19 +23,20 @@ class but you can still use L{keyWait} and L{isWindowClosed} to control
2823
_mouser = 0
2924

3025
# this interpets the constants from libtcod and makes a key -> keyname dictionary
31-
def _parse_keynames(module):
32-
"""returns a dictionary mapping of human readable key names to their keycodes
26+
def _parseKeyNames(module):
27+
"""
28+
returns a dictionary mapping of human readable key names to their keycodes
3329
this parses constants with the names of K_* and makes code=name pairs
3430
this is for KeyEvent.keyname variable and that enables things like:
3531
if (event.keyname == 'PAGEUP'):
3632
"""
37-
_keynames = {}
33+
_keyNames = {}
3834
for attr in dir(module): # from the modules variables
3935
if attr[:2] == 'K_': # get the K_* constants
40-
_keynames[getattr(_tcod, attr)] = attr[2:] # and make CODE=NAME pairs
41-
return _keynames
36+
_keyNames[getattr(_tcod, attr)] = attr[2:] # and make CODE=NAME pairs
37+
return _keyNames
4238

43-
_keynames = _parse_keynames(_tcod)
39+
_keyNames = _parseKeyNames(_tcod)
4440

4541
class Event(object):
4642
__slots__ = ()
@@ -51,20 +47,17 @@ class Event(object):
5147
"""
5248

5349
def __repr__(self):
54-
"""List an events public attributes in print calls
50+
"""List an events public attributes in print calls.
5551
"""
5652
attrdict = {}
5753
for varname in dir(self):
5854
if '_' == varname[0]:
5955
continue
6056
attrdict[varname] = self.__getattribute__(varname)
6157
return '%s Event %s' % (self.__class__.__name__, repr(attrdict))
62-
63-
#def __tuple__(self):
64-
# return tuple((getattr(self, attr) for attr in self.__slots__))
6558

6659
class Quit(Event):
67-
"""For when the window is closed by the user.
60+
"""Fired when the window is closed by the user.
6861
"""
6962
__slots__ = ()
7063
type = 'QUIT'
@@ -74,62 +67,78 @@ class KeyEvent(Event):
7467
'leftAlt', 'leftCtrl', 'rightAlt', 'rightCtrl')
7568

7669
def __init__(self, key, char, lalt, lctrl, ralt, rctrl, shift):
77-
self.key = key
78-
"""Look up the use of L{keyname} instead."""
79-
self.keyname = _keynames[key]
80-
"""A human readable version of key
70+
self.key = _keyNames[key]
71+
"""Human readable name of the key pressed.
8172
82-
Can be one of 'NONE', 'ESCAPE', 'BACKSPACE', 'TAB', 'ENTER', 'SHIFT', 'CONTROL', 'ALT', 'PAUSE', 'CAPSLOCK', 'PAGEUP', 'PAGEDOWN', 'END', 'HOME', 'UP', 'LEFT', 'RIGHT', 'DOWN', 'PRINTSCREEN', 'INSERT', 'DELETE', 'LWIN', 'RWIN', 'APPS', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'KP0', 'KP1', 'KP2', 'KP3', 'KP4', 'KP5', 'KP6', 'KP7', 'KP8', 'KP9', 'KPADD', 'KPSUB', 'KPDIV', 'KPMUL', 'KPDEC', 'KPENTER', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'NUMLOCK', 'SCROLLLOCK', 'SPACE', 'CHAR'"""
73+
Can be one of
74+
'NONE', 'ESCAPE', 'BACKSPACE', 'TAB', 'ENTER', 'SHIFT', 'CONTROL',
75+
'ALT', 'PAUSE', 'CAPSLOCK', 'PAGEUP', 'PAGEDOWN', 'END', 'HOME', 'UP',
76+
'LEFT', 'RIGHT', 'DOWN', 'PRINTSCREEN', 'INSERT', 'DELETE', 'LWIN',
77+
'RWIN', 'APPS', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
78+
'KP0', 'KP1', 'KP2', 'KP3', 'KP4', 'KP5', 'KP6', 'KP7', 'KP8', 'KP9',
79+
'KPADD', 'KPSUB', 'KPDIV', 'KPMUL', 'KPDEC', 'KPENTER', 'F1', 'F2',
80+
'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
81+
'NUMLOCK', 'SCROLLLOCK', 'SPACE', 'CHAR'
82+
@type: string"""
8383
char = char if isinstance(char, str) else char.decode()
8484
self.char = char.replace('\x00', '') # change null to empty string
8585
"""A single character string of the letter or symbol pressed.
86-
Special characters like delete and return are not cross platform."""
86+
Special characters like delete and return are not cross platform.
87+
@type: string"""
8788
self.leftAlt = bool(lalt)
8889
self.rightAlt = bool(ralt)
8990
self.leftCtrl = bool(lctrl)
9091
self.rightCtrl = bool(rctrl)
9192
self.shift = bool(shift)
92-
"True if shift was held down during this event."
93+
"""True if shift was held down during this event.
94+
@type: boolean"""
9395
self.alt = bool(lalt or ralt)
94-
"True if alt was held down during this event."
96+
"""True if alt was held down during this event.
97+
@type: boolean"""
9598
self.control = bool(lctrl or rctrl)
96-
"True if control was held down during this event."
99+
"""True if control was held down during this event.
100+
@type: boolean"""
97101

98102
class KeyDown(KeyEvent):
99-
"""For when the user presses a key on the keyboard or a key repeats.
103+
"""Fired when the user presses a key on the keyboard or a key repeats.
100104
"""
101105
__slots__ = ()
102106
type = 'KEYDOWN'
103107

104108
class KeyUp(KeyEvent):
105-
"""For when the user releases a key on the keyboard.
109+
"""Fired when the user releases a key on the keyboard.
106110
"""
107111
__slots__ = ()
108112
type = 'KEYUP'
109113

114+
_mouseNames = {1: 'LEFT', 2: 'MIDDLE', 3: 'RIGHT', 4: 'SCROLLUP', 5: 'SCROLLDOWN'}
110115
class MouseButtonEvent(Event):
111116
__slots__ = ('button', 'pos', 'cell')
112117

113118
def __init__(self, button, pos, cell):
114-
self.button = button
115-
"1=left, 2=middle, 3=right, 4=scrollUp, 5=scrollDown"
119+
self.button = _mouseNames[button]
120+
"""Can be one of
121+
'LEFT', 'MIDDLE', 'RIGHT', 'SCROLLUP', 'SCROLLDOWN'
122+
@type: string"""
116123
self.pos = pos
117-
"(x, y) position of the mouse on the screen"
124+
"""(x, y) position of the mouse on the screen
125+
@type: (int, int)"""
118126
self.cell = cell
119-
"(x, y) position of the mouse snapped to a cell on the root console"
127+
"""(x, y) position of the mouse snapped to a cell on the root console
128+
@type: (int, int)"""
120129

121130
class MouseDown(MouseButtonEvent):
122-
"""For when a button is pressed."""
131+
"""Fired when a button is pressed."""
123132
__slots__ = ()
124133
type = 'MOUSEDOWN'
125134

126135
class MouseUp(MouseButtonEvent):
127-
"""For when a button is released."""
136+
"""Fired when a button is released."""
128137
__slots__ = ()
129138
type = 'MOUSEUP'
130139

131140
class MouseMotion(Event):
132-
"""For when the mouse is moved."""
141+
"""Fired when the mouse is moved."""
133142
__slots__ = ('pos', 'motion', 'cell', 'cellmotion')
134143
type = 'MOUSEMOTION'
135144

0 commit comments

Comments
 (0)