Question: Is there a way to get keybinding and convert it to string (text)? #9008

Closed
opened 2026-01-31 01:43:33 +00:00 by claunia · 12 comments
Owner

Originally created by @Chips1234 on GitHub (Jun 13, 2020).

Description of the new feature/enhancement

Is there a way to get the appkeybinding item (e.g. shortcut "Ctrl+T") and convert it to string so we can display it as tooltip/dialog label etc? Thank you in advance!

Originally created by @Chips1234 on GitHub (Jun 13, 2020). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> # Description of the new feature/enhancement Is there a way to get the `appkeybinding` item (e.g. shortcut "Ctrl+T") and convert it to string so we can display it as tooltip/dialog label etc? Thank you in advance!
Author
Owner

@beviu commented on GitHub (Jun 14, 2020):

Maybe you can use this:

c0ffc9b6dc/src/cascadia/TerminalApp/TerminalPage.cpp (L1406-L1411)

@beviu commented on GitHub (Jun 14, 2020): Maybe you can use this: https://github.com/microsoft/terminal/blob/c0ffc9b6dce9b632c868b6584a0f1c8c9dd1df93/src/cascadia/TerminalApp/TerminalPage.cpp#L1406-L1411
Author
Owner

@zadjii-msft commented on GitHub (Jun 15, 2020):

Even better, I'd try KeyChordSerialization::ToString. That's what I'm using to similar effect in the command palette:
dd684cbca1/src/cascadia/TerminalApp/TerminalPage.cpp (L106-L110)

@zadjii-msft commented on GitHub (Jun 15, 2020): Even better, I'd try `KeyChordSerialization::ToString`. That's what I'm using to similar effect in the command palette: https://github.com/microsoft/terminal/blob/dd684cbca1002806f4b8b80d80178942f0bcbb81/src/cascadia/TerminalApp/TerminalPage.cpp#L106-L110
Author
Owner

@Chips1234 commented on GitHub (Jun 15, 2020):

Okay thanks!

@Chips1234 commented on GitHub (Jun 15, 2020): Okay thanks!
Author
Owner

@Chips1234 commented on GitHub (Jun 15, 2020):

So do I replace command with the command (example Copy)?

@Chips1234 commented on GitHub (Jun 15, 2020): So do I replace `command` with the command (example Copy)?
Author
Owner

@Chips1234 commented on GitHub (Jun 15, 2020):

@zadjii-msft. So, can you please give me an example (maybe for the copyText shortcut?) Thanks!

@Chips1234 commented on GitHub (Jun 15, 2020): @zadjii-msft. So, can you please give me an example (maybe for the copyText shortcut?) Thanks!
Author
Owner

@Chips1234 commented on GitHub (Jun 16, 2020):

And what should I call the method? std::wstring?

@Chips1234 commented on GitHub (Jun 16, 2020): And what should I call the method? `std::wstring`?
Author
Owner

@zadjii-msft commented on GitHub (Jun 16, 2020):

I'd probably just make a method on AppKeyBindings that exposed the map of KeyChord->ActionAndArgs, and I'd do something like this:

for (const auto& keyAndArgs : _settings->GetKeybindings().GetKeyMap())
{
    const auto& chord = keyAndArgs.first;
    const auto& actionAndArgs = keyAndArgs.second;
    if (keyChord) 
    { 
        auto label = KeyChordSerialization::ToString(keyChord);
        // Do something with the label here. 
        // EX: Maybe append it to a text box:
        _myTextBox.Text(fmt::format(L"{} - {}", _myTextBox.Text(), label));
    }
}

That would be easier than building every possible action and then checking if that action is bound to something.

@zadjii-msft commented on GitHub (Jun 16, 2020): I'd probably just make a method on `AppKeyBindings` that exposed the map of `KeyChord`->`ActionAndArgs`, and I'd do something like this: ```c++ for (const auto& keyAndArgs : _settings->GetKeybindings().GetKeyMap()) { const auto& chord = keyAndArgs.first; const auto& actionAndArgs = keyAndArgs.second; if (keyChord) { auto label = KeyChordSerialization::ToString(keyChord); // Do something with the label here. // EX: Maybe append it to a text box: _myTextBox.Text(fmt::format(L"{} - {}", _myTextBox.Text(), label)); } } ``` That would be easier than building every possible action and then checking if that action is bound to something.
Author
Owner

@Chips1234 commented on GitHub (Jun 16, 2020):

Thanks!

@Chips1234 commented on GitHub (Jun 16, 2020): Thanks!
Author
Owner

@Chips1234 commented on GitHub (Jun 22, 2020):

@zadjii-msft I figured out the Dialog Construct from run-time part but can you maybe go to my branch on my fork and demonstrate the KeyChordSerialization::ToString part? I learn much better if I get to see it in action. Thank you so much!

@Chips1234 commented on GitHub (Jun 22, 2020): @zadjii-msft I figured out the Dialog Construct from run-time part but can you maybe go to my [branch ](https://github.com/Chips1234/terminal/tree/dev/chips1234/dialog-to-show-all-shortcuts)on my fork and demonstrate the `KeyChordSerialization::ToString` part? I learn much better if I get to see it in action. Thank you so much!
Author
Owner

@zadjii-msft commented on GitHub (Jun 22, 2020):

@Chips1234 Sure.

This code is a good sample of how to add runs of text to a dialog dynamically at runtime.

Then, I'd just do the code I mentioned above to retrieve all the KeyChord's and convert them into strings of text. You can see something similar over in this branch, though, you won't be setting KeyChordText, you'll probably just be setting run.Text(KeyChordSerialization::ToString(keyChord));

@zadjii-msft commented on GitHub (Jun 22, 2020): @Chips1234 Sure. [This code is a good sample](https://github.com/microsoft/terminal/blob/81eb13542abac72094ce0371a45f99768add9f61/src/cascadia/TerminalApp/AppLogic.cpp#L340-L382) of how to add runs of text to a dialog dynamically at runtime. Then, I'd just do the code I mentioned above to retrieve all the `KeyChord`'s and convert them into strings of text. You can see something similar over in [this branch](https://github.com/microsoft/terminal/blob/b90cc38f0ffb371f16a3bb5f306f01ddf72ddc5e/src/cascadia/TerminalApp/TerminalPage.cpp#L107-L110), though, you won't be setting `KeyChordText`, you'll probably just be setting `run.Text(KeyChordSerialization::ToString(keyChord));`
Author
Owner

@Chips1234 commented on GitHub (Jun 22, 2020):

Thanks

@Chips1234 commented on GitHub (Jun 22, 2020): Thanks
Author
Owner

@Chips1234 commented on GitHub (Jun 24, 2020):

And I put the run.text where?

@Chips1234 commented on GitHub (Jun 24, 2020): And I put the `run.text` where?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#9008