如何在VS C#中获取本地化的键盘键名称 [英] How to get the localized keyboard-key-name in VS C#

查看:141
本文介绍了如何在VS C#中获取本地化的键盘键名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ToolStripMenuItem,该ToolStripMenuItem具有ShortcutKeys Ctrl + Oemcomma(即Ctrl+,).我想在项目名称附近显示该快捷方式,以便用户可以看到该快捷方式.不幸的是,它显示为Ctrl+Oemcomma而不是更易理解的Ctrl+,.

I have a ToolStripMenuItem that has the ShortcutKeys Ctrl+Oemcomma (i.e. Ctrl+,). 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+,.

有一个属性ShortcutKeyDisplayString,它覆盖了自动创建的字符串,因此可以对其进行修复.但是,一旦应用程序以不调用控制键Ctrl的语言运行(例如在德国,它称为Strg),则ShortcutKeyDisplayString看起来就不正确,因为所有其他自动创建的快捷方式说明都已翻译(例如,如果在英语操作系统中,说明显示为Ctrl+S,在德语操作系统中,说明显示为Strg+S.

There's the property ShortcutKeyDisplayString that overrides the automatic created string, so that way one can fix it. But as soon as the application is run in a language that doesn't call the control-key Ctrl (e.g. in germany it's called Strg), that ShortcutKeyDisplayString looks wrong, as all other automatic created shortcut-descriptions are translated (i.e. if in an english OS a description is displayed as Ctrl+S, in a german OS it then displays Strg+S).

是否有一个函数返回键的本地化名称,以便我可以使用它来设置ShortcutKeyDisplayString? IE.我正在寻找一个在英语操作系统中返回Ctrl并在德语OS等中返回Strg的函数.我尝试了System.Windows.Forms.Keys.Control.ToString(),但是当然只返回了Control.

Is there a function that returns the localized name of a key so that I could use that to set the ShortcutKeyDisplayString? I.e. 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.

推荐答案

Keys枚举类型定义TypeConverter.
我们从KeysConverter继承,因为这是Keys的关联TypeConverter,我们只需要处理Keys.Oemcomma值.

Define a TypeConverter for Keys enum type.
We inherit from KeysConverter as this is the associated TypeConverter of Keys and we need to handle only the Keys.Oemcomma value.

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

然后在您的Program.cs中将其插入Application.Run(...)之前:

Then in your Program.cs before callign Application.Run(...):

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

这篇关于如何在VS C#中获取本地化的键盘键名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆