Send Key Strokes to a specific window handle.
Hi,
i wanna do a bot program for a game.This program send key strokes to Game Application.But i got a problem.I only wanna send key to Game Application Window Handle not another active window.I write a class for this but i havent get worked it.Its sending keys only notepad not to game or any other window.Can ya help me for this ? Iam sending ya codes below ; and tell me where is my problem. thanx for ur help.
class SendKeySample
{
private static Int32 WM_KEYDOWN = 0x100;
private static Int32 WM_KEYUP = 0x101;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public static IntPtr FindWindow(string windowName)
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.MainWindowHandle != IntPtr.Zero && p.MainWindowTitle.ToLower() == windowName.ToLower())
return p.MainWindowHandle;
}
return IntPtr.Zero;
}
public static IntPtr FindWindow(IntPtr parent, string childClassName)
{
return FindWindowEx(parent, IntPtr.Zero, childClassName, string.Empty);
}
public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key)
{
PostMessage(hWnd, WM_KEYDOWN, key, 0);
}
}
Calling Code ;
var hWnd = SendKeySample.FindWindow("Untitled - Notepad");
var editBox = SendKeySample.FindWindow(hWnd, "edit");
SendKeySample.SendKey(editBox, Keys.A);
Re: Send Key Strokes to a specific window handle.
a simpler way would probably be
Code:
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(
IntPtr hWnd,
uint msg,
int wParam,
int lParam
);
const uint WM_KEYDOWN = 0x100;
const int WM_a = 0x41;
const int WM_b = 0x42;
const int WM_c = 0x43;
static void Main(string[] args)
{
//using Process.GetProcessesByName to get the handle we want
Process[] p = Process.GetProcessesByName("notepad");
IntPtr pHandle = p[0].MainWindowHandle;
//will write "abc" in the open Notepad window
PostMessage(pHandle, WM_KEYDOWN, WM_a, 0);
PostMessage(pHandle, WM_KEYDOWN, WM_b, 0);
PostMessage(pHandle, WM_KEYDOWN, WM_c, 0);
}
and for a list of the keys just search google for virtual key codes
Re: Send Key Strokes to a specific window handle.
this code is not working pal :(
Re: Send Key Strokes to a specific window handle.
Quote:
Originally Posted by
thorny
this code is not working pal :(
mind posting the error?
also, you should probably look into the security features of what ever you're trying to "bot", some games have a lot of restrictions and blocks to make sure people play legit.
Re: Send Key Strokes to a specific window handle.
no i tried it in NOTEPAD.It does not sendin any key to Notepad.No error no activity from Program.
Re: Send Key Strokes to a specific window handle.
Hi,
This is a lame way to do it, but it works :)
Code:
Microsoft.VisualBasic.Interaction.AppActivate("Untitled - Notepad");
SendKeys.Send ("This is a test.");
Just make sure you add a reference to your project Microsoft.VisualBasic
Another way is to use the windows api's to findwindow, and setforgroundwindow. Then sendkeys will go there. Take a look at this project http://www.codeproject.com/KB/cs/SendKeys.aspx
Re: Send Key Strokes to a specific window handle.
This might be a newbe follow-up question:
I am trying to do exactly the same thing, but from a service application without GUI.
Seems to me that SendKeys only work from within Form object. Is there a way around this?
Re: Send Key Strokes to a specific window handle.