-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DavidCEllis/py39_markdown_hotkeys
Update to Python 3.10 - Add support for hotkeys - Fix issue with updated Bleach
- Loading branch information
Showing
18 changed files
with
695 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .keyboard_fixer import Hotkey, read_hotkey, hotkey_or_none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
The keyboard library does not handle numpad hotkeys correctly. | ||
This is a rewrite of the relevant functions to handle this. | ||
""" | ||
|
||
import queue as _queue | ||
import json | ||
|
||
import attr | ||
import keyboard | ||
|
||
|
||
KEY_DOWN = "down" | ||
KEY_UP = "up" | ||
|
||
|
||
# Hotkey = namedtuple("Hotkey", "scancodes, name") | ||
@attr.s | ||
class Hotkey: | ||
scancodes = attr.ib(default=None) | ||
name = attr.ib(default=None) | ||
|
||
def to_json(self): | ||
as_dict = attr.asdict(self) | ||
return json.dumps(as_dict) | ||
|
||
|
||
def hotkey_or_none(keydict): | ||
return Hotkey(**keydict) if keydict else None | ||
|
||
|
||
def read_hotkey(suppress=True): | ||
""" | ||
Modified read_hotkey function to correctly support numpad keys. | ||
The original function returns just the names, this returns a ([scancodes], name) tuple. | ||
The scancodes can then be stored while the name can be displayed. | ||
""" | ||
|
||
queue = _queue.Queue() | ||
|
||
# Replace lambda with | ||
def hook_func(event): | ||
queue.put(event) | ||
return event.event_type == KEY_DOWN | ||
|
||
hooked = keyboard.hook(hook_func, suppress=suppress) | ||
while True: | ||
keychange = queue.get() | ||
if keychange.event_type == KEY_UP: | ||
keyboard.unhook(hooked) | ||
with keyboard._pressed_events_lock: | ||
key_events = list(keyboard._pressed_events.values()) | ||
key_events.append(keychange) | ||
key_codes = [e.scan_code for e in key_events] | ||
key_string = keyboard.get_hotkey_name([e.name for e in key_events]) | ||
return Hotkey(key_codes, key_string) |
Oops, something went wrong.