Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Trouble setting cooperative level [SharpDX]

Trouble setting cooperative level [SharpDX]

Scheduled Pinned Locked Moved WPF
helpcomdebuggingtutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jayme65
    wrote on last edited by
    #1

    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.Interop

    Class 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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups