Trouble setting cooperative level [SharpDX]
WPF
1
Posts
1
Posters
0
Views
1
Watching
-
Hi, I'm using SharpDX for DirectInput joystick management...and I can't resolve the problem I get while setting cooperative level
Imports SharpDX
Imports SharpDX.DirectInput
Imports System.Windows.InteropClass MainWindow
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
MainForJoystick()
End Sub
'
Private Shared Sub MainForJoystick()
' Initialize DirectInput
Dim directInput As New DirectInput' Find a Joystick Guid Dim joystickGuid = Guid.Empty For Each deviceInstance As DeviceInstance In directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices) joystickGuid = deviceInstance.InstanceGuid Next ' If Gamepad not found, look for a Joystick If joystickGuid = Guid.Empty Then For Each deviceInstance As DeviceInstance In directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices) joystickGuid = deviceInstance.InstanceGuid Next End If ' If Joystick not found, throws an error If joystickGuid = Guid.Empty Then Debug.Print("No joystick/Gamepad found.") End If ' Instantiate the joystick Dim hwnd As IntPtr = New WindowInteropHelper(Application.Current.MainWindow).EnsureHandle() Dim joystick = New Joystick(directInput, joystickGuid) joystick.SetCooperativeLevel(hwnd, CooperativeLevel.Foreground) Debug.Print("Found Joystick/Gamepad with GUID: {0}", joystickGuid) ' Set BufferSize in order to use buffered data. joystick.Properties.BufferSize = 128 ' Acquire the joystick joystick.Acquire() ' Poll events from joystick While True joystick.Poll() Dim datas = joystick.GetBufferedData() For Each state As JoystickUpdate In datas Debug.Print(state.ToString) ' Example detecting 'Up' button If state.Offset = 60 Then If state.Value = 128 Then Debug.Print("Up button pressed") End If End If Next End While End Sub
End Class
The problem comes from here: joystick.SetCooperativeLevel(hwnd, CooperativeLevel.Foreground) SharpDX.SharpDXException was unhandled HResult=-2147024809 Message=HRESUL