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

Add a KeyChordListener to the Settings UI #10652

Merged
3 commits merged into from
Jul 16, 2021
Merged

Conversation

carlos-zamora
Copy link
Member

@carlos-zamora carlos-zamora commented Jul 13, 2021

Summary of the Pull Request

Replaces the key chord editor in the actions page with a listener instead of a plain text box.

References

#6900 - Settings UI Epic

Detailed Description of the Pull Request / Additional comments

  • Actions page:
    • Replace Keys with CurrentKeys for consistency with Action/CurrentAction
    • ProposedKeys is now a Control::KeyChord
    • removes key chord validation (now we don't need it)
    • removes accept/cancel shortcuts (nowhere we could use it now)
  • KeyChordListener:
    • Keys: dependency property that hooks us up to a system to the committed setting value
      • this is the key binding view model, which propagates the change to the settings model clone on "accept changes"
    • We bind to PreviewKeyDown to intercept the key event before some special key bindings are handled (i.e. "select all" in the text box)
    • CoreWindow is used to get the modifier keys because (1) it's easier than updating on each key press and (2) that approach resulted in a strange bug where the Alt key-up event was not detected
    • LosingFocus means that we have completed our operation and want to commit our changes to the key binding view model
    • KeyDown does most of the magic of updating Keys. We filter out any key chords that could be problematic (i.e. Shift+Tab and Tab for keyboard navigation)

Validation Steps Performed

  • Tested a few key chords:
    • ✅single key: X
    • ✅key with modifier(s): Ctrl+Alt+X
    • ❌plain modifier: Ctrl
    • ✅key that is used by text box: Ctrl+A
    • ✅key that is used by Windows Terminal: Alt+F4
    • ❌key that is taken by Windows OS: Windows+X
    • ✅key that is not taken by Windows OS: Windows+Shift+X
  • Known issue:
    • global key taken by Windows Terminal: (i.e. quake mode keybinding)
      • Behavior: global key binding executed
      • Expected: key chord recorded

Demo

Key Chord Listener Demo

@skyline75489
Copy link
Collaborator

I’ve been expecting this feature for a long time. Thanks for this!

Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

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

Despite my comments, I'm totally cool with this. Well done!

src/cascadia/TerminalSettingsEditor/Actions.h Outdated Show resolved Hide resolved
src/cascadia/TerminalSettingsEditor/KeyChordListener.xaml Outdated Show resolved Hide resolved
src/cascadia/TerminalSettingsEditor/KeyChordListener.cpp Outdated Show resolved Hide resolved
const auto key{ e.OriginalKey() };
for (const auto mod : ModifierKeys)
{
if (key == mod)
Copy link
Member

Choose a reason for hiding this comment

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

some implementations show "partial" keys as they go. like, if you press shift it'll say "shift+". Is that desirable here, or too complicated?

Copy link
Member Author

Choose a reason for hiding this comment

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

I played with that a little bit. I ended up not liking it because plain modifiers aren't a valid key chord (at least for a key binding, not the object itself).

We could change it at any time, but I vote we see how this feels in a selfhost/bug-bash, then iterate from there.

@carlos-zamora carlos-zamora added the Needs-Second It's a PR that needs another sign-off label Jul 16, 2021
Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

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

Just one question and some unimportant nits.

src/cascadia/TerminalSettingsEditor/KeyChordListener.cpp Outdated Show resolved Hide resolved
}
}

const auto modifiers{ _GetModifiers() };
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the need for _GetModifiers.
If you keep track over how many keys are simultaneously pressed you can use the key down events directly to assemble the modifiers bitfield. This removes the need for the majority of the _GetModifiers implementation.
(You'd also need to reset the modifiers when the control looses focus of course.)

Copy link
Member

Choose a reason for hiding this comment

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

My gut feeling is that introducing another place where state must be preserved and maintained is going to be worse for code clarity (and has the risk of introducing torn state) where here in a non-perf-sensitive context we can just use the state the UI framework is already keeping track of for us 😄

Copy link
Member

@lhecker lhecker Jul 16, 2021

Choose a reason for hiding this comment

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

I haven't tried out this branch locally but I assumed that it works similar to key bindings in Discord: All key presses are additive, until you let go of any key, after which the result is added.

Meaning if you press:

  • M Down
  • Ctrl Down
  • Shift Down
  • M Up

...in that order it'll save Ctrl+Shift+M as a keybinding. Even though I pressed M first, which is important because the UI should work even if I attempt to press all 3 keys simultaneously! (...and then any of those may be pressed first.)

If that's how this is supposed to work as well I disagree: Using the events lends itself to a clearer association between what happens in the UI and how the code would work (namely all key downs being additive, until a key up occurs).

Copy link
Member

Choose a reason for hiding this comment

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

I've tested it some more in Discord: If you let go of a modifier key for a while it'll count as a keybinding automatically. But I don't think we have to make it that complicated.

Also I just realized a problem with my approach: You still would have to detect when you have released all modifier keys. 😅 As such a good approach might be to count the concurrently pressed keys after all.
I'll approve the PR as I realize that the current code is already written and functional.

Copy link
Member

Choose a reason for hiding this comment

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

I've been talking with Dustin about this: Making all key presses purely additive would probably be the most consistent way to enter key chords. Discord's keybinding editor is a pretty great template for us.
As I said before we'd need a "concurrently pressed keys counter" though, in order to detect when all keys have been released. Because every time this counter reaches zero we'll have to call KeyChordSerialization::ToString and see if all added up keys formed a valid sequence, as well as to reset the modifiers and vkey member fields.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's funny that you say that, because PowerToys' hotkey listener kinda works like that. It constantly updates the control on every key press, but it only sets the setting on a key-up event.

I figured the current approach would be a bit easier and cleaner because we only accept key chords that have an associated non-modifier vkey. This also forces you to actually press the key combination you want to enact, because in TermControl pressing M-Ctrl-Shift would execute the M key binding.

src/cascadia/TerminalSettingsEditor/KeyChordListener.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalSettingsEditor/Actions.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalSettingsEditor/Actions.cpp Outdated Show resolved Hide resolved
Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

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

I'll approve the PR as I realize that the current code is already written and functional.

There's a couple nits I commented before which you can tackle if you agree about them.

@carlos-zamora carlos-zamora added AutoMerge Marked for automatic merge by the bot when requirements are met and removed Needs-Second It's a PR that needs another sign-off labels Jul 16, 2021
@ghost
Copy link

ghost commented Jul 16, 2021

Hello @carlos-zamora!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 8947909 into main Jul 16, 2021
@ghost ghost deleted the dev/cazamor/actions-page/key-listener branch July 16, 2021 22:12
@ghost
Copy link

ghost commented Aug 31, 2021

🎉Windows Terminal Preview v1.11.2421.0 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoMerge Marked for automatic merge by the bot when requirements are met
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants