Skip to content

Commit

Permalink
Merge pull request #4861 from Textualize/key-modifiers
Browse files Browse the repository at this point in the history
tweak key reporting
  • Loading branch information
willmcgugan authored Aug 12, 2024
2 parents e56c391 + 4e5a2b6 commit 23172c1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `tooltip` to Binding https://github.com/Textualize/textual/pull/4859
- Added `TOOLTIP_DELAY` to App to customize time until a tooltip is displayed

### Changed

- Removed caps_lock and num_lock modifiers https://github.com/Textualize/textual/pull/4861

### Fixed

- Fix crash when `validate_on` value isn't a set https://github.com/Textualize/textual/pull/4868
Expand Down
16 changes: 5 additions & 11 deletions src/textual/_xterm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,13 @@ def _sequence_to_key_events(self, sequence: str) -> Iterable[events.Key]:
key_tokens: list[str] = []
if modifiers:
modifier_bits = int(modifiers) - 1
MODIFIERS = (
"shift",
"alt",
"ctrl",
"super",
"hyper",
"meta",
"caps_lock",
"num_lock",
)
for bit, modifier in zip(range(8), MODIFIERS):
# Not convinced of the utility in reporting caps_lock and num_lock
MODIFIERS = ("shift", "alt", "ctrl", "super", "hyper", "meta")
# Ignore caps_lock and num_lock modifiers
for bit, modifier in enumerate(MODIFIERS):
if modifier_bits & (1 << bit):
key_tokens.append(modifier)

key_tokens.sort()
key_tokens.append(key)
yield events.Key(
Expand Down
2 changes: 1 addition & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def active_bindings(self) -> dict[str, ActiveBinding]:
This property may be used to inspect current bindings.
Returns:
Active binding information
A dict that maps keys on to binding information.
"""
return self.screen.active_bindings

Expand Down
8 changes: 6 additions & 2 deletions src/textual/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,12 @@ def is_printable(self) -> bool:

def _key_to_identifier(key: str) -> str:
"""Convert the key string to a name suitable for use as a Python identifier."""
if len(key) == 1 and key.isupper():
key = f"upper_{key.lower()}"
key_no_modifiers = key.split("+")[-1]
if len(key_no_modifiers) == 1 and key_no_modifiers.isupper():
if "+" in key:
key = f"{key.rpartition('+')[0]}+upper_{key_no_modifiers}"
else:
key = f"upper_{key_no_modifiers}"
return key.replace("+", "_").lower()


Expand Down

0 comments on commit 23172c1

Please sign in to comment.