摇杆按键捕获 [英] Joystick Key Capture

查看:598
本文介绍了摇杆按键捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用WINMM DLL来捕获游戏杆输入..我没有进口和DLL文件的工作很多知识。

I want to capture input from joystick using winmm dll .. I don't have much knowledge of importing and working with DLLs.

我想这样做,但我不知道该怎么做。

I tried to do it but i have no idea how to do it ..

[DllImport("winmm.dll")]
public static extern string joyGetPosEx(uint dev, JoyinfoEx ) //Something Similar

我如何从DLL joyinfoEx结构,并把它变成JoygetPosEx ..? :•

How do I get joyinfoEx struct from DLL and put it into JoygetPosEx .. ? :S

推荐答案

至于你的问题建议,必须从 wimmm导入 joyGetPosEx 功能.DLL 。这通常被称为 P /调用因为你调用一个函数出来土生土长的Win32 API(或平台DLL)。你已经知道你需要使用该功能,所以让我们来看看它更仔细。

As your question suggests, you have to import the joyGetPosEx function from wimmm.dll. This is often called P/Invoking because you're invoking a function out of a native Win32 API (or "platform" DLL). You already know that you need to use that function, so let's look at it more carefully.

该功能的文档表明它有两个参数:

The documentation for that function indicates that it takes two parameters:

  • 第一( uJoyID )是一个简单的 INT (或整数)值标识操纵杆进行查询。

  • The first (uJoyID) is a simple int (or, integer) value that identifies the joystick to be queried.

第二个( PJI )是比较复杂的比第一点点,我觉得这是你都在努力的一部分。取而代之的是标准值的类型,它是一个名为 JOYINFOEX 结构,你必须在你的code定义的结构。什么 joyGetPosEx 函数的作用是填补这一特殊结构有关操纵杆的位置信息,允许其返回一堆不同的价值观的一次。这就是为什么你必须把它作为一个参考( REF ),因为该功能实际上是要的填写的您传递结构的实例将它与一组值。然后,您的code可以读取这些值回的的包含结构的实例变量登录到到网文档的结构可用的此处,它会告诉你的名字和数据类型
每个成​​员的。你可以把一个 DWORD INT ,所以它的声明是这样的:

The second (pji) is a little bit more complicated than the first, and I think this is the part you were struggling with. Instead of a standard value type, it is a structure called JOYINFOEX, and you have to define that structure in your code. What the joyGetPosEx function does is fill that special structure with information about the joystick's position, allowing it to return a bunch of different values all at once. That's why you have to pass it as a reference (ref), because the function is actually going to fill the instance of the structure that you pass to it with a set of values. Your code can then read those values back out of the variable containing the instance of the structure.

The documentation for that structure is available here, and it tells you the names and data types
of each of its members. You can treat a DWORD as an int, so its declaration looks like this:

[StructLayout(LayoutKind.Sequential)]
public struct JOYINFOEX 
{
    public int dwSize; 
    public int dwFlags; 
    public int dwXpos; 
    public int dwYpos; 
    public int dwZpos; 
    public int dwRpos; 
    public int dwUpos; 
    public int dwVpos; 
    public int dwButtons; 
    public int dwButtonNumber; 
    public int dwPOV; 
    public int dwReserved1; 
    public int dwReserved2; 
}

最后, joyGetPosEx 函数返回类型的值 INT 。此值告诉您函数是否成功,如果没有,究竟哪里出了问题。这是一个错误code。上面链接的文档,为您提供所有可返回的可能值的表。如果你想核对这些值,则需要将其定义为在code常数:

Finally, the joyGetPosEx function returns a value of type int. This value tells you whether or not the function succeeded, and if it didn't, what exactly went wrong. It's an error code. The documentation linked above gives you a table of all the possible values that can be returned. If you want to check against those values, you'll need to define them as constants in your code:

public const int JOYERR_BASE = 160
public const int JOYERR_PARMS = (JOYERR_BASE + 5); 
public const int JOYERR_UNPLUGGED = (JOYERR_BASE + 7);
public const int MMSYSERR_BASE = 0;
public const int MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2); 
public const int MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11); 

因此​​,鉴于上述情况,最终处理函数的定义是这样的:

So, given the above, the final function definition looks like this:

[DllImport("winmm.dll")]
public static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji);

在未来,牢记 pinvoke.net 至于如何定义一个基准,并宣布这些Win32 API函数在code。它不会发生有 joyGetPosEx 函数(或至少不,我发现),但它通常有你需要的几乎所有的东西。然后,如果失败了,你可以做我试图解释以上:看该函数的原始文件,并试图找出相应地定义它在.NET工作

In the future, keep in mind pinvoke.net as a reference for how to define and declare these Win32 API functions in your code. It doesn't happen to have the joyGetPosEx function (or at least not that I found), but it usually has almost everything you need. And then if that fails, you can do as I tried to explain above: look at the original documentation for the function and try to figure out to define it accordingly to work in .NET.

这篇关于摇杆按键捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆