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

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

Closed
Chips1234 opened this issue Jun 13, 2020 · 12 comments
Closed
Labels
Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. Issue-Question For questions or discussion Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Product-Terminal The new Windows Terminal. Resolution-Answered Related to questions that have been answered

Comments

@Chips1234
Copy link
Contributor

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!

@Chips1234 Chips1234 added the Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. label Jun 13, 2020
@ghost ghost added Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Jun 13, 2020
@beviu
Copy link
Contributor

beviu commented Jun 14, 2020

Maybe you can use this:

auto overrideString = _FormatOverrideShortcutText(keyChord.Modifiers());
auto mappedCh = MapVirtualKeyW(keyChord.Vkey(), MAPVK_VK_TO_CHAR);
if (mappedCh != 0)
{
menuItem.KeyboardAcceleratorTextOverride(overrideString + gsl::narrow_cast<wchar_t>(mappedCh));
}

@zadjii-msft
Copy link
Member

Even better, I'd try KeyChordSerialization::ToString. That's what I'm using to similar effect in the command palette:

auto keyChord{ _settings->GetKeybindings().GetKeyBindingForActionWithArgs(command.Action()) };
if (keyChord)
{
command.KeyChordText(KeyChordSerialization::ToString(keyChord));
}

@zadjii-msft zadjii-msft added Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. Issue-Question For questions or discussion Product-Terminal The new Windows Terminal. Resolution-Answered Related to questions that have been answered and removed Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. labels Jun 15, 2020
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Jun 15, 2020
@Chips1234
Copy link
Contributor Author

Okay thanks!

@Chips1234
Copy link
Contributor Author

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

@Chips1234
Copy link
Contributor Author

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

@Chips1234
Copy link
Contributor Author

Chips1234 commented Jun 16, 2020

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

@zadjii-msft
Copy link
Member

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.

@Chips1234
Copy link
Contributor Author

Thanks!

@Chips1234
Copy link
Contributor Author

@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!

@zadjii-msft
Copy link
Member

@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));

@Chips1234
Copy link
Contributor Author

Chips1234 commented Jun 22, 2020 via email

@Chips1234
Copy link
Contributor Author

And I put the run.text where?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. Issue-Question For questions or discussion Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Product-Terminal The new Windows Terminal. Resolution-Answered Related to questions that have been answered
Projects
None yet
Development

No branches or pull requests

3 participants