Windows Develop Bookmark and Share   
 index > Windows Forms General > Send Key Strokes to a specific window handle.
 

Send Key Strokes to a specific window handle.

Hi iwanna 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's 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);

System Administrator of C.B.U.
İbrahim AKGÜN  Saturday, January 03, 2009 10:20 AM
This will send "A" to notepad:

[DllImport("User32.dll")]

public static extern Int32 SendMessage(

IntPtr hWnd,

int Msg,

int wParam,

[MarshalAs(UnmanagedType.LPStr)] string lParam);

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,

string lpszWindow);



private void Form1_Load(object sender, EventArgs e)

{

var notepad = FindWindow("notepad", null);

var edit = FindWindowEx(notepad, IntPtr.Zero, "edit", null);

SendMessage(edit, 0x000C, 0, "A");

}



I need a job!
cablehead  Sunday, January 04, 2009 12:16 AM
This will send "A" to notepad:

[DllImport("User32.dll")]

public static extern Int32 SendMessage(

IntPtr hWnd,

int Msg,

int wParam,

[MarshalAs(UnmanagedType.LPStr)] string lParam);

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,

string lpszWindow);



private void Form1_Load(object sender, EventArgs e)

{

var notepad = FindWindow("notepad", null);

var edit = FindWindowEx(notepad, IntPtr.Zero, "edit", null);

SendMessage(edit, 0x000C, 0, "A");

}



I need a job!
cablehead  Sunday, January 04, 2009 12:16 AM
i found solution.thank ya.
System Administrator of C.B.U.
İbrahim AKGÜN  Thursday, January 08, 2009 3:48 PM

You can use google to search for other answers

Custom Search

More Threads

• How to draw multiple gradient colors?
• Does winform runs on Single Thread or Multiple Threads
• OnMouseWheel Code example
• GridView Related Question
• Show/Hide a form with a .gif image
• Delete Key DataGrid
• PDA Hardware Details
• SystemColor.InactiveCaptionText do not work with Vista
• Number by "," separater in maskedtextbox.
• KeyBoard