mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Populate character on simulated key presses (#960)
* Populate character on simulated key presses * Undo example
This commit is contained in:
@@ -6,8 +6,8 @@ import io
|
|||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
|
import unicodedata
|
||||||
import warnings
|
import warnings
|
||||||
from collections import defaultdict
|
|
||||||
from contextlib import redirect_stderr, redirect_stdout
|
from contextlib import redirect_stderr, redirect_stdout
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path, PurePath
|
from pathlib import Path, PurePath
|
||||||
@@ -32,7 +32,7 @@ from ._callback import invoke
|
|||||||
from ._context import active_app
|
from ._context import active_app
|
||||||
from ._event_broker import NoHandler, extract_handler_actions
|
from ._event_broker import NoHandler, extract_handler_actions
|
||||||
from ._filter import LineFilter, Monochrome
|
from ._filter import LineFilter, Monochrome
|
||||||
from .binding import Binding, Bindings, NoBinding
|
from .binding import Binding, Bindings
|
||||||
from .css.query import NoMatches
|
from .css.query import NoMatches
|
||||||
from .css.stylesheet import Stylesheet
|
from .css.stylesheet import Stylesheet
|
||||||
from .design import ColorSystem
|
from .design import ColorSystem
|
||||||
@@ -44,6 +44,7 @@ from .drivers.headless_driver import HeadlessDriver
|
|||||||
from .features import FeatureFlag, parse_features
|
from .features import FeatureFlag, parse_features
|
||||||
from .file_monitor import FileMonitor
|
from .file_monitor import FileMonitor
|
||||||
from .geometry import Offset, Region, Size
|
from .geometry import Offset, Region, Size
|
||||||
|
from .keys import REPLACED_KEYS
|
||||||
from .messages import CallbackType
|
from .messages import CallbackType
|
||||||
from .reactive import Reactive
|
from .reactive import Reactive
|
||||||
from .renderables.blank import Blank
|
from .renderables.blank import Blank
|
||||||
@@ -611,10 +612,15 @@ class App(Generic[ReturnType], DOMNode):
|
|||||||
print(f"(pause {wait_ms}ms)")
|
print(f"(pause {wait_ms}ms)")
|
||||||
await asyncio.sleep(float(wait_ms) / 1000)
|
await asyncio.sleep(float(wait_ms) / 1000)
|
||||||
else:
|
else:
|
||||||
print(f"press {key!r}")
|
original_key = REPLACED_KEYS.get(key, key)
|
||||||
driver.send_event(
|
try:
|
||||||
events.Key(self, key, key if len(key) == 1 else None)
|
char = unicodedata.lookup(
|
||||||
)
|
original_key.upper().replace("_", " ")
|
||||||
|
)
|
||||||
|
except KeyError:
|
||||||
|
char = key if len(key) == 1 else None
|
||||||
|
print(f"press {key!r} (char={char!r})")
|
||||||
|
driver.send_event(events.Key(self, key, char))
|
||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
|
|
||||||
await app._animator.wait_for_idle()
|
await app._animator.wait_for_idle()
|
||||||
|
|||||||
@@ -206,6 +206,7 @@ KEY_NAME_REPLACEMENTS = {
|
|||||||
"plus_sign": "plus",
|
"plus_sign": "plus",
|
||||||
"low_line": "underscore",
|
"low_line": "underscore",
|
||||||
}
|
}
|
||||||
|
REPLACED_KEYS = {value: key for key, value in KEY_NAME_REPLACEMENTS.items()}
|
||||||
|
|
||||||
# Some keys have aliases. For example, if you press `ctrl+m` on your keyboard,
|
# Some keys have aliases. For example, if you press `ctrl+m` on your keyboard,
|
||||||
# it's treated the same way as if you press `enter`. Key handlers `key_ctrl_m` and
|
# it's treated the same way as if you press `enter`. Key handlers `key_ctrl_m` and
|
||||||
|
|||||||
Reference in New Issue
Block a user