Home > Blockchain >  Capture the keyboard in the game?
Capture the keyboard in the game?

Time:10-26

I am trying to use unity3d to do a button monitoring function, but now I have encountered some difficulties. My first version of the method is to do it through hook, everything is ok, until I encounter League of Legends, on the interface of this game, my keyboard cannot be captured by hook. The main code like this:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelHookProc lpfn, IntPtr hMod, uint dwThreadId);

public delegate IntPtr LowLevelHookProc(int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);

Next, I used the DirectX input component to monitor. Similarly, there was no response on the League of Legends interface. By the way, I am using SharpDX.DirectInput. The code like this:

Keyboard keyboard;

void Start()
{
    var directInput = new DirectInput();
    keyboard = new Keyboard(directInput);
    keyboard.Acquire();
}

void Update()
{
    KeyboardState cur = keyboard.GetCurrentState();
    if (cur.PressedKeys.Count != 0)
    {
        Debug.LogError(cur.ToString());
    }
}

Finally I used GetAsyncKeyState to capture, the same does not take effect.

So does anyone know what's going on? This kind of imagination should not only lie in League of Legends, but also in other games. I haven't had time to test it. If you have any ideas or suggestions, I would be very grateful!

CodePudding user response:

Solved the question in comment section:

Some high budget games have anti-cheat systems. You have to run your program as administrator in order to do the button monitoring.

  • Related