C# Joystick Kullanımı
| Kasım 6, 2016Bir projemizde joystick kullanmamız gerekiyordu.C# ta joystick komutları nasıl alırım diye araştırmaya başladım. CodeProject birkaç başrısız örnekten sonra Mostafa Korashy ‘in bir örneğine rasladım. Ama bazı tuşların çalışmadığını gördüm. Bu uygulama üzerinden geliştirme yaptım.
Joystick.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.DirectX.DirectInput; using System.Windows.Forms; using System; class Joystick { #region param private Device joystickDevice; private JoystickState state; public int Xaxis; // X-axis movement public int Yaxis; //Y-axis movement public int Pov; // X-axis movement private IntPtr hWnd; public bool[] buttons; private string systemJoysticks; #endregion public Joystick(IntPtr window_handle) { hWnd = window_handle; Xaxis = -1; } public string FindJoysticks() { systemJoysticks = null; try { // Find all the GameControl devices that are attached. DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); // check that we have at least one device. if (gameControllerList.Count > 0) { foreach (DeviceInstance deviceInstance in gameControllerList) { // create a device from this controller so we can retrieve info. joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); systemJoysticks = joystickDevice.DeviceInformation.InstanceName; break; } } } catch { return null; } return systemJoysticks; } public bool AcquireJoystick(string name) { try { DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); int i = 0; bool found = false; foreach (DeviceInstance deviceInstance in gameControllerList) { if (deviceInstance.InstanceName == name) { found = true; joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; } i++; } if (!found) return false; joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); joystickDevice.Acquire(); UpdateStatus(); } catch (Exception err) { return false; } return true; } public void ReleaseJoystick() { joystickDevice.Unacquire(); } public void UpdateStatus() { Poll(); int[] extraAxis = state.GetSlider(); int[] _GetPointOfView = state.GetPointOfView(); Pov = _GetPointOfView[0]; Xaxis = state.X; Yaxis = state.Y; byte[] jsButtons = state.GetButtons(); buttons = new bool[jsButtons.Length]; int i = 0; foreach (byte button in jsButtons) { buttons[i] = button >= 128; i++; } } private void Poll() { try { // poll the joystick joystickDevice.Poll(); // update the joystick state field state = joystickDevice.CurrentJoystickState; } catch { throw (null); } } } |
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | public partial class Form1 : Form { private Joystick joystick; private bool[] joystickButtons; public Form1() { InitializeComponent(); joystick = new Joystick(this.Handle); connectToJoystick(joystick); } private void connectToJoystick(Joystick joystick) { while (true) { string sticks = joystick.FindJoysticks(); if (sticks != null) { if (joystick.AcquireJoystick(sticks)) { enableTimer(); break; } } } } private void enableTimer() { if (this.InvokeRequired) { BeginInvoke(new ThreadStart(delegate () { joystickTimer.Enabled = true; })); } else joystickTimer.Enabled = true; } private void joystickTimer_Tick_1(object sender, EventArgs e) { try { joystick.UpdateStatus(); joystickButtons = joystick.buttons; //output.Text += "Pov:" + joystick.Pov; /*********************RightButton************************/ if (joystick.Pov == 9000) { RightButton(); } if (joystick.Xaxis >= 55535) { RightButton(); } /*********************RightButton************************/ /*********************LeftButton************************/ if (joystick.Xaxis <= 16535) { LeftButton(); } if (joystick.Pov == 27000) { LeftButton(); } /********************LeftButton*************************/ /********************UpButton*************************/ if (joystick.Pov == 0) { UpButton(); } if (joystick.Yaxis <= 16535) { UpButton(); } /********************LeftButton*************************/ /********************DownButton*************************/ if (joystick.Yaxis >= 55535) { DownButton(); } if (joystick.Pov == 18000) { DownButton(); } /********************DownButton*************************/ for (int i = 0; i < joystickButtons.Length; i++) { if (joystickButtons[i] == true) { output.Text += "Button " + i + " Pressed\n"; if (i == 9) { StartButton(); } else if (i == 8) { StopButton(); } } } } catch (Exception ex) { joystickTimer.Enabled = false; connectToJoystick(joystick); } } private void LeftButton() { output.Text += "Left\n"; _SelectForm.fxSelectButton("Left"); } private void RightButton() { output.Text += "Right\n"; } private void UpButton() { output.Text += "Up\n"; } private void DownButton() { output.Text += "Down\n"; } private void StartButton() { output.Text += "Start\n"; } private void StopButton() { output.Text += "Stop\n"; } } |
Hata:
1 2 3 | Managed Debugging Assistant 'LoaderLock' has detected a problem in 'C:\Users\zafer\Desktop\RunJoyStickOnLocalMachine\RunJoyStickOnLocalMachine\bin\Debug\RunJoyStickOnLocalMachine.vshost.exe'. Additional information: DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX.DirectInput\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.DirectInput.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang. |
Çözüm:
“Debug > Windows > Exception Setting > Managed Debugging Asistants > LoaderLock” Check’ini kaldırın.