简体   繁体   中英

How can i generate a localized keyboard shortcut in C# / WPF?

I want to generate a localized keyboard shortcut for "de-DE" instead of writing it ("Strg+Einfg") by hand.

var kg = new KeyGesture( Key.Insert, ModifierKeys.Control, "Strg+Einfg" );

Is there a nice way to generate the "Strg+Einfg" at the time the KeyGesture is created ?

Do i have to use something like

var dummy = new KeyGesture( Key.Insert, ModifierKeys.Control );   
var kg    = new KeyGesture( Key.Insert, ModifierKeys.Control, dummy.GetDisplayStringForCulture( ... ) );

? (?) (??)

I had the same problem myself and checked the WPF source code. Sadly, strings like "Ctrl" are in fact hardcoded.

So my idea was to simply use the WindowsForms classes for displaying shortcut strings. You can do this with the following code (you could use this for a derived RoutedCommand class that automatically replaces all shortcut keys with a new display string):

private void LocalizeShortcuts()
{
    foreach (KeyGesture keyGuesture in this.InputGestures.OfType<KeyGesture>().ToArray())
    {
        this.InputGestures.Remove(keyGuesture);

        System.Windows.Forms.Keys formsKey = (Keys)KeyInterop.VirtualKeyFromKey(keyGuesture.Key);
        if ((keyGuesture.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
        {
            formsKey |= Keys.Alt;
        }

        if ((keyGuesture.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            formsKey |= Keys.Control;
        }

        if ((keyGuesture.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
        {
            formsKey |= Keys.Shift;
        }

        string keyDisplayString = TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(formsKey);
        this.InputGestures.Add(new KeyGesture(keyGuesture.Key, keyGuesture.Modifiers, keyDisplayString));
    }
}

I think the answer is that you can safely leave out that parameter.

The documentation isn't completely clear, but I think this is what is going on:

  1. Only set DisplayString if you need to change the default behavior. I would have named it something like "DisplayStringOverride".
  2. Whenever you generate a MenuItem, WPF will call KeyGesture.GetDisplayStringForCulture.
  3. If you need to access the display string elsewhere (eg. in a button tooltip), you should also use KeyGesture.GetDisplayStringForCulture.

In my code, #3 is where I was messing up. I was reading KeyGesture.DisplayString, which led me to believe that I needed to set it in the constructor. However, I found that I was able to omit that parameter when I started using this extension method instead:

using System.Globalization;
using System.Windows.Input;
 ...
public static string GetDisplayString(this KeyGesture keyGesture)
{
    return keyGesture.GetDisplayStringForCulture(CultureInfo.CurrentCulture);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM