操纵杆按键捕捉

发布于 2024-10-07 18:31:13 字数 263 浏览 2 评论 0原文

我想使用 winmm dll 捕获来自操纵杆的输入..我对导入和使用 DLL 没有太多了解。

我尝试这样做,但我不知道该怎么做..

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

如何从DLL获取joyinfoEx结构并将其放入JoygetPosEx..? :S

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2024-10-14 18:31:13

正如您的问题所示,您必须从 wimmm.dll 导入 joyGetPosEx 函数。这通常称为 P/Invoking 因为您正在调用一个函数本机 Win32 API(或“平台”DLL)的。您已经知道需要使用该函数,所以让我们更仔细地看看它。

该函数的文档表明它需要两个参数:

  • 第一个 (uJoyID) 是一个简单的 int(或整数)值,用于标识要查询的操纵杆。

  • 第二个(pji)比第一个稍微复杂一些,我认为这是您正在努力解决的部分。它不是标准值类型,而是一个名为 JOYINFOEX 的结构,您必须在代码中定义该结构。 JoyGetPosEx 函数的作用是用有关操纵杆位置的信息填充该特殊结构,使其能够一次返回一堆不同的值。这就是为什么您必须将其作为引用 (ref) 传递,因为该函数实际上将使用一组参数来填充您传递给它的结构实例价值观。然后,您的代码可以从包含该结构实例的变量中读取这些值。

    该结构的文档位于此处,它会告诉您
    每个成员的名称和数据类型。您可以将 DWORD 视为 int,因此其声明如下所示:

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

最后,joyGetPosEx 函数返回一个 类型的值int。该值告诉您该函数是否成功,如果没有成功,则到底出了什么问题。这是一个错误代码。上面链接的文档为您提供了可以返回的所有可能值的表格。如果您想检查这些值,则需要在代码中将它们定义为常量:

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); 

因此,鉴于上述内容,最终的函数定义如下所示:

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

将来,请记住 pinvoke.net 作为如何在代码中定义和声明这些 Win32 API 函数的参考。它碰巧没有 joyGetPosEx 函数(或者至少我没有发现),但它通常几乎拥有您需要的一切。然后,如果失败,您可以按照我上面尝试解释的那样进行操作:查看该函数的原始文档,并尝试找出相应的定义以使其在 .NET 中工作。

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:

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

  • 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; 
    }
    

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);

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.

上课铃就是安魂曲 2024-10-14 18:31:13

该函数通过第二个参数返回数据。返回值是一个整数,指示任何可能的错误。正确的声明是:

    [DllImport("winmm.dll")]
    private static extern int joyGetPosEx(int joystickNumber, ref JOYINFOEX info);

您必须声明如下结构:

    [StructLayout(LayoutKind.Sequential)]
    private 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;
    }

示例调用:

        JOYINFOEX info = new JOYINFOEX();
        info.dwSize = Marshal.SizeOf(info);
        int err = joyGetPosEx(1, ref info);
        if (err != 0) reportError(err);
        else {
            int xpos = info.dwXpos;
            // Do something with xpos
            //...
        }

The function returns data through the second argument. The return value is an integer, indicating any possible error. The proper declaration is:

    [DllImport("winmm.dll")]
    private static extern int joyGetPosEx(int joystickNumber, ref JOYINFOEX info);

You'll have to declare the structure like this:

    [StructLayout(LayoutKind.Sequential)]
    private 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;
    }

A sample call:

        JOYINFOEX info = new JOYINFOEX();
        info.dwSize = Marshal.SizeOf(info);
        int err = joyGetPosEx(1, ref info);
        if (err != 0) reportError(err);
        else {
            int xpos = info.dwXpos;
            // Do something with xpos
            //...
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文