Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

How to translate KeyCode to work at Keys.SendKey

is there a way to Translate a KeyCode in that way ,which will work if i use it at Keys.Sendkey();

private void Manager_KeyDown(object sender, KeyEventArgs e)
{
      Keys.SendKey(e.KeyCode.toString());
}

i tried that way and it wont work ,so is there a way to do that dynamically.

Upvotes: 5

Views: 5545

Answers (3)

MiMFa
MiMFa

Reputation: 1164

It works great!

    public string ToString(Keys key)
    {
        switch (key)
        {
            case Keys.Back:
                return "{BACKSPACE}";
            case Keys.Separator:
                return "{BREAK}";
            case Keys.CapsLock:
                return "{CAPSLOCK}";
            case Keys.Delete:
                return "{DELETE}";
            case Keys.Down:
                return "{DOWN}";
            case Keys.End:
                return "{END}";
            case Keys.Enter:
                return "{ENTER}";
            case Keys.Escape:
                return "{ESC}";
            case Keys.Help:
                return "{HELP}";
            case Keys.Home:
                return "{HOME}";
            case Keys.Insert:
                return "{INSERT}";
            case Keys.Left:
                return "{LEFT}";
            case Keys.NumLock:
                return "{NUMLOCK}";
            case Keys.PageDown:
                return "{PGDN}";
            case Keys.PageUp:
                return "{PGUP}";
            case Keys.PrintScreen:
                return "{PRTSC}";
            case Keys.Right:
                return "{RIGHT}";
            case Keys.Scroll:
                return "{SCROLLLOCK}";
            case Keys.Tab:
                return "{TAB}";
            case Keys.Up:
                return "{UP}";
            case Keys.F1:
                return "{F1}";
            case Keys.F2:
                return "{F2}";
            case Keys.F3:
                return "{F3}";
            case Keys.F4:
                return "{F4}";
            case Keys.F5:
                return "{F5}";
            case Keys.F6:
                return "{F6}";
            case Keys.F7:
                return "{F7}";
            case Keys.F8:
                return "{F8}";
            case Keys.F9:
                return "{F9}";
            case Keys.F10:
                return "{F10}";
            case Keys.F11:
                return "{F11}";
            case Keys.F12:
                return "{F12}";
            case Keys.F13:
                return "{F13}";
            case Keys.F14:
                return "{F14}";
            case Keys.F15:
                return "{F15}";
            case Keys.F16:
                return "{F16}";
            case Keys.Add:
                return "{ADD}";
            case Keys.Subtract:
                return "{SUBTRACT}";
            case Keys.Multiply:
                return "{MULTIPLY}";
            case Keys.Divide:
                return "{DIVIDE}";
            case Keys.ShiftKey:
                return "+";
            case Keys.ControlKey:
                return "^";
            case Keys.Alt:
                return "%";

        }
        return key.ToString();
    }

Enjoy...

Upvotes: 0

user1047099
user1047099

Reputation: 40

Well i don't know a better what but this ,you should capture all keys on KeyDown event and send them as String in that format from that web site.

http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

Upvotes: 2

Pete
Pete

Reputation: 10871

Keys is the enumeration and does not contain a SendKey method. You can however do something like this:

SendKeys.Send(Keys.A.ToString());

You can also send multiple keys by using string concatenation:

SendKeys.Send(Keys.A.ToString() + Keys.B.ToString());

Similary, this code works for me:

private void departmentList_KeyDown(object sender, KeyEventArgs e)
{
    Keys key = e.KeyCode;
    SendKeys.Send(key.ToString());
}

Also check out this question: SendKeys::Send, going berserk. What is your goal by doing this, if I might ask?

Upvotes: 1

Related Questions