views:

1796

answers:

6

Hi,

I wanna do a macro program for a game. But there is a problem with sending keys to only game application (game window). I am using keybd_event API for sending keys to game window. But I only want to send keys to the game window, not to explorer or any opened window while my macro program is running. When I changed windows its still sending keys. I tried to use Interaction.App with Visual Basic.dll reference. But Interaction.App only Focus the game window.

I couldn't find anything about my problem. Can anyone help me? Thanx

+1  A: 

Are you retrieving the handle of the window all the time, or are you remembering it?

If you use the FindWindow() API, you can simply store the Handle and use the SendMessage API to send key/mouse events manually.

Tom Anderson
A: 

If you want to communicate with a game, you typically will have to deal with DirectInput, not the normal keyboard API's.

Lasse V. Karlsen
Completely agree with this, but he seems to have it already working with the keyboard event api, and only has problems when the window loses focus.
Tom Anderson
Not necessarily - the normal keyboard API's are lower level than DirectInput, and while it may be simpler to use DirecctInput, both ways should work.
configurator
+1  A: 

FindWindow API:
http://www.pinvoke.net/default.aspx/user32.FindWindowEx

SendMessage API:
http://www.pinvoke.net/default.aspx/user32/SendMessage.html

VB

Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101

C#

private static int WM_KEYDOWN = 0x100
private static int WM_KEYUP = 0x101
Tom Anderson
A: 
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);
Tom Anderson
+1  A: 

i fixed my problem. in this field ;

PostMessage(hWnd, WM_KEYDOWN, key, {have to give lParam of the key});

otherwise it does not work.And we can control of ChildWindow Class with Spy++ tool of Microsoft.

Thanks everyone for helping.

A: 

How do you use direct input so send a key to game window?

email me at [email protected] plaese let me know how to do this nothing else work.. not sendkeys or sendmessage only direct input will