Joystick Key Capture

喜你入骨 提交于 2019-12-06 07:07:58

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.

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
            //...
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!