2 Replies - 40095 Views - Last Post: 03 October 2006 - 11:21 PM Rate Topic: -----

#1 Vegter   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Sending Mouse and Keyboard input from Code

Posted 28 September 2006 - 06:10 AM

Hi there

I am writing an application that should interface with a User Interface application. This basically means that my application should move the mouse and click in certain areas of another application, and then retrieve a value for me. So, for example, my application should be able to move the mouse to a different application, move through several forms by clicking on the correct buttons, enter certain values into the correct textboxes, and at the very end retrieve a value for me by selecting it and pressing Ctrl+C, and then send back this value. I am aware that this is probably the worst possible way of doing this, but it seems like I have no other option.

I have seen a similar application written in Delphi which seemed VERY simple. I have attempted something similar with C#, but all the solutions I find seem to overcomplicate matters. I found that it is easy enough to move the mouse by using
Cursor.Position = new Position(x,y)
, but I can't seem to get the clicking right, and providing and retrieving keyboard input seems a nightmare. I am seriously considering just running a delphi application from C#.

I had a look at the following article

http://www.codeproje...s/take-over.asp

which seems to do something similar to what I want, but when I perform a click event (I tried to just get it to open the windows start menu) it just does nothing.

This is the code I have written so far:
		private void button1_Click(object sender, System.EventArgs e)
		{
			//Cursor.Position = new Point(5,763);
			SendLeftButtonDown(5,5);
			SendLeftButtonUp(5,5);
		}

		private int handle;

		#region SendMessage Constants

		private const int WM_LBUTTONDOWN = 0x0201;
		private const int WM_LBUTTONUP   = 0x0202;
		private const int WM_LBUTTONDBLCLK = 0x0203; 
		private const int WM_RBUTTONDOWN = 0x0204;
		private const int WM_RBUTTONUP	 = 0x0205;
		private const int WM_RBUTTONDBLCLK = 0x0206;
		private const int WM_MOUSEMOVE   = 0x0200;

		private const int WM_KEYDOWN = 0x100;
		private const int WM_KEYUP = 0x0101;
		private const int WM_CHAR = 0x0102;

		#endregion SendMessage Constants

		#region Virtual Keys Constants
  
		public const int VK_0		   = 0x30;
		public const int VK_1		   = 0x31;
		public const int VK_2		   = 0x32;
		public const int VK_3		   = 0x33;
		public const int VK_4		   = 0x34;
		public const int VK_5		   = 0x35;
		public const int VK_6		   = 0x36;
		public const int VK_7		   = 0x37;
		public const int VK_8		   = 0x38;
		public const int VK_9		   = 0x39;
		public const int VK_A		   = 0x41;
		public const int VK_B		   = 0x42;
		public const int VK_C		   = 0x43;
		public const int VK_D		   = 0x44;
		public const int VK_E		   = 0x45;
		public const int VK_F		   = 0x46;
		public const int VK_G		   = 0x47;
		public const int VK_H		   = 0x48;
		public const int VK_I		   = 0x49;
		public const int VK_J		   = 0x4A;
		public const int VK_K		   = 0x4B;
		public const int VK_L		   = 0x4C;
		public const int VK_M		   = 0x4D;
		public const int VK_N		   = 0x4E;
		public const int VK_O		   = 0x4F;
		public const int VK_P		   = 0x50;
		public const int VK_Q		   = 0x51;
		public const int VK_R		   = 0x52;
		public const int VK_S		   = 0x53;
		public const int VK_T		   = 0x54;
		public const int VK_U		   = 0x55;
		public const int VK_V		   = 0x56;
		public const int VK_W		   = 0x57;
		public const int VK_X		   = 0x58;
		public const int VK_Y		   = 0x59;
		public const int VK_Z		   = 0x5A;

		public const int VK_BACK	  =	0x08;
		public const int VK_TAB	   =	0x09;
		public const int VK_CLEAR	 =	0x0C;
		public const int VK_RETURN	=	0x0D;
		public const int VK_SHIFT	 =	0x10;
		public const int VK_CONTROL   =	0x11;
		public const int VK_MENU	  =	0x12;
		public const int VK_PAUSE	 =	0x13;
		public const int VK_CAPITAL   =	0x14;
		public const int VK_KANA	  =	0x15;
		public const int VK_HANGEUL   =	0x15;
		public const int VK_HANGUL	=	0x15;
		public const int VK_JUNJA	 =	0x17;
		public const int VK_FINAL	 =	0x18;
		public const int VK_HANJA	 =	0x19;
		public const int VK_KANJI	 =	0x19;
		public const int VK_ESCAPE	=	0x1B;
		public const int VK_CONVERT   =	0x1C;
		public const int VK_NONCONVERT=	0x1D;
		public const int VK_ACCEPT	=	0x1E;
		public const int VK_MODECHANGE=	0x1F;
		public const int VK_SPACE	 =	0x20;
		public const int VK_PRIOR	 =	0x21;
		public const int VK_NEXT	  =	0x22;
		public const int VK_END	   =	0x23;
		public const int VK_HOME	  =	0x24;
		public const int VK_LEFT	  =	0x25;
		public const int VK_UP		=	0x26;
		public const int VK_RIGHT	 =	0x27;
		public const int VK_DOWN	  =	0x28;
		public const int VK_SELECT	=	0x29;
		public const int VK_PRINT	 =	0x2A;
		public const int VK_EXECUTE   =	0x2B;
		public const int VK_SNAPSHOT  =	0x2C;
		public const int VK_INSERT	=	0x2D;
		public const int VK_DELETE	=	0x2E;
		public const int VK_HELP	  =	0x2F;
		public const int VK_LWIN	  =	0x5B;
		public const int VK_RWIN	  =	0x5C;
		public const int VK_APPS	  =	0x5D;
		public const int VK_SLEEP	 =	0x5F;
		public const int VK_NUMPAD0   =	0x60;
		public const int VK_NUMPAD1   =	0x61;
		public const int VK_NUMPAD2   =	0x62;
		public const int VK_NUMPAD3   =	0x63;
		public const int VK_NUMPAD4   =	0x64;
		public const int VK_NUMPAD5   =	0x65;
		public const int VK_NUMPAD6   =	0x66;
		public const int VK_NUMPAD7   =	0x67;
		public const int VK_NUMPAD8   =	0x68;
		public const int VK_NUMPAD9   =	0x69;
		public const int VK_MULTIPLY  =	0x6A;
		public const int VK_ADD	   =	0x6B;
		public const int VK_SEPARATOR =	0x6C;
		public const int VK_SUBTRACT  =	0x6D;
		public const int VK_DECIMAL   =	0x6E;
		public const int VK_DIVIDE	=	0x6F;
		public const int VK_F1		=	0x70;
		public const int VK_F2		=	0x71;
		public const int VK_F3		=	0x72;
		public const int VK_F4		=	0x73;
		public const int VK_F5		=	0x74;
		public const int VK_F6		=	0x75;
		public const int VK_F7		=	0x76;
		public const int VK_F8		=	0x77;
		public const int VK_F9		=	0x78;
		public const int VK_F10	   =	0x79;
		public const int VK_F11	   =	0x7A;
		public const int VK_F12	   =	0x7B;
		public const int VK_F13	   =	0x7C;
		public const int VK_F14	   =	0x7D;
		public const int VK_F15	   =	0x7E;
		public const int VK_F16	   =	0x7F;
		public const int VK_F17	   =	0x80;
		public const int VK_F18	   =	0x81;
		public const int VK_F19	   =	0x82;
		public const int VK_F20	   =	0x83;
		public const int VK_F21	   =	0x84;
		public const int VK_F22	   =	0x85;
		public const int VK_F23	   =	0x86;
		public const int VK_F24	   =	0x87;
		public const int VK_NUMLOCK   =	0x90;
		public const int VK_SCROLL	=	0x91;

		#endregion Virtual Keys Constants

		[DllImport("user32.dll", CharSet=CharSet.Auto)]
		private static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
		[DllImport("user32.dll", CharSet=CharSet.Auto)] // used for button-down & button-up
		private static extern int PostMessage(int hWnd, int msg, int wParam, IntPtr lParam);
		[DllImport("user32.dll", CharSet=CharSet.Auto)]
		private static extern bool SetForegroundWindow(IntPtr hWnd);

		public void SetFocus() 
		{
			SetForegroundWindow(new IntPtr(this.handle));
		}

		public void SendLeftButtonDown(int x,int y) 
		{
			PostMessage(handle,WM_LBUTTONDOWN,0,new IntPtr(y * 0x10000 + x));
		}

		public void SendLeftButtonUp(int x,int y) 
		{
			PostMessage(handle,WM_LBUTTONUP,0,new IntPtr(y * 0x10000 + x));
		}

		public void SendLeftButtondblclick(int x,int y) 
		{
			PostMessage(handle,WM_LBUTTONDBLCLK,0,new IntPtr(y * 0x10000 + x));
		}

		public void SendRightButtonDown(int x,int y) 
		{
			PostMessage(handle,WM_RBUTTONDOWN,0,new IntPtr(y * 0x10000 + x));
		}

		public void SendRightButtonUp(int x,int y) 
		{
			PostMessage(handle,WM_RBUTTONUP,0,new IntPtr(y * 0x10000 + x));
		}

		public void SendRightButtondblclick(int x,int y) 
		{
			PostMessage(handle,WM_RBUTTONDBLCLK,0,new IntPtr(y * 0x10000 + x));
		}
			
		public void SendMouseMove(int x,int y) 
		{
			PostMessage(handle,WM_MOUSEMOVE,0,new IntPtr(y * 0x10000 + x));
		}

		public void SendKeyDown(int key) 
		{
			PostMessage(handle,WM_KEYDOWN,key,IntPtr.Zero);
		}
			
		public void SendKeyUp(int key) 
		{
			PostMessage(handle,WM_KEYUP,key,new IntPtr(1));
		}

		public void SendChar(char c)
		{
			SendMessage(handle, WM_CHAR, c, IntPtr.Zero);
		}
			
		public void SendString(string s)
		{
			foreach (char c in s) SendChar(c);
		}



Any help will be greatly appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Sending Mouse and Keyboard input from Code

#2 Vegter   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Sending Mouse and Keyboard input from Code

Posted 02 October 2006 - 11:24 PM

Well, it seems like I have everyone stumped. The delphi application that does what I need uses the Win32.dll, not user32.dll as this application does. Problem is, I obvioustly can't put a reference to the dll, and I can't figure out which methods the dll supports. Does anyone have any experience with this?
Was This Post Helpful? 1

#3 Vegter   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Sending Mouse and Keyboard input from Code

Posted 03 October 2006 - 11:21 PM

Ok, I think I may have figured it out now. The following class will support mouse clicks and moving the mouse. Still have to do keyboard input tho.

public class SendInputHelper
	{
		#region External Dll Calls
		[DllImport("user32.dll")]
		private static extern bool SetCursorPos(int X, int Y);  

		[DllImport("user32.dll")]
		private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,  int dwExtraInfo);

		[Flags]
		private enum MouseEventFlags
		{
			LEFTDOWN   = 0x00000002,
			LEFTUP	 = 0x00000004,
			MIDDLEDOWN = 0x00000020,
			MIDDLEUP   = 0x00000040,
			MOVE	   = 0x00000001,
			ABSOLUTE   = 0x00008000,
			RIGHTDOWN  = 0x00000008,
			RIGHTUP	= 0x00000010
		} 
		#endregion

		#region Constructors and Destructors
		public SendInputHelper()
		{
			throw new NotImplementedException("All methods in this class are static.");
		}
		#endregion

		#region Mouse Methods
		public static void MouseClick(MouseButtons button)
		{
			switch(button)
			{
				case MouseButtons.Left : 
					mouse_event((uint)MouseEventFlags.LEFTDOWN,0,0,0,0);
					mouse_event((uint)MouseEventFlags.LEFTUP,0,0,0,0);
					break;
				case MouseButtons.Right :
					mouse_event((uint)MouseEventFlags.RIGHTDOWN,0,0,0,0);
					mouse_event((uint)MouseEventFlags.RIGHTUP,0,0,0,0);
					break;
			}
		}

		public static void MoveMouse(int x, int y)
		{
			Point p = Cursor.Position;
			p.X += x;
			p.Y += y;
			Cursor.Position = p;
		}

		public static void MoveMouseTo(int x, int y)
		{
			Cursor.Position = new Point(x,y);
		}
		#endregion
	}


Was This Post Helpful? 1

Page 1 of 1