CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Post 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);

  2. #2
    Join Date
    Nov 2008
    Posts
    15

    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.

  3. #3
    Join Date
    Jan 2009
    Posts
    3

    Re: Send Key Strokes to a specific window handle.

    this code is not working pal

  4. #4
    Join Date
    Nov 2008
    Posts
    15

    Re: Send Key Strokes to a specific window handle.

    Quote Originally Posted by thorny View Post
    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.

  5. #5
    Join Date
    Jan 2009
    Posts
    3

    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.

  6. #6
    Join Date
    Mar 2007
    Posts
    274

    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.

  7. #7
    Join Date
    Apr 2009
    Posts
    1

    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?

  8. #8
    Join Date
    Dec 2002
    Location
    at home and at office :D
    Posts
    126

    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
  •  





Click Here to Expand Forum to Full Width

Featured