Exemplo n.º 1
0
 public static extern int JoyGetPosEx(int uJoyID, ref JoyInfoEx pji);
Exemplo n.º 2
0
        private void TimerFunction(object sender, System.Timers.ElapsedEventArgs e)
        {
            int count           = NativeMethods.JoyGetNumDevs();
            int sizeCaps        = Marshal.SizeOf(typeof(JoyCaps));
            int sizeInfo        = Marshal.SizeOf(typeof(JoyInfoEx));
            int controllerIndex = -1;

            var pressedKeys = new List <string>();

            for (int deviceIndex = 0; deviceIndex < count; deviceIndex++)
            {
                if (NativeMethods.JoyGetDevCaps(new IntPtr(deviceIndex), out JoyCaps caps, sizeCaps) != 0)
                {
                    continue;
                }

                JoyInfoEx info = new JoyInfoEx
                {
                    dwSize    = (uint)sizeInfo,
                    dwOptions = JoyInfoExOptions.ReturnAll | JoyInfoExOptions.ReturnCentered | JoyInfoExOptions.UseDeadZone
                };

                if (NativeMethods.JoyGetPosEx(deviceIndex, ref info) != 0)
                {
                    continue;
                }

                controllerIndex++;

                int index1 = controllerIndex;
                int index2 = caps.ManufacturerID << 16 | caps.ProductID;

                for (int i = 0; i < 6; i++)
                {
                    string controllerName = (string)ControllerNameConverter.Default.Convert(new object[] { this.JoystickControllers, controllerIndex }, null, null, null);
                    string controller     = string.Format(CultureInfo.InvariantCulture, "{0} : {1}", index1, index2);
                    string axis           = string.Format(CultureInfo.InvariantCulture, "{0}", i);
                    uint   position       = 0;

                    switch (i)
                    {
                    case 0:
                        position = info.dwXpos;
                        break;

                    case 1:
                        position = info.dwYpos;
                        break;

                    case 2:
                        position = info.dwZpos;
                        break;

                    case 3:
                        position = info.dwRpos;
                        break;

                    case 4:
                        position = info.dwUpos;
                        break;

                    case 5:
                        position = info.dwVpos;
                        break;
                    }

                    int index = -1;

                    for (int j = 0; j < this.JoystickConfigAxes.Count; j++)
                    {
                        if (string.Equals(this.JoystickConfigAxes[j].Controller, controller, StringComparison.Ordinal) &&
                            string.Equals(this.JoystickConfigAxes[j].Axis, axis, StringComparison.Ordinal))
                        {
                            index = j;
                            break;
                        }
                    }

                    if (index != -1)
                    {
                        this.JoystickConfigAxes[index].Position = (int)position;
                    }
                    else
                    {
                        this.Dispatcher.Invoke(() => this.JoystickConfigAxes.Add(new JoystickConfigAxis(controller, controllerName, axis)
                        {
                            Position = (int)position
                        }));
                    }
                }

                for (int i = 0; i < caps.wNumButtons; i++)
                {
                    if ((info.dwButtons & 1U << i) != 0)
                    {
                        string key1 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_{1}", index1, i + 1);
                        string key2 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_{1}", index2, i + 1);

                        pressedKeys.Add(key1);
                        pressedKeys.Add(key2);
                    }
                }

                if (caps.wCaps.HasFlag(JoyDriverCaps.HasPov))
                {
                    if (info.dwPOV != 0xffff)
                    {
                        int povIndex = (int)(info.dwPOV / 9000U);

                        string key1 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_pov{1}", index1, povIndex + 1);
                        string key2 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_pov{1}", index2, povIndex + 1);

                        pressedKeys.Add(key1);
                        pressedKeys.Add(key2);
                    }
                }
            }

            foreach (JoystickConfigButton button in this.JoystickConfigButtons)
            {
                button.IsPressed = false;

                foreach (string pressedKey in pressedKeys)
                {
                    if (string.Equals(button.Key, pressedKey, StringComparison.Ordinal))
                    {
                        button.IsPressed = true;
                        break;
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void TimerFunction(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.JoystickConfigButtons.Count == 0)
            {
                return;
            }

            int count           = NativeMethods.JoyGetNumDevs();
            int sizeCaps        = Marshal.SizeOf(typeof(JoyCaps));
            int sizeInfo        = Marshal.SizeOf(typeof(JoyInfoEx));
            int controllerIndex = -1;

            var pressedKeys = new List <string>();

            for (int deviceIndex = 0; deviceIndex < count; deviceIndex++)
            {
                if (NativeMethods.JoyGetDevCaps(new IntPtr(deviceIndex), out JoyCaps caps, sizeCaps) != 0)
                {
                    continue;
                }

                JoyInfoEx info = new JoyInfoEx
                {
                    dwSize    = (uint)sizeInfo,
                    dwOptions = JoyInfoExOptions.ReturnPov | JoyInfoExOptions.ReturnButtons | JoyInfoExOptions.ReturnCentered
                };

                if (NativeMethods.JoyGetPosEx(deviceIndex, ref info) != 0)
                {
                    continue;
                }

                controllerIndex++;

                int index1 = controllerIndex;
                int index2 = caps.ManufacturerID << 16 | caps.ProductID;

                for (int i = 0; i < caps.wNumButtons; i++)
                {
                    if ((info.dwButtons & 1U << i) != 0)
                    {
                        string key1 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_{1}", index1, i + 1);
                        string key2 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_{1}", index2, i + 1);

                        pressedKeys.Add(key1);
                        pressedKeys.Add(key2);
                    }
                }

                if (caps.wCaps.HasFlag(JoyDriverCaps.HasPov))
                {
                    if (info.dwPOV != 0xffff)
                    {
                        int povIndex = (int)(info.dwPOV / 9000U);

                        string key1 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_pov{1}", index1, povIndex + 1);
                        string key2 = string.Format(CultureInfo.InvariantCulture, "joybutton_{0}_pov{1}", index2, povIndex + 1);

                        pressedKeys.Add(key1);
                        pressedKeys.Add(key2);
                    }
                }
            }

            foreach (JoystickConfigButton button in this.JoystickConfigButtons)
            {
                button.IsPressed = false;

                foreach (string pressedKey in pressedKeys)
                {
                    if (string.Equals(button.Key, pressedKey, StringComparison.Ordinal))
                    {
                        button.IsPressed = true;
                    }
                }
            }
        }