Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello community,
 
I want to send a Key (in this case 'T') to a game.
This game (Battlefield 3) gets it's Keyinformation from DirectInput.
 
So I need additional classes/ sources for DirectX.
 
I took SlimDX, XNA is aviable as well but I only tested SlimDX.
 

In the internet people wrote, it would work with DirectX Keycodes (when use with SendInput). Unfortuanly, this wont work.
 
The Virtualkey for T is 0x54
In DirectX case, it's 0x14
 
So when I'm ingame, and I start my program, my Capslock is active.
 

I don't get it. :s
The World Wide Web didn't help me out (half aviable code or not working code).
 

Maybe someone can help me?
Would be awesome!
 

Thanks in advance.
Posted 7-Nov-11 7:23am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Can you supply a usage example, 10x!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Alright, somehow I managed to get this working.
 
Because I think I am not the only one had problems with this topic I gotta answer my own question.
 

First thing we have to do is to import the "user32.dll" because .NET doesn't support DirectInput (atleast that's what I understood, correct me if not).
 
So we do..
 
using System.Runtime.InteropServices;   //Needed to import your .dll
 

After that line we can import our dll..
[DllImport("user32.dll")]
        static extern UInt32 SendInput(UInt32 nInputs,     [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);
 

Now we need to declare some variables to get out program working.
Because we want to emulate a Key, we only need to use the following code:
[StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            public short wVk;      //Virtual KeyCode (not needed here)
            public short wScan;    //Directx Keycode 
            public int dwFlags;    //This tells you what is use (Keyup, Keydown..)
            public int time;       
            public IntPtr dwExtraInfo;
        }
 

Now we need to declare the 'INPUT' variable..
 [StructLayout(LayoutKind.Explicit)]
        struct INPUT
        {
            [FieldOffset(0)]
            public int type;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
        }
 

After we initialised our variables we can send some Keys to DirectX Games.
public void Send_Key(short Keycode, int KeyUporDown)
      {
          INPUT[] InputData = new INPUT[1];
 
          InputData[0].type = 1;                 
          InputData[0].ki.wScan = Keycode;       
          InputData[0].ki.dwFlags = KeyUporDown;  
          InputData[0].ki.time = 0;
          InputData[0].ki.dwExtraInfo = IntPtr.Zero;
 
          SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT)));
      }
 
InputData[0].ki.wScan => Means out Keycode we like to send.
If we want to send the Key T, we have to type in 0x14.
0x14 means T in DirectX. Lists with KeyCodes are easy findable in the net.
 
InputData[0].ki.dwFlags => Means our Flag, we could type 0x0008 (for Sendkey)
or create a constant variable. What you prefer.
 

 
Our final code could look like the following class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace DirectInput
{
    class cDirectInpit
    {
        [DllImport("user32.dll")]
        static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);
 
        [StructLayout(LayoutKind.Sequential)]
        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public int mouseData;
            public int dwFlags;
            public int time;
            public IntPtr dwExtraInfo;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            public short wVk;
            public short wScan;
            public int dwFlags;
            public int time;
            public IntPtr dwExtraInfo;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }
 
        [StructLayout(LayoutKind.Explicit)]
        struct INPUT
        {
            [FieldOffset(0)]
            public int type;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }
 
        const int KEYEVENTF_EXTENDEDKEY = 0x0001;
        const int KEYEVENTF_KEYUP = 0x0002;
        const int KEYEVENTF_UNICODE = 0x0004;
        const int KEYEVENTF_SCANCODE = 0x0008;
 

        public void Send_Key(short Keycode, int KeyUporDown)
        {
            INPUT[] InputData = new INPUT[1];
 
            InputData[0].type = 1;
            InputData[0].ki.wScan = Keycode;
            InputData[0].ki.dwFlags = KeyUporDown;
            InputData[0].ki.time = 0;
            InputData[0].ki.dwExtraInfo = IntPtr.Zero;
 
            SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT)));
        }
 
    }
}
  Permalink  
v2
Comments
metehan_1 at 5-Nov-12 15:58pm
   
i have error "cant found INPUT" __???
yazimi2 at 7-Nov-12 18:32pm
   
hi.. nice work!
by the way, i am doing similar program related to your problem.
but i can't send the key into the games. i'm using the your code for my program. am I missing something?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 209
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 9 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100