Event dispatching, super simply

This commit is contained in:
Josh Klar
2018-09-03 04:22:52 -07:00
parent c641903d61
commit d9b909d841
6 changed files with 102 additions and 18 deletions

View File

@@ -1,3 +1,21 @@
KEY_UP_EVENT = 'KEY_UP'
KEY_DOWN_EVENT = 'KEY_DOWN'
def key_up_event(keycode):
return {
'type': KEY_UP_EVENT,
'keycode': keycode,
}
def key_down_event(keycode):
return {
'type': KEY_DOWN_EVENT,
'keycode': keycode,
}
class Keymap:
def __init__(self, map):
self.map = map
@@ -6,13 +24,13 @@ class Keymap:
for row in self.map
]
def parse(self, matrix):
def parse(self, matrix, store):
for ridx, row in enumerate(matrix):
for cidx, col in enumerate(row):
if col != self.state[ridx][cidx]:
yield '{}: {}'.format(
'KEYDOWN' if col else 'KEYUP',
self.map[ridx][cidx],
)
if col:
store.dispatch(key_down_event(self.map[ridx][cidx]))
else:
store.dispatch(key_up_event(self.map[ridx][cidx]))
self.state = matrix