Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3422314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:12:07+00:00 2026-05-18T06:12:07+00:00

In my application, I want to let users customize keyboard shortcuts, just like it’s

  • 0

In my application, I want to let users customize keyboard shortcuts, just like it’s done in Visual Studio’s keyboard options. The user can focus a blank text box and then type any shortcut he wants to assign to a command.

The closest I’ve come to make it work is by subscribing to the TextBox.PreviewKeyDown event, setting it as handled to prevent actual text input in the text box. I then ignore the KeyDown events associated with modifier keys (is there a cleaner way to determine if a Key is a modifier key?).

// Code-behind
private void ShortcutTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    // The text box grabs all input
    e.Handled = true;

    if (e.Key == Key.LeftCtrl || 
        e.Key == Key.RightCtrl || 
        e.Key == Key.LeftAlt ||
        e.Key == Key.RightAlt || 
        e.Key == Key.LeftShift ||
        e.Key == Key.RightShift)
        return;

    string shortcutText = "";
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        shortcutText += "Ctrl+";
    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
        shortcutText += "Shift+";
    if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
        shortcutText += "Alt+";
    _ShortcutTextBox.Text = shortcutText + e.Key.ToString();

}

The above works for any shortcut starting with Ctrl and Ctrl+Shift, but fails for any Alt shortcuts. The e.Key is always set to Key.System when I press a shortcut containing Alt.

How can I record Alt shortcuts from the user? Is there a better, more robust way to record shortcuts form the user?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:12:08+00:00Added an answer on May 18, 2026 at 6:12 am

    The trick is to use the SystemKey property if the Key property is set to Key.System:

    private void ShortcutTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        // The text box grabs all input.
        e.Handled = true;
    
        // Fetch the actual shortcut key.
        Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
    
        // Ignore modifier keys.
        if (key == Key.LeftShift || key == Key.RightShift
            || key == Key.LeftCtrl || key == Key.RightCtrl
            || key == Key.LeftAlt || key == Key.RightAlt
            || key == Key.LWin || key == Key.RWin) {
            return;
        }
    
        // Build the shortcut key name.
        StringBuilder shortcutText = new StringBuilder();
        if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) {
            shortcutText.Append("Ctrl+");
        }
        if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) {
            shortcutText.Append("Shift+");
        }
        if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0) {
            shortcutText.Append("Alt+");
        }
        shortcutText.Append(key.ToString());
    
        // Update the text box.
        _ShortcutTextBox.Text = shortcutText.ToString();
    }
    

    I added the left and right Windows keys to the modifier list, because they sometimes appeared in the shortcut key name when a complex (Ctrl+Shift+Alt) key combination was typed from a Terminal Server session. They’re never present in Keyboard.Modifiers, though, since they’re reserved for global shortcuts, so I don’t handle them there.

    I also used a StringBuilder to avoid creating too many string instances.

    This solution works with any key combination, except Shift+Alt (the Alt modifier is not seen in that case). That might be an artifact of my Terminal Server environment, though, so your mileage may vary.

    Finally, I added a _File menu to the window to see what would happen, and the Alt+F shortcut key is effectively trapped by the text box before it reaches the menu, which seems to be what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to let the user customize the background of of my application and
If we want in our WPF application to let a user make a SQL
I want to add a feature to our application that let's a user know
I develop a web application that let users to upload files like images and
I'm developing an application and want to let users connect their account to one
I want to create an application that will let users get new music from
In my C# standalone application, I want to let users click on a link
Possible Duplicate: 'Like' a page using facebook graph api I want to let users
i want to open a pdf file in my web application , let users
I want my application to reset shared preferences when the application reinstalling let's say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.