GamePadListener

★GamePad イベント・リスナー、ハンドラー

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using SlimDX.DirectInput;
using System.Timers;
namespace WindowsFormsApp4
{

    public partial class Form1 : Form
    {
        GamePadClass GamePad = new GamePadClass();      //GamePadオブジェクト宣言
 
        //*****************************************************
        //*      
        //*****************************************************
        public Form1()
        {
            InitializeComponent();
            MainForm_Load();
        }
        //*****************************************************
        //*      Form Load
        //*****************************************************
        private void MainForm_Load()
        {
            GamePad.GamePadInitialize();    //GamePadをイニシャライズし、eventリスナーを起動する
        }
        //*****************************************************
        //*      GamePad event関数(ジョイスティック)
        //*****************************************************
        public static void OnChangeJoystick(String Name,double Value)
        {
            Console.WriteLine(Name + "=" + Value.ToString());
        }
        //*****************************************************
        //*      GamePad event関数(Button Down)
        //*****************************************************
        public static void OnGamePadBDown(String Name)
        {
            Console.WriteLine("Down "+Name);
        }
        //*****************************************************
        //*      GamePad event関数(Button Up)
        //*****************************************************
        public static void OnGamePadBUp(String Name)
        {
            Console.WriteLine("Up "+Name );
        }


    //#####################################################
    //#      GamePad クラス
    //#####################################################
    public class GamePadClass
    {
        public delegate void GamePadEventHandler(string Name, double Value);     //GamePad イベントハンドラー
        public delegate void GamePadBDownEventHandler(string Name);             //GamePad イベントハンドラー
        public delegate void GamePadBUpEventHandler(string Name);               //GamePad イベントハンドラー

        public static event GamePadEventHandler EventGamePad;
        public static event GamePadBDownEventHandler EventGamePadBDown;
        public static event GamePadBUpEventHandler EventGamePadBUp;
        List directInputList = new List();
        DirectInput directInput = new DirectInput();
        SlimDX.DirectInput.Joystick gamepad;
        SlimDX.DirectInput.JoystickState state;
        bool SelectButton = false;
        bool StartButton = false;
        bool YButton = false;
        bool BButton = false;
        bool AButton = false;
        bool XButton = false;
        bool L1Button = false;
        bool L2Button = false;
        bool R1Button = false;
        bool R2Button = false;
        bool LPushButton = false;
        bool RPushButton = false;
        double GamePadX = 0;
        double GamePadY = 0;
        double GamePadZ = 0;
        double GamePadRZ = 0;
        double GamePadHatX = 0;
        double GamePadHatY = 0;
        //*****************************************************
        //*      GamePadイニシャライズ
        //*****************************************************
        public void GamePadInitialize()
        {
            directInputList.AddRange(directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly));
            gamepad = new SlimDX.DirectInput.Joystick(directInput, directInputList[1].InstanceGuid);        //何故か、0ではNGで、1ならOK
            var TimerUpdateStatus = new System.Timers.Timer();      //GamePad Status Senseのためのタイマーループ
            EventGamePad = Form1.OnChangeJoystick;    //event発生時Form1のChangeJoystickを起動する
            EventGamePadBDown = Form1.OnGamePadBDown;
            EventGamePadBUp = Form1.OnGamePadBUp;
            TimerUpdateStatus.Elapsed += new ElapsedEventHandler(UpdateStatus);
            TimerUpdateStatus.Interval = 20;
            TimerUpdateStatus.Start();
        }
        //*****************************************************
        //*      GamePad Status更新(イベント・リスナー)
        //*****************************************************
        public void UpdateStatus(object sender, ElapsedEventArgs e)
        {
            int[] hxy;
            double NewGamePadX = 0;
            double NewGamePadY = 0;
            double NewGamePadZ = 0;
            double NewGamePadRZ = 0;
            double NewGamePadHatX = 0;
            double NewGamePadHatY = 0;
            string Command = "";
            String Name = "";
            double Value=0;
            try
            {
                gamepad.Acquire();
                gamepad.Poll();
                state = this.gamepad.GetCurrentState();
                
            }
            catch {
                Console.WriteLine("Error");
                gamepad.Unacquire();
                return;
            };

            NewGamePadX = (state.X - 32767.0) / 32767.0;                   //-1~+1f
            NewGamePadY = (state.Y - 32767.0) / 32767.0;
            NewGamePadZ = (state.Z - 32767.0) / 32767.0;
            NewGamePadRZ = (state.RotationZ - 32767.0) / 32767.0;
            bool[] buttons = state.GetButtons();
            hxy = state.GetPointOfViewControllers();
            if (hxy[0] == 27000)                    //HatX 左側Push
            {
                NewGamePadHatX = -1;
            }
            else if (hxy[0] == 9000)                    //HatX 右側Push
            {
                NewGamePadHatX = 1;
            }
            else if (hxy[0] == 0)                    //HatY= 上側Push
            {
                NewGamePadHatY = -1;
            }
            else if (hxy[0] == 18000)                    //HatY 下側Push
            {
                NewGamePadHatY = 1;
            }
            else
            {
                NewGamePadHatX = 0;
                NewGamePadHatY = 0;
            }
            if(GamePadX != NewGamePadX)
            {
                GamePadX = NewGamePadX;
                EventGamePad("X", GamePadX);
            }
            else if (GamePadY != NewGamePadY)
            {
                GamePadY = NewGamePadY;
                EventGamePad("Y", GamePadY);
            }
            else if (GamePadZ != NewGamePadZ)
            {
                GamePadZ = NewGamePadZ;
                EventGamePad("Z", GamePadZ);
            }
            else if (GamePadRZ != NewGamePadRZ)
            {
                GamePadRZ = NewGamePadRZ;
                EventGamePad("RZ", GamePadRZ);
            }
            else if (GamePadHatX != NewGamePadHatX)
            {
                GamePadHatX = NewGamePadHatX;
                EventGamePad("HatX", GamePadHatX);
            }
            else if (GamePadHatY != NewGamePadHatY)
            {
                GamePadHatY = NewGamePadHatY;
                EventGamePad("HatY", GamePadHatY);
            }

            //Button down

            if (YButton == false && buttons[4] == true)
            {
                YButton = true;
                EventGamePadBDown("YButton");
            }
            else if (AButton == false && buttons[0] == true)
            {
                AButton = true;
                EventGamePadBDown("AButton");
            }
            else if (BButton == false && buttons[1] == true)
            {
                BButton = true;
                EventGamePadBDown("BButton");
            }
            else if (XButton == false && buttons[3] == true)
            {
                XButton = true;
                EventGamePadBDown("XButton");
            }
            else if (L1Button == false && buttons[6] == true)
            {
                L1Button = true;
                EventGamePadBDown("L1Button");
            }
            else if (L2Button == false && buttons[8] == true)
            {
                L2Button = true;
                EventGamePadBDown("L2Button");
            }
            else if (R1Button == false && buttons[7] == true)
            {
                R1Button = true;
                EventGamePadBDown("R1Button");
            }
            else if (R2Button == false && buttons[9] == true)
            {
                R2Button = true;
                EventGamePadBDown("R2Button");
            }
            else if (LPushButton == false && buttons[13] == true)
            {
                LPushButton = true;
                EventGamePadBDown("LPushButton");
            }
            else if (RPushButton == false && buttons[14] == true)
            {
                RPushButton = true;
                EventGamePadBDown("RPushButton");
            }
            else if (SelectButton == false && buttons[10] == true)
            {
                SelectButton = true;
                EventGamePadBDown("SelectButton");
            }
            else if (StartButton == false && buttons[11] == true)
            {
                StartButton = true;
                EventGamePadBDown("StartButton");
            }
            //Button Up

            if (YButton == true && buttons[4] == false)
            {
                YButton = false;
                EventGamePadBUp("YButton");
            }
            else if (AButton == true && buttons[0] == false)
            {
                AButton = false;
                EventGamePadBUp("AButton");
            }
            else if (BButton == true && buttons[1] == false)
            {
                BButton = false;
                EventGamePadBUp("BButton");
            }
            else if (XButton == true && buttons[3] == false)
            {
                XButton = false;
                EventGamePadBUp("XButton");
            }
            else if (L1Button == true && buttons[6] == false)
            {
                L1Button = false;
                EventGamePadBUp("L1Button");
            }
            else if (L2Button == true && buttons[8] == false)
            {
                L2Button = false;
                EventGamePadBUp("L2Button");
            }
            else if (R1Button == true && buttons[7] == false)
            {
                R1Button = false;
                EventGamePadBUp("R1Button");
            }
            else if (R2Button == true && buttons[9] == false)
            {
                R2Button = false;
                EventGamePadBUp("R2Button");
            }
            else if (LPushButton == true && buttons[13] == false)
            {
                LPushButton = false;
                EventGamePadBUp("LPushButton");
            }
            else if (RPushButton == true && buttons[14] == false)
            {
                RPushButton = false;
                EventGamePadBUp("RPushButton");
            }
            else if (SelectButton == true && buttons[10] == false)
            {
                SelectButton = false;
                EventGamePadBUp("SelectButton");
            }
            else if (StartButton == true && buttons[11] == false)
            {
                StartButton = false;
                EventGamePadBUp("StartButton");
            }
            gamepad.Unacquire();
        }
    }
}

〒350-0821        埼玉県川越市福田422