2

I'd like to click in a game's button. I've tried with several ways, SendInput, and MouseEvent are some of them. Nevertheless I always got the same result, it doesn't work.

This is the original button.

enter image description here

And this is the button once I click on it with SendInput or MouseEvent

enter image description here

The color changes, but it doesn't work at all. (I also tried with leftClick, double LeftClick, and leftClickUp/Down.) My guess is that it is just supposed to work if the click isn't virtual? I'm not sure at all, since I don't have much idea about it.

Any idea?

Little update: As I've mentioned I have tried with SendInput, MouseEvent, InputSimulator and so on, the problem is always the same, while it works out of that game, it doesn't with that button. I'm pretty sure it could be because the game detect it's a virtual click simulation.

Since some fellas asked for code. (I'll repeat, this doesn't look a problem in the code tho...)

This one is for InputSimulator.


InputSimulator sim = new InputSimulator();
sim.Mouse.LeftButtonDoubleClick();

This one is using MouseEvent


[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

private const int MOUSEEVENT_LEFTDOWN = 0x02;
private const int MOUSEEVENT_LEFTUP = 0x04;
private const int MOUSEEVENT_MIDDLEDOWN = 0x20;
private const int MOUSEEVENT_MIDDLEUP = 0x40;
private const int MOUSEEVENT_RIGHTDOWN = 0x08;
private const int MOUSEEVENT_RIGHTUP = 0x10;

static void Click(){
   mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
   mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
}

private void timer1_Tick(object sender, EventArgs e) {
   Click();
}

This one is using SendInput. (MouseSimulator is a static class using SendInput.)


MouseSimulator.ClickLeftMouseButton();

This is the class for the MouseSimulator.

using System;
using System.Runtime.InteropServices;

public class MouseSimulator
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

    [StructLayout(LayoutKind.Sequential)]
    struct INPUT
    {
        public SendInputEventType type;
        public MouseKeybdhardwareInputUnion mkhi;
    }
    [StructLayout(LayoutKind.Explicit)]
    struct MouseKeybdhardwareInputUnion
    {
        [FieldOffset(0)]
        public MouseInputData mi;

        [FieldOffset(0)]
        public KEYBDINPUT ki;

        [FieldOffset(0)]
        public HARDWAREINPUT hi;
    }
    [StructLayout(LayoutKind.Sequential)]
    struct KEYBDINPUT
    {
        public ushort wVk;
        public ushort wScan;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }
    [StructLayout(LayoutKind.Sequential)]
    struct HARDWAREINPUT
    {
        public int uMsg;
        public short wParamL;
        public short wParamH;
    }
    struct MouseInputData
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public MouseEventFlags dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }
    [Flags]
    enum MouseEventFlags : uint
    {
        MOUSEEVENTF_MOVE = 0x0001,
        MOUSEEVENTF_LEFTDOWN = 0x0002,
        MOUSEEVENTF_LEFTUP = 0x0004,
        MOUSEEVENTF_RIGHTDOWN = 0x0008,
        MOUSEEVENTF_RIGHTUP = 0x0010,
        MOUSEEVENTF_MIDDLEDOWN = 0x0020,
        MOUSEEVENTF_MIDDLEUP = 0x0040,
        MOUSEEVENTF_XDOWN = 0x0080,
        MOUSEEVENTF_XUP = 0x0100,
        MOUSEEVENTF_WHEEL = 0x0800,
        MOUSEEVENTF_VIRTUALDESK = 0x4000,
        MOUSEEVENTF_ABSOLUTE = 0x8000
    }
    enum SendInputEventType : int
    {
        InputMouse,
        InputKeyboard,
        InputHardware
    }

    public static void ClickLeftMouseButton()
    {
        INPUT mouseDownInput = new INPUT();
        mouseDownInput.type = SendInputEventType.InputMouse;
        mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
        SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));

        INPUT mouseUpInput = new INPUT();
        mouseUpInput.type = SendInputEventType.InputMouse;
        mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
        SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
    }
    public static void ClickRightMouseButton()
    {
        INPUT mouseDownInput = new INPUT();
        mouseDownInput.type = SendInputEventType.InputMouse;
        mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN;
        SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));

        INPUT mouseUpInput = new INPUT();
        mouseUpInput.type = SendInputEventType.InputMouse;
        mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP;
        SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
    }
}

All of them work well out of the game, inside they just work as I described above.

Since this is happening with all buttons in the game, this is a gif showing what's happening versus what's is expecting.

This is the expected behavior.

https://gyazo.com/ffd6af281414c5b0b10e19cf7a27823d

Nevertheless this is what's happening. (You can see that the color change and it looks like if it was pressed but it clearly doesn't)

https://gyazo.com/20ebb3bc360a4a5bccbe3ea5207d201b

Sharki
  • 404
  • 4
  • 23
  • Does this answer your question? [Directing mouse events \[DllImport("user32.dll")\] click, double click](https://stackoverflow.com/questions/8739523/directing-mouse-events-dllimportuser32-dll-click-double-click) – T.Schwarz Aug 20 '21 at 08:30
  • @OlivierRogier Yeah I also tried InputSimulator and got the same problem. `InputSimulator sim = new InputSimulator(); sim.Mouse.LeftButtonDoubleClick();` the code itself doesn't give any error, it works on other applications perfectly, but it doesn't with that button. (.Net framework WinForms) – Sharki Aug 20 '21 at 08:32
  • @T.Schwarz As I've mentioned I tried with MouseEvent before and got the same problem. Those clicks works perfectly out of that game, that's why I'm looking for some alternative since I'm pretty sure it's due to they detect the click as a virtual one – Sharki Aug 20 '21 at 08:32
  • @OlivierRogier I'm afraid I can't tell more, since I don't know what's happening neither. That's why I'm asking here. Also I'm telling you this is not about code, since the code clearly works. It's more about a game that's likely able to detect a virtual click, an as such I'm looking for an alternative for that. – Sharki Aug 20 '21 at 08:42
  • 1
    @OlivierRogier I've added some code. – Sharki Aug 20 '21 at 08:53
  • @OlivierRogier Being honest the button just need one click to work, (The first code is using double click because I was testing with other kind of clicks, I've tried with one single click, double click, and using up/down click, but still the button doesn't work) as you can see in the last image, it looks like the button is pressed but for sure it's not, or least it's not working (I don't know the reason why). – Sharki Aug 20 '21 at 09:01
  • @OlivierRogier I'm gonna update the post with a gif in some minutes, maybe is more clear like that. Sorry, and I'm also sorry for my bad english. – Sharki Aug 20 '21 at 09:05
  • 1
    @OlivierRogier Updated, I hope now it looks a little bit clear. – Sharki Aug 20 '21 at 09:16
  • 1
    Finally I solved uising a wrapper of Interception, using a driver to make believe the game that's a real mouse and not a virtual one. Here's the wrapper link. https://github.com/jasonpang/Interceptor – Sharki Aug 20 '21 at 10:38

1 Answers1

-3

Your question has a really low quality. The design of the button is´t relevant, your code would be. But I´ll take a guess anyway.

Well. It looks like the click got registered. Could it be that your script presses the mousebutton and never releases it? Many UI´s wont trigger at the pressing but on the release of the button. I don´t know much about C# but in Powershell you need to tell the script to PRESS and to RELEASE the button seperatly.