Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak key reporting #4861

Merged
merged 12 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Added `tooltip` to Binding https://github.com/Textualize/textual/pull/4859

### Changed

- Change to how keys with modifiers are reported. When shift + another modifier is pressed, then the key name will contain "shift", and the key will be *lower case*. i.e. `ctrl+shift+E` and not `ctrl+E` https://github.com/Textualize/textual/pull/4861
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need updating to match the latest state of the PR?


### 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 @@ -870,7 +870,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
Loading