1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-14 06:12:31 +03:00

hotkey/darwin: No need to sys.platform gate within this

And it gets rid of a `pre-commit run --all-files mypy` error.
This commit is contained in:
Athanasius 2022-12-22 13:43:51 +00:00
parent 4041890f39
commit 872ab1b814
No known key found for this signature in database
GPG Key ID: 772697E181BB2767

View File

@ -153,117 +153,116 @@ class MacHotkeyMgr(AbstractHotkeyMgr):
self.keycode = 0 self.keycode = 0
self.modifiers = 0 self.modifiers = 0
if sys.platform == 'darwin': # noqa: C901 @objc.callbackFor(NSEvent.addGlobalMonitorForEventsMatchingMask_handler_)
@objc.callbackFor(NSEvent.addGlobalMonitorForEventsMatchingMask_handler_) def _handler(self, event) -> None:
def _handler(self, event) -> None: # use event.charactersIgnoringModifiers to handle composing characters like Alt-e
# use event.charactersIgnoringModifiers to handle composing characters like Alt-e if (
if ( (event.modifierFlags() & self.MODIFIERMASK) == self.modifiers
(event.modifierFlags() & self.MODIFIERMASK) == self.modifiers and ord(event.charactersIgnoringModifiers()[0]) == self.keycode
and ord(event.charactersIgnoringModifiers()[0]) == self.keycode ):
): if config.get_int('hotkey_always'):
if config.get_int('hotkey_always'): self.activated = True
else: # Only trigger if game client is front process
front = NSWorkspace.sharedWorkspace().frontmostApplication()
if front and front.bundleIdentifier() == 'uk.co.frontier.EliteDangerous':
self.activated = True self.activated = True
else: # Only trigger if game client is front process def acquire_start(self) -> None:
front = NSWorkspace.sharedWorkspace().frontmostApplication() """Start acquiring hotkey state via polling."""
if front and front.bundleIdentifier() == 'uk.co.frontier.EliteDangerous': self.acquire_state = MacHotkeyMgr.ACQUIRE_ACTIVE
self.activated = True self.root.after_idle(self._acquire_poll)
def acquire_start(self) -> None: def acquire_stop(self) -> None:
"""Start acquiring hotkey state via polling.""" """Stop acquiring hotkey state."""
self.acquire_state = MacHotkeyMgr.ACQUIRE_ACTIVE self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE
self.root.after_idle(self._acquire_poll)
def acquire_stop(self) -> None: def _acquire_poll(self) -> None:
"""Stop acquiring hotkey state.""" """Perform a poll of current hotkey state."""
self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE if config.shutting_down:
return
def _acquire_poll(self) -> None: # No way of signalling to Tkinter from within the monkey-patched event handler that doesn't
"""Perform a poll of current hotkey state.""" # cause Python to crash, so poll.
if config.shutting_down: if self.acquire_state:
return if self.acquire_state == MacHotkeyMgr.ACQUIRE_NEW:
# Abuse tkEvent's keycode field to hold our acquired key & modifier
self.root.event_generate('<KeyPress>', keycode=self.acquire_key)
self.acquire_state = MacHotkeyMgr.ACQUIRE_ACTIVE
self.root.after(50, self._acquire_poll)
# No way of signalling to Tkinter from within the monkey-patched event handler that doesn't def fromevent(self, event) -> Optional[Union[bool, Tuple]]:
# cause Python to crash, so poll. """
if self.acquire_state: Return configuration (keycode, modifiers) or None=clear or False=retain previous.
if self.acquire_state == MacHotkeyMgr.ACQUIRE_NEW:
# Abuse tkEvent's keycode field to hold our acquired key & modifier
self.root.event_generate('<KeyPress>', keycode=self.acquire_key)
self.acquire_state = MacHotkeyMgr.ACQUIRE_ACTIVE
self.root.after(50, self._acquire_poll)
def fromevent(self, event) -> Optional[Union[bool, Tuple]]: :param event: tk event ?
""" :return: False to retain previous, None to not use, else (keycode, modifiers)
Return configuration (keycode, modifiers) or None=clear or False=retain previous. """
(keycode, modifiers) = (event.keycode & 0xffff, event.keycode & 0xffff0000) # Set by _acquire_poll()
if (
keycode
and not (modifiers & (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask))
):
if keycode == 0x1b: # Esc = retain previous
self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE
return False
:param event: tk event ? # BkSp, Del, Clear = clear hotkey
:return: False to retain previous, None to not use, else (keycode, modifiers) elif keycode in [0x7f, ord(NSDeleteFunctionKey), ord(NSClearLineFunctionKey)]:
""" self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE
(keycode, modifiers) = (event.keycode & 0xffff, event.keycode & 0xffff0000) # Set by _acquire_poll() return None
if (
keycode
and not (modifiers & (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask))
):
if keycode == 0x1b: # Esc = retain previous
self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE
return False
# BkSp, Del, Clear = clear hotkey # don't allow keys needed for typing in System Map
elif keycode in [0x7f, ord(NSDeleteFunctionKey), ord(NSClearLineFunctionKey)]: elif keycode in [0x13, 0x20, 0x2d] or 0x61 <= keycode <= 0x7a:
self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE NSBeep()
return None self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE
return None
# don't allow keys needed for typing in System Map return (keycode, modifiers)
elif keycode in [0x13, 0x20, 0x2d] or 0x61 <= keycode <= 0x7a:
NSBeep()
self.acquire_state = MacHotkeyMgr.ACQUIRE_INACTIVE
return None
return (keycode, modifiers) def display(self, keycode, modifiers) -> str:
"""
Return displayable form of given hotkey + modifiers.
def display(self, keycode, modifiers) -> str: :param keycode:
""" :param modifiers:
Return displayable form of given hotkey + modifiers. :return: string form
"""
text = ''
if modifiers & NSControlKeyMask:
text += u''
:param keycode: if modifiers & NSAlternateKeyMask:
:param modifiers: text += u''
:return: string form
"""
text = ''
if modifiers & NSControlKeyMask:
text += u''
if modifiers & NSAlternateKeyMask: if modifiers & NSShiftKeyMask:
text += u'' text += u''
if modifiers & NSShiftKeyMask: if modifiers & NSCommandKeyMask:
text += u'' text += u''
if modifiers & NSCommandKeyMask: if (modifiers & NSNumericPadKeyMask) and keycode <= 0x7f:
text += u'' text += u''
if (modifiers & NSNumericPadKeyMask) and keycode <= 0x7f: if not keycode:
text += u'' pass
if not keycode: elif ord(NSF1FunctionKey) <= keycode <= ord(NSF35FunctionKey):
pass text += f'F{keycode + 1 - ord(NSF1FunctionKey)}'
elif ord(NSF1FunctionKey) <= keycode <= ord(NSF35FunctionKey): elif keycode in MacHotkeyMgr.DISPLAY: # specials
text += f'F{keycode + 1 - ord(NSF1FunctionKey)}' text += MacHotkeyMgr.DISPLAY[keycode]
elif keycode in MacHotkeyMgr.DISPLAY: # specials elif keycode < 0x20: # control keys
text += MacHotkeyMgr.DISPLAY[keycode] text += chr(keycode + 0x40)
elif keycode < 0x20: # control keys elif keycode < 0xf700: # key char
text += chr(keycode + 0x40) text += chr(keycode).upper()
elif keycode < 0xf700: # key char else:
text += chr(keycode).upper() text += u''
else: return text
text += u''
return text
def play_good(self): def play_good(self):
"""Play the 'good' sound.""" """Play the 'good' sound."""