1

I wrote a program which simulates keystrokes in a game, it waits till i press 'v' and then simulates

- open console (p)
- ctrl + a
- copy to clipboard some stuff
- ctrl + v
- enter
- close console (p)

The weird thing is that it works just as expected when I hit F5 in Visual Studio Release mode, but when I double click the .exe from the Release Folder, the program bugs, it still opens and renders the windows correctly, but it just seems to ignore everything except the enter keystroke (so im in chat mode in the game after pressing 'v'), for input injection I use a quick and dirty C++ library, here is the code

#include <stdlib.h>
#include <Windows.h>

extern "C"
{

__declspec(dllexport ) void InjectKey(int c, int flags)
{
    char ch = (char)c;
    INPUT key;
    memset(&key, 0, sizeof(INPUT));
    key.type = INPUT_KEYBOARD;
    key.ki.dwExtraInfo = GetMessageExtraInfo();
    key.ki.wScan = static_cast<WORD>(MapVirtualKeyEx(VkKeyScan(ch), MAPVK_VK_TO_VSC, GetKeyboardLayout(0)));
key.ki.dwFlags = KEYEVENTF_SCANCODE | flags;

SendInput(1, &key, sizeof(INPUT));
}

__declspec(dllexport ) void InjectKeyDirect(int code, int flags)
{
    INPUT input = { 0 };
    input.type = INPUT_KEYBOARD;
    input.ki.dwFlags = KEYEVENTF_SCANCODE | flags;
    input.ki.wScan = MapVirtualKeyEx(code, MAPVK_VK_TO_VSC, GetKeyboardLayout(0));
    SendInput(1, &input, sizeof(INPUT));
}

__declspec(dllexport ) void InjectKeyPress(int code, int flags)
{
    InjectKey(code, flags);
    InjectKey(code, KEYEVENTF_KEYUP | flags);
}

__declspec(dllexport ) void InjectKeyPressDirect(int code, int flags)
{
    InjectKeyDirect(code, flags);
    InjectKeyDirect(code, KEYEVENTF_KEYUP | flags);
}

I use the functions from C# with PInvoke like this

            [DllImport("InputInjector.dll", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
    public static extern void InjectKey(int c, int flags = 0);[DllImport("InputInjector.dll", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
    public static extern void InjectKey(int c, int flags = 0);

I would be glad about any help!

4

0 回答 0