我写了一个模拟游戏中按键的程序,它等到我按'v'然后模拟
- open console (p)
- ctrl + a
- copy to clipboard some stuff
- ctrl + v
- enter
- close console (p)
奇怪的是,当我在Visual Studio Release模式下点击F5时,它的效果与预期的一样,但当我从Release文件夹中双击.exe时,程序会出错,它仍会打开并正确呈现窗口,但是它似乎忽略了除了输入击键之外的一切(所以我在按下'v'后在游戏中的聊天模式中),对于输入注入我使用了一个快速而又脏的C ++库,这里是代码
#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);
}
我像这样使用C#和PInvoke中的函数
[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);
我会很高兴有任何帮助!