From 22fb56fa9ad73033fc313f148e47c40a49285d1c Mon Sep 17 00:00:00 2001 From: Ivan Kosarev Date: Sat, 20 Mar 2021 23:47:12 +0200 Subject: [PATCH] [#22] Handle key strokes via events. --- zx/_emulator.py | 6 ++++-- zx/_keyboard.py | 11 ++++++++++- zx/_machine.py | 1 - 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/zx/_emulator.py b/zx/_emulator.py index 8e22e75..d91df05 100644 --- a/zx/_emulator.py +++ b/zx/_emulator.py @@ -9,6 +9,7 @@ # # Published under the MIT license. +# TODO: Remove unused imports. import time from ._data import MachineSnapshot from ._data import SoundFile @@ -69,8 +70,10 @@ def __init__(self, speed_factor=1.0, profile=None, devices=None): self.__events_to_signal = RunEvents.NO_EVENTS + self.__keyboard_state = Keyboard() + if devices is None: - devices = [self, TapePlayer()] + devices = [self, TapePlayer(), self.__keyboard_state] # Don't even create the window on full throttle. if self.__speed_factor is not None: @@ -80,7 +83,6 @@ def __init__(self, speed_factor=1.0, profile=None, devices=None): self.devices = devices - self.__keyboard_state = Keyboard() self.set_on_input_callback(self.__on_input) self.__playback_player = None diff --git a/zx/_keyboard.py b/zx/_keyboard.py index 55e4ba5..667329f 100644 --- a/zx/_keyboard.py +++ b/zx/_keyboard.py @@ -8,6 +8,8 @@ # # Published under the MIT license. +from ._device import Device +from ._device import KeyStroke from ._utils import tupilize @@ -44,7 +46,7 @@ def __init__(self, id, index): KEYS[i] = info -class Keyboard(object): +class Keyboard(Device): _state = [0xff] * 8 def read_port(self, addr): @@ -79,3 +81,10 @@ def handle_key_stroke(self, key_info, pressed): self._state[addr_line - 8] &= mask ^ 0xff else: self._state[addr_line - 8] |= mask + + def on_event(self, event, devices, result): + if isinstance(event, KeyStroke): + key = KEYS.get(event.id, None) + if key: + self.handle_key_stroke(key, event.pressed) + return result diff --git a/zx/_machine.py b/zx/_machine.py index 61f89d9..54a19f3 100644 --- a/zx/_machine.py +++ b/zx/_machine.py @@ -460,7 +460,6 @@ def on_event(self, event, devices, result): if key: self.paused = False self._quit_playback_mode() - self._handle_key_stroke(key, event.pressed) elif isinstance(event, LoadFile): self._load_file(event.filename) elif isinstance(event, SaveSnapshot):