Custom key maps#328
Conversation
Actions can now be overridden with custom keys
trevarj
left a comment
There was a problem hiding this comment.
A lot of the diff in crates/libtiny_tui/src/input_area/mod.rs is just moving code around
|
|
||
| #[derive(Debug, Copy, Clone, Deserialize, PartialEq)] | ||
| #[serde(rename_all = "snake_case")] | ||
| pub(crate) enum KeyAction { |
There was a problem hiding this comment.
A KeyAction is dropped in any where a hardcoded Key combo was (except in a few areas where it makes no sense, like up arrow for scrolling back in history.
| [8] => Key::Backspace, | ||
| [127] => Key::Backspace, | ||
| [1] => Key::Ctrl('a'), | ||
| [2] => Key::Ctrl('b'), |
There was a problem hiding this comment.
Added more supported keys. Did not verify them all, but a few I did verify with showkey -a.
If more keys/combos are useful I can add them too
Note to myselfSandbox Install Generate config file template Add to the end of config file I use |
|
edit: WIP on #300 |
Added 'disabled' action to disable key
|
Everything is working well with my AZERTY keyboard and my mapping. |
There was a problem hiding this comment.
Thanks @trevarj !
One concern here is, as far as I can see, we allow keys that we don't support. For example, C-h cannot be supported but we allow ctrl_h. As a user I would absolutely hate it if I would have to manually debug unsupported keys by making changes in my config, /reload, try again...
Maybe we could add a function in term_input to return a list of all supported keys, and then have a validation step after parsing to check that the keys user specified are supported.
We should also have a documentation somewhere (perhaps a GitHub wiki page) for the actions, and what they do.
@osa1 Let's enable the wiki and I can write some docs on this and also migrate some stuff from the README over to it for more organization
Pushing a commit to address this |
|
@osa1 thanks for the review. I addressed all the above comments except for the "Disable" key action
|
| .map(|r| &r.value.0) | ||
| .collect::<HashSet<_>>() | ||
| .into_iter(); | ||
| let fn_name = format_ident!("{}_is_valid_key", fn_name); |
There was a problem hiding this comment.
Didn't know about this macro, nice 👍
|
I created a Wiki page to list supported key actions and key sequences: https://github.com/osa1/tiny/wiki/Configuring-key-bindings |
- Added prev/next entry KeyAction (previously Up and Down arrow) - Added KeyAction::Cancel as a default binding for the Key::Esc - Added KeyAction::Input(char), so individual character keys can be remapped
| pub(crate) fn keypressed(&mut self, key: Key) -> WidgetRet { | ||
| match key { | ||
| Key::Ctrl('c') => { | ||
| pub(crate) fn keypressed(&mut self, key_action: Option<KeyAction>) -> WidgetRet { |
There was a problem hiding this comment.
This function does not handle None case (returns KeyIgnored), I think if we make the argument KeyAction instead of Option<KeyAction> it becomes clear to the caller that this does not handle when a key is not mapped to an action.
If we do that then we decide what to do when a key is not mapped in the code where we try to map the key to an action. It's much nicer, I don't need to follow the value in the code base to find out where the None case is handled and how.
Could you implement that? Should be a few lines of change in TUI::keypressed.
If no KeyAction is found in the current keymap we check to see if the key is a regular character or alt+char to map it to the respective actions
|
Thanks @trevarj ! |
|
Great work, thanks! But we can't map the functional keys (F1...F12...) right now, am I correct? |
|
@097115 right, those keys aren't captured correctly by Tiny and aren't supported yet. I can look into it and see what it would take to add them. |
|
@trevarj Would be great, thanks once again! |
|
Sorry to disturb you, but a bit of issue: ShiftLeft and ShiftRight keys aren't defined in |
|
@097115 thanks. I will add these |
Actions can now be overridden with custom keys
Implementation and usage are fairly straight forward if you look at the
config.yamlI won't really be using this, so I tried to make it very simple. Maybe if someone who uses custom key maps has other ideas, then I will add to this. For example, binding a command to a key combo, or adding more combos (
ctrl_shift_x), etc...Closes #328