Pedro77
Pedro77

Reputation: 5294

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. Sample:

Key.Enter to {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. But, if it is a command key (ex: Key.Enter) I need to make it upper and add {}.

Upvotes: 2

Views: 653

Answers (2)

Tigran
Tigran

Reputation: 62246

Key.Enter.ToString().ToUpper()

Upvotes: 0

aleroot
aleroot

Reputation: 72636

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.

Take a look at this example :

Enum Wrapper 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())
        }

Upvotes: 1

Related Questions