简体   繁体   English

在C#中模拟KeyPress进行游戏

[英]Simulate KeyPress in C# to Game

I'm trying to simulate a keypress into another window, which is a game. 我正在尝试在另一个窗口中模拟按键,这是一个游戏。 The things that don't work are: 无效的是:

  • SendKey - Send the input as strings like some text or something but doesn't go for ENTER TAB or other critical keys SendKey-以字符串形式发送输入内容,例如一些文本或其他内容,但不用于ENTER TAB或其他关键键
  • InputSimulator - Complete waste of time InputSimulator-完全浪费时间
  • SendInput - Have some problems with this as well. SendInput-也有一些问题。 It doesn't compile, and I've never used it before 它不会编译,我以前从未使用过
  • PostMessage - it seems these 2 are pretty much the same and again they do send text but again no other keys such as ENTER TAB or other. PostMessage-似乎这2个几乎相同,并且它们再次发送文本,但是再没有其他键,例如ENTER TAB或其他。 Also couldn't figure out the value for wParam if i wanna hold down the key for a number of seconds. 如果我想按住键几秒钟,也无法弄清楚wParam的值。 It is a 32bit Hex value in which seems the last 16 are repetition number. 这是一个32位十六进制值,其中似乎最后16个是重复编号。
  • SendMessage - See PostMessage SendMessage-请参阅PostMessage
  • AUTOIT or AHK it seems that they have troubles being incorporated into C# if anyone knows how to do that, it would help immensely, this meaning I'd be able to write AHK script in the C# .cs file so it would compile. 如果有人知道如何将AUTOIT或AHK合并到C#中,似乎会遇到麻烦,这将极大地帮助您,这意味着我可以在C#.cs文件中编写AHK脚本,以便进行编译。 COM, ref, or anything. COM,引用或其他任何内容。

Having problems with DirectInput DirectInput遇到问题

public void Test_KeyDown()
{
        INPUT[] InputData = new INPUT[2];
        Key ScanCode =  Microsoft.DirectX.DirectInput.Key.W;

        InputData[0].type = 1; //INPUT_KEYBOARD
        InputData[0].ki.wScan = (short)VirtualKeyCode.NUMPAD2 ; //ScanCode;
        InputData[0].ki.dwFlags = (int)KEYEVENTF.SCANCODE;

        InputData[1].type = 1; //INPUT_KEYBOARD
        InputData[1].ki.wScan = (short)VKeys.VK_W; 
        InputData[1].ki.dwFlags = (int)(KEYEVENTF.KEYUP | KEYEVENTF.UNICODE);

        // send keydown
        if (SendInput(2, InputData, Marshal.SizeOf(InputData[1])) == 0)
        {
            System.Diagnostics.Debug.WriteLine("SendInput failed with code: " +
            Marshal.GetLastWin32Error().ToString());
        }
}

It gives this error: 它给出了这个错误:

Could not load file or assembly 'Microsoft.DirectX.DirectInput.dll' or one of  
its dependencies.  is not a valid Win32 application.  
(Exception from HRESULT: 0x800700C1)

I've referenced it from DirectX SDK tool kit 2007 我已经从DirectX SDK工具包2007中引用了它

I'm trying to send a key to another instance or handle. 我正在尝试将密钥发送到另一个实例或句柄。 C++ or C# anything that works is fine, too. C ++或C#任何可行的方法也都可以。

看起来您正在尝试在64位解决方案中加载32位dll,因此请使用64位dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM