-
January 3rd, 2009, 04:41 AM
#1
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);
-
January 3rd, 2009, 05:14 AM
#2
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
Last edited by Jensecj; January 3rd, 2009 at 07:28 AM.
-
January 3rd, 2009, 05:58 AM
#3
Re: Send Key Strokes to a specific window handle.
this code is not working pal
-
January 3rd, 2009, 06:49 AM
#4
Re: Send Key Strokes to a specific window handle.
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.
-
January 3rd, 2009, 07:04 AM
#5
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.
-
January 3rd, 2009, 08:35 PM
#6
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
Last edited by Traps; January 3rd, 2009 at 08:42 PM.
-
April 30th, 2009, 05:12 AM
#7
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?
-
April 30th, 2009, 06:15 AM
#8
Re: Send Key Strokes to a specific window handle.
Better ask and make a rhyme
than search dead threads for a long time.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|