Simulate KeyPress in C# to Game

1

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
  • InputSimulator - Complete waste of time
  • SendInput - Have some problems with this as well. 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. Also couldn't figure out the value for wParam if i wanna hold down the key for a number of seconds. It is a 32bit Hex value in which seems the last 16 are repetition number.
  • SendMessage - See 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. COM, ref, or anything.

Having problems with 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

I'm trying to send a key to another instance or handle. C++ or C# anything that works is fine, too.

c#
c++
keyboard
keypress
simulate
asked on Stack Overflow Jul 22, 2012 by Mandah Mr. • edited Jul 22, 2012 by chris

1 Answer

1

Looks like you are trying to load a 32bit dll in a 64 bit solution, so please use the 64bit dll

answered on Stack Overflow Jul 22, 2012 by HatSoft

User contributions licensed under CC BY-SA 3.0