简体   繁体   English

有没有简单的方法(包装器/字典)从 Keys enum 转换为 SenKeys 字符串?

[英]Is there any easy way (wrapper/dictionary) to convert from Keys enum to SenKeys string?

I need to convert from Keys to strings used in Sendkeys method.我需要将 Keys 转换为 Sendkeys 方法中使用的字符串。 Sample:样本:

Key.Enter to {ENTER} Key.Enter 到 {ENTER}

Is there any easy way to do that?有什么简单的方法可以做到吗? I could not find it.我找不到。

 if (key == Key.Enter)
     SendKeys.SendWait({ENTER});

I need tomething that convert ALL keys.. If key == Key.a, I will just send a.我需要转换所有键的东西。如果键 == Key.a,我将只发送一个。 But, if it is a command key (ex: Key.Enter) I need to make it upper and add {}.但是,如果它是一个命令键(例如:Key.Enter),我需要把它放在上面并添加 {}。

A Possible way could be wrap the Key standard enumerator into a wrapper class, with this approach you can create a ToString() method that transform the enumerator value into a string.一种可能的方法是将Key标准枚举器包装到包装器 class 中,使用这种方法,您可以创建将枚举器值转换为字符串的ToString()方法。

Take a look at this example:看看这个例子:

Enum Wrapper Class枚举包装器 Class

class KeyEnumWrapper {
        public System.Windows.Forms.Keys key { get; set; }

        public KeyEnumWrapper(System.Windows.Forms.Keys key) {
            this.key = key;
        }

        public string ToString() {
            return "{" + key.ToString().ToUpper() + "}";
        }
    }

Client (usage)客户端(使用)

  private void Form1_KeyUp(object sender, KeyEventArgs e) {
            KeyEnumWrapper wp = new KeyEnumWrapper(e.KeyCode);
            SendKeys.SendWait(wp.ToString())
        }
Key.Enter.ToString().ToUpper()

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

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