简体   繁体   English

如何在VS C#中获取本地化的键盘键名称

[英]How to get the localized keyboard-key-name in VS C#

I have a ToolStripMenuItem that has the ShortcutKeys Ctrl+Oemcomma (ie Ctrl+, ). 我有一个具有ShortcutKeys Ctrl + Oemcomma(即Ctrl+, )的ToolStripMenuItem I want to show that shortcut near the item-name, so the user can see that shortcut. 我想在项目名称旁边显示该快捷方式,以便用户可以看到该快捷方式。 Unluckily it's shown as Ctrl+Oemcomma and not as the more understandable Ctrl+, . 不幸的是,它显示为Ctrl+Oemcomma而不是更易理解的Ctrl+,

There's the property ShortcutKeyDisplayString that overrides the automatic created string, so that way one can fix it. 有一个属性ShortcutKeyDisplayString,它会覆盖自动创建的字符串,因此可以对其进行修复。 But as soon as the application is run in a language that doesn't call the control-key Ctrl (eg in germany it's called Strg ), that ShortcutKeyDisplayString looks wrong, as all other automatic created shortcut-descriptions are translated (ie if in an english OS a description is displayed as Ctrl+S , in a german OS it then displays Strg+S ). 但是,只要应用程序以不调用控制键Ctrl的语言运行(例如在德国,它称为Strg ),那么ShortcutKeyDisplayString就会看起来不正确,因为所有其他自动创建的快捷方式描述都已翻译(即,如果使用英文操作系统,说明显示为Ctrl+S ,而在德语操作系统中,则显示Strg+S )。

Is there a function that returns the localized name of a key so that I could use that to set the ShortcutKeyDisplayString? 是否有一个函数返回键的本地化名称,以便我可以使用它来设置ShortcutKeyDisplayString? Ie I'm looking for a function that returns Ctrl in an english OS and Strg in a german OS etc. I tried System.Windows.Forms.Keys.Control.ToString() , but that of course just returns Control . 即我正在寻找一个函数,该函数在英语OS中返回Ctrl ,在德语OS中返回Strg等。我尝试了System.Windows.Forms.Keys.Control.ToString() ,但是当然只返回Control

Define a TypeConverter for Keys enum type. Keys枚举类型定义TypeConverter
We inherit from KeysConverter as this is the associated TypeConverter of Keys and we need to handle only the Keys.Oemcomma value. 我们从KeysConverter继承,因为这是关联的Keys TypeConverter ,我们只需要处理Keys.Oemcomma值。

public class ShortcutKeysConverter : KeysConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (Type.Equals(destinationType, typeof(string)) && value is Keys)
        {
            var key = (Keys)value;

            if (key.HasFlag(Keys.Oemcomma))
            {
                string defaultDisplayString =
                    base
                        .ConvertTo(context, culture, value, destinationType)
                        .ToString();

                return defaultDisplayString.Replace(Keys.Oemcomma.ToString(), ",");
            }
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }
}

Then in your Program.cs before callign Application.Run(...) : 然后在您的Program.cs调用Application.Run(...)

TypeDescriptor
    .AddAttributes(
        typeof(Keys),
        new TypeConverterAttribute(typeof(ShortcutKeysConverter))
    );

Based on Gabors answer I solved it as follows. 根据Gabors的答案,我如下解决了它。 It might be hacky but it's short and works. 它可能很笨拙,但很短而且可以正常工作。

settingsToolStripMenuItem.ShortcutKeyDisplayString = ((new KeysConverter()).ConvertTo(Keys.Control, typeof(string))).ToString().Replace("None", ",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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