# Forum > MMO > Guild Wars 2 > GW2 Bots and Programs >  Sending Keys in C# to GW2

## RazorDreamz

I'm working on a bot for GW2 using C#. I can detect pixels, and use the mouse to move and click etc, but for some reason sending keys isn't working for me.

I've tried:



```
SendKeys.SendWait("1");  // does nothing except pop the menu for some reason.
```

Also:



```
BotControl.SendMessage(win, WM_KEYDOWN, ((IntPtr) Keys.Oem1), IntPtr.Zero);
```

Seems to do nothing that I can tell. 



```
BotControl.PostMessage(win, BotControl.WM_KEYDOWN, ((IntPtr) Keys.Oem1),IntPtr.Zero);
```

Does nothing, but if I select the chat window it drops ";" in chat instead of "1". 

Also tried the Virtual Keyboard:



```
[DllImport("user32.dll")]
            private static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

            public static void KeyDown(Keys key)
            {
                keybd_event((byte) key, 0, 0, 0);
            }

            public static void KeyUp(Keys key)
            {
                keybd_event((byte) key, 0, 0x0002, 0);
            }
```

doing 


```
KeyDown(Keys.Oem1);
KeyUp(Keys.Oem1);
```

And it also does nothing, but if I select the chat window it prints ";" again. 

Any ideas what could be going wrong? Did this for WOW, EVE and SWTOR without issue.

----------


## spawnfestis

Try posting a keyboard message instead using the PostMessage() API, you can look it up here at MSDN

----------


## saddened

GW2 uses DirectInput to receive keyboard/mouse events, window messages/events won't work. 

SendInput function (SendInput)

----------


## xtrmxtrm

In autoit there is a ControlSendKey function which does what you want. You will need to do the same as that method.
1. Focus in the windows of GW2 and set it active.
2. Send Control Keys to the focused active window of GW2.

----------


## Maddin1803

@saddened
Are you absolutly sure ? 
I thought you can use postmessage, with the right parms.

----------


## Arakkoa

keybd_event function

This is also a way, I'm sure pinvoke.net or something has the C# stuff you need to use this.

----------


## MrUnreal

PostMsg works just fine

----------


## viperbot

keybd_event works fine, just make sure the game has the focus.

----------


## Xelper

Call Example() when GW2 has focus. If you are in windowed mode it doesn't need to have focus. You need to have the proper wParam and lParam. You can google them for a list.



```
        [DllImport("User32.Dll")]
        public static extern Int32 PostMessage(int hWnd, int msg, int wParam, int lParam);


        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();


        ///summary>
        /// Virtual Messages
        /// </summary>
        public enum WMessages : int
        {
            WM_LBUTTONDOWN = 0x201, //Left mousebutton down
            WM_LBUTTONUP = 0x202,  //Left mousebutton up
            WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
            WM_RBUTTONDOWN = 0x204, //Right mousebutton down
            WM_RBUTTONUP = 0x205,   //Right mousebutton up
            WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
            WM_KEYDOWN = 0x100,  //Key down
            WM_KEYUP = 0x101,   //Key up
        }


        public static void PostMessage_PressKeyDown(int hWnd, int Key, int lParam)
        {
            PostMessage(hWnd, (int)WMessages.WM_KEYDOWN, Key, lParam);
        }
        public static void PostMessage_PressKeyUp(int hWnd, int Key, int lParam)
        {
            PostMessage(hWnd, (int)WMessages.WM_KEYUP, Key, lParam);
        }




	public static void Example()
	{


		int VK_2 = (int)0x32;
		int VK_2_lParam = 0x30001;
		System.Threading.Thread.Sleep(3000); //This is just so you have enough time to get back to the proper foreground window that you wanna send the 2s to...this can be avoided by using a saved hWnd instead of GetForgroundWindow()
		cSendInput.PostMessage_PressKeyDown(GetForegroundWindow(), VK_2, VK_2_lParam);
		System.Threading.Thread.Sleep(1000);
		cSendInput.PostMessage_PressKeyUp(GetForegroundWindow(), VK_2, VK_2_lParam);
	}
```

----------

