也许有人可以帮帮我? 太棒了! 提前致谢。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 0x54In DirectX case, it''s 0x14So when I''m ingame, and I start my program, my Capslock is active.I don''t get it. :sThe 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.推荐答案好吧,不知怎的,我设法让这个工作。 因为我觉得我不是唯一一个有这个话题的问题我得回答我自己的问题。 我们要做的第一件事就是输入 user32.dll因为.NET不支持DirectInput(至少是我所理解的,如果没有,请纠正我)。 所以我们做..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 在该行之后我们可以导入我们的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); 现在我们需要声明一些变量才能让程序正常工作。 因为我们想模拟一个Key,所以我们只需要使用以下代码: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; } 现在我们需要声明''INPUT''变量..Now we need to declare the ''INPUT'' variable..[StructLayout(LayoutKind.Explicit)] struct INPUT { [FieldOffset(0)] public int type; [FieldOffset(4)] public KEYBDINPUT ki; } 我们初始化变量后,我们可以发送一些密钥到DirectX游戏。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 =>表示我们要发送的密钥代码。 如果我们要发送密钥T,我们必须输入0x14。 0x14表示DirectX中的T.可以在网上轻松找到KeyCodes列表。 InputData [0] .ki.dwFlags =>意味着我们的Flag,我们可以输入0x0008(对于Sendkey) 或创建一个常量变量。你喜欢什么。 我们的最终代码可能如下所示: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))); } }}您能提供一个使用示例,10x!Can you supply a usage example, 10x! 这篇关于c- sharp DirectInput(发送密钥)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 19:54