C# でジョイスティックを使う方法

C#  でジョイスティックを使う方法について記載します。

 

 

目次

1. まずは動かしてみる

参考

 

 

1. まずは動かしてみる

[概要]

まずは動かしてみます。

 

[環境]

 

こんな感じのテストプログラムを作成してみます。

作成するプログラムの画面

 

["Joystick.cs"]

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace JoyStickController
  8. {
  9. public enum JOYSTICK_ID
  10. {
  11. JOYSTICKID1 = 0,
  12. JOYSTICKID2 = 1,
  13. JOYSTICKID3 = 2,
  14. JOYSTICKID4 = 3,
  15. JOYSTICKID5 = 4,
  16. JOYSTICKID6 = 5,
  17. JOYSTICKID7 = 6,
  18. JOYSTICKID8 = 7,
  19. JOYSTICKID9 = 8,
  20. JOYSTICKID10 = 9,
  21. JOYSTICKID11 = 10,
  22. JOYSTICKID12 = 11,
  23. JOYSTICKID13 = 12,
  24. JOYSTICKID14 = 13,
  25. JOYSTICKID15 = 14,
  26. JOYSTICKID16 = 15
  27. }
  28.  
  29. [StructLayout(LayoutKind.Sequential)]
  30. public struct JOYINFO
  31. {
  32. public uint dwXpos;
  33. public uint dwYpos;
  34. public uint dwZpos;
  35. public uint dwButtons;
  36. }
  37.  
  38. [StructLayout(LayoutKind.Sequential)]
  39. public struct JOYINFOEX
  40. {
  41. public uint dwSize;
  42. public uint dwFlags;
  43. public uint dwXpos;
  44. public uint dwYpos;
  45. public uint dwZpos;
  46. public uint dwRpos;
  47. public uint dwUpos;
  48. public uint dwVpos;
  49. public uint dwButtons;
  50. public uint dwButtonNumber;
  51. public uint dwPOV;
  52. public uint dwReserved1;
  53. public uint dwReserved2;
  54. }
  55.  
  56. public enum MMRESULT : uint
  57. {
  58. JOYERR_NOERROR = 0,
  59. MMSYSERR_NOERROR = 0,
  60. MMSYSERR_ERROR = 1,
  61. MMSYSERR_BADDEVICEID = 2,
  62. MMSYSERR_NOTENABLED = 3,
  63. MMSYSERR_ALLOCATED = 4,
  64. MMSYSERR_INVALHANDLE = 5,
  65. MMSYSERR_NODRIVER = 6,
  66. MMSYSERR_NOMEM = 7,
  67. MMSYSERR_NOTSUPPORTED = 8,
  68. MMSYSERR_BADERRNUM = 9,
  69. MMSYSERR_INVALFLAG = 10,
  70. MMSYSERR_INVALPARAM = 11,
  71. MMSYSERR_HANDLEBUSY = 12,
  72. MMSYSERR_INVALIDALIAS = 13,
  73. MMSYSERR_BADDB = 14,
  74. MMSYSERR_KEYNOTFOUND = 15,
  75. MMSYSERR_READERROR = 16,
  76. MMSYSERR_WRITEERROR = 17,
  77. MMSYSERR_DELETEERROR = 18,
  78. MMSYSERR_VALNOTFOUND = 19,
  79. MMSYSERR_NODRIVERCB = 20,
  80. WAVERR_BADFORMAT = 32,
  81. WAVERR_STILLPLAYING = 33,
  82. WAVERR_UNPREPARED = 34,
  83.  
  84. JOYERR_PARMS = NativeMethods.JOYERR_PARMS, //!< 165: 指定されたジョイスティックIDは無効
  85. JOYERR_UNPLUGGED = NativeMethods.JOYERR_UNPLUGGED //!< 167: 指定されたジョイスティックが接続されていない
  86. }
  87.  
  88. public class JoyStick
  89. {
  90. private JOYINFO _pji;
  91. private JOYINFOEX _pjiex;
  92. private MMRESULT _mmresult;
  93.  
  94. public JoyStick(JOYSTICK_ID joystick_id)
  95. {
  96. _pji = new JOYINFO();
  97. _pjiex = new JOYINFOEX();
  98. JoystickId = joystick_id;
  99. }
  100.  
  101. public JOYSTICK_ID JoystickId { get; }
  102.  
  103. private MMRESULT GetPos(uint uJoyID, ref JOYINFO pji)
  104. {
  105. return NativeMethods.joyGetPos(uJoyID, ref pji);
  106. }
  107.  
  108. private MMRESULT GetPosEx(uint uJoyID, ref JOYINFOEX pjiex)
  109. {
  110. return NativeMethods.joyGetPosEx(uJoyID, ref pjiex);
  111. }
  112.  
  113. public JOYINFO GetPos()
  114. {
  115. _mmresult = GetPos((uint)JoystickId, ref _pji);
  116. return _pji;
  117. }
  118.  
  119. public JOYINFOEX GetPosEx()
  120. {
  121. _pjiex.dwSize = (uint)Marshal.SizeOf(_pjiex);
  122. _pjiex.dwFlags = NativeMethods.JOY_RETURNALL;
  123. _mmresult = GetPosEx((uint)JoystickId, ref _pjiex);
  124. return _pjiex;
  125. }
  126.  
  127. public static MMRESULT JoyConfigChanged()
  128. {
  129. return NativeMethods.joyConfigChanged(0);
  130. }
  131.  
  132. public MMRESULT GetMMRESULT()
  133. {
  134. return _mmresult;
  135. }
  136. }
  137.  
  138. internal static class NativeMethods
  139. {
  140. #pragma warning disable IDE1006 // 命名スタイル
  141. [DllImport("winmm.dll")]
  142. public static extern uint joyGetNumDevs();
  143. [DllImport("winmm.dll")]
  144. public static extern MMRESULT joyGetPos(uint uJoyID, ref JOYINFO pji);
  145. [DllImport("winmm.dll"), System.Security.SuppressUnmanagedCodeSecurity]
  146. public static extern MMRESULT joyGetPosEx(uint uJoyID, ref JOYINFOEX pjiex);
  147. [DllImport("winmm.dll")]
  148. public static extern MMRESULT joyConfigChanged(uint dwFlags);
  149. #pragma warning restore IDE1006 // 命名スタイル
  150.  
  151. public static UInt32 JOY_RETURNX = 0x00000001;
  152. public static UInt32 JOY_RETURNY = 0x00000002;
  153. public static UInt32 JOY_RETURNZ = 0x00000004;
  154. public static UInt32 JOY_RETURNR = 0x00000008;
  155. public static UInt32 JOY_RETURNU = 0x00000010;
  156. public static UInt32 JOY_RETURNV = 0x00000020;
  157. public static UInt32 JOY_RETURNPOV = 0x00000040;
  158. public static UInt32 JOY_RETURNBUTTONS = 0x00000080;
  159. public static UInt32 JOY_RETURNALL = (JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNPOV | JOY_RETURNBUTTONS);
  160.  
  161. public const int JOYERR_BASE = 160;
  162. public const int JOYERR_PARMS = (JOYERR_BASE + 5); //!< 指定されたジョイスティックIDは無効
  163. public const int JOYERR_UNPLUGGED = (JOYERR_BASE + 7); //!< 指定されたジョイスティックが接続されていない
  164. public const int MMSYSERR_BASE = 0;
  165. public const int MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2);
  166. public const int MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11);
  167. }
  168. }

 

["Form1.cs"]

  1. using JoyStickController;
  2. using System;
  3. using System.Windows.Forms;
  4.  
  5. namespace test_JoyStick
  6. {
  7. public partial class Form1 : Form
  8. {
  9. private JoyStick _joyStick1 = new JoyStick(JOYSTICK_ID.JOYSTICKID1);
  10. private JoyStick _joyStick2 = new JoyStick(JOYSTICK_ID.JOYSTICKID2);
  11.  
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16.  
  17. private void Timer1_Tick(object sender, EventArgs e)
  18. {
  19. // JoyStick 1
  20. {
  21. JOYINFOEX joyInfoEx1 = _joyStick1.GetPosEx();
  22.  
  23. if (_joyStick1.GetMMRESULT() == MMRESULT.JOYERR_NOERROR) {
  24. tb_x_1.Text = joyInfoEx1.dwXpos.ToString();
  25. tb_y_1.Text = joyInfoEx1.dwYpos.ToString();
  26. tb_z_1.Text = joyInfoEx1.dwZpos.ToString();
  27. tb_buttons_1.Text = joyInfoEx1.dwButtons.ToString("X8");
  28. tb_JoyStick1_ID.Text = (_joyStick1.JoystickId).ToString();
  29. }
  30. else
  31. {
  32. if (tb_x_1.Text != "")
  33. {
  34. JoyStick.JoyConfigChanged();
  35. }
  36. tb_x_1.Text = "";
  37. tb_y_1.Text = "";
  38. tb_z_1.Text = "";
  39. tb_buttons_1.Text = "";
  40. tb_JoyStick1_ID.Text = "";
  41. }
  42. }
  43.  
  44. // JoyStick 2
  45. {
  46. JOYINFOEX joyInfoEx2 = _joyStick2.GetPosEx();
  47.  
  48. if (_joyStick2.GetMMRESULT() == MMRESULT.JOYERR_NOERROR)
  49. {
  50. tb_x_2.Text = joyInfoEx2.dwXpos.ToString();
  51. tb_y_2.Text = joyInfoEx2.dwYpos.ToString();
  52. tb_z_2.Text = joyInfoEx2.dwZpos.ToString();
  53. tb_buttons_2.Text = joyInfoEx2.dwButtons.ToString("X8");
  54. tb_JoyStick2_ID.Text = (_joyStick2.JoystickId).ToString();
  55. }
  56. else
  57. {
  58. if (tb_x_2.Text != "")
  59. {
  60. JoyStick.JoyConfigChanged();
  61. }
  62. tb_x_2.Text = "";
  63. tb_y_2.Text = "";
  64. tb_z_2.Text = "";
  65. tb_buttons_2.Text = "";
  66. tb_JoyStick2_ID.Text = "";
  67. }
  68. }
  69. }
  70.  
  71. private void Button1_Click(object sender, EventArgs e)
  72. {
  73. Close();
  74. }
  75. }
  76. }

 

ダウンロード

サンプルプログラム ダウンロード

 

 

参考


 

記載:
2018年05月19日 新規作成、木下英俊