Question

I want to send keyboard events to another application, more specifically, a gameboy emulator.

I have tried using many different methods of the User32.dll. None worked as I want.

I have the following window:

enter image description here

I tried sending data with the code:

        var p = Process.GetProcessesByName("VisualBoyAdvance-M").FirstOrDefault();
        if (p != null)
        {
            IntPtr h = p.MainWindowHandle;
            SetForegroundWindow(h);
        }

        while (true)
        {
            SendKeys.SendWait("{DOWN}");
            Console.WriteLine("DOWN");
            Thread.Sleep(3000);
        }

The "down" is a command to the down on the game. When I do it, the emulator should move the arrow down. When I run the code above, the emulator becomes the active window. But the only thing that moves are the menu items, if I click on one. The selection moves up and down. Example:

enter image description here

I have also tried (with no success):

        var p = Process.GetProcessesByName("VisualBoyAdvance-M").FirstOrDefault();
        if (p == null) return;

        IntPtr Hwnd = p.MainWindowHandle;
        SetForegroundWindow(Hwnd);

        while (true)
        {
            SendMessage(Hwnd, WindowsAPI.WM_KEYDOWN, uint.MinValue, int.MinValue);
            Thread.Sleep(3000);
            SendMessage(Hwnd, WindowsAPI.WM_KEYUP, uint.MinValue, int.MinValue);
            Thread.Sleep(3000);
            SendMessage(Hwnd, WindowsAPI.WM_KEYDOWN, uint.MinValue, int.MinValue);
            Thread.Sleep(3000);
        }

I have no idea how to select the window I need and start sending the commands to it.

Any suggestions?

No correct solution

OTHER TIPS

Try using WINAPI SendMessage function to send WM_CHAR or WM_KEYDOWN/WM_KEYUP events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top