Skip to content

Commit

Permalink
[#18] Use events to handle key strokes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarev committed Feb 20, 2021
1 parent 2647d5c commit b45277b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions zx/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class IsTapePlayerPaused(DeviceEvent):
pass


class KeyStroke(object):
def __init__(self, id, pressed):
self.id = id
self.pressed = pressed


class PauseStateUpdated(DeviceEvent):
pass

Expand Down
8 changes: 2 additions & 6 deletions zx/_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ._device import GetEmulationTime
from ._device import GetTapePlayerTime
from ._device import IsTapePlayerPaused
from ._device import KeyStroke
from ._device import PauseStateUpdated
from ._device import QuantumRun
from ._device import ScreenUpdated
Expand All @@ -25,7 +26,6 @@
from ._error import USER_ERRORS
from ._error import verbalize_error
from ._except import EmulationExit
from ._keyboard import KEYS
from ._time import get_elapsed_time
from ._time import get_timestamp
from ._utils import div_ceil
Expand Down Expand Up @@ -380,11 +380,7 @@ def __on_key(self, event, devices):
self._KEY_HANDLERS[event.id](devices)

zx_key_id = self._GTK_KEYS_TO_ZX_KEYS.get(event.id, event.id)
key = KEYS.get(zx_key_id, None)
if key:
self.xmachine.paused = False
self.xmachine._quit_playback_mode()
self.xmachine._handle_key_stroke(key, event.pressed)
devices.notify(KeyStroke(zx_key_id, event.pressed))

def __on_gdk_click(self, widget, event):
TYPES = {
Expand Down
8 changes: 8 additions & 0 deletions zx/_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
from ._device import GetEmulationTime
from ._device import GetTapePlayerTime
from ._device import IsTapePlayerPaused
from ._device import KeyStroke
from ._device import PauseStateUpdated
from ._device import ToggleEmulationPause
from ._device import ToggleTapePause
from ._emulatorbase import _Spectrum48Base
from ._except import EmulationExit
from ._keyboard import KEYS
from ._rom import get_rom_image
from ._utils import make16

Expand Down Expand Up @@ -429,6 +431,12 @@ def on_event(self, event, devices, result):
return self._tape_player.get_time()
elif isinstance(event, IsTapePlayerPaused):
return self._is_tape_paused()
elif isinstance(event, KeyStroke):
key = KEYS.get(event.id, None)
if key:
self.paused = False
self._quit_playback_mode()
self._handle_key_stroke(key, event.pressed)
elif isinstance(event, ToggleEmulationPause):
self.paused ^= True
elif isinstance(event, ToggleTapePause):
Expand Down

0 comments on commit b45277b

Please sign in to comment.