0

I'm trying to remap some keys for a game (Elsword).

First of all I use this code to simulate the pressed key:

#define WINVER 0x0500

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "dinput.h"

int main()
{
    HWND hWndMain = FindWindow(0, "Elsword");
    SetForegroundWindow(hWndMain);

    Sleep(3000);

    INPUT input[2];

    input[0].type=INPUT_KEYBOARD;
    input[0].ki.wScan = DIK_V; // direct-input scancode for key '1'
    input[0].ki.dwFlags=KEYEVENTF_SCANCODE;

    input[1].type=INPUT_KEYBOARD;
    input[1].ki.wScan = DIK_V; // direct-input scancode for key '1'
    input[1].ki.dwFlags=KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;

    SendInput(2,input,sizeof(INPUT));

    Sleep(1000);
    return 0;
}

In all other applications it WORKS fine, but in the game window it does nothing...

xXDraklordXx
  • 45
  • 1
  • 7

1 Answers1

2

Note that hooking Direct3D will only give you access to graphics-related functions, NOT input-related ones. To hook a game which uses Direct3D, you would either need to hook DirectInput (if the game uses it), or use SentInput or PostMessage. Most games these days use standard Windows messages for the mouse, and either DirectInput or their own custom keyboard implementation. Note also that the SendInput API is system-wide, and requires the proper target window to have the focus. Also, some games create an additional window to act as an invisible layer over the client area of the D3D window, causing confusion about which window handle is the correct one to send input messages to.

To check if your game uses Windows messages for input, open Spy++ and run your game in windowed mode. Click on the 'Processes' button, right-click the process for your game and select 'Messages'. Then click the 'Logging Options' button and switch over to the 'Messages' tab. From here, you could select 'Keyboard' and 'Mouse', or hold the CTRL key down and select/deselect individual messages from the list. After that, hit OK and test your keyboard and mouse in the target application. If you see any mouse-related messages, then you know SendInput, PostMessage, SetWindowsHookEx and possibly mouse_event() will work for the mouse. If you see any keyboard-related messages, then you know SendInput, PostMessage, SetWindowsHookEx and possibly keyb_event() will work for the keyboard.

If you don't see any messages for the keyboard, then the most likely action to take would be to check if the process ever loads dinput.dll or dinput8.dll, and install a hook if it does (google "directinput hooking"). If it doesn't, then you could check each of the loaded modules in Dependency Walker, IDA or OllyDbg to see if they have implemented their own keyboard handler, but things get rather complicated at this point and would require a good amount of ASM knowledge and reverse-engineering techniques. However, if you are familiar with developing drivers, then this might help: SendInput Not working for Games

Community
  • 1
  • 1
RectangleEquals
  • 1,825
  • 2
  • 25
  • 44