vb.net gamepad支持,部分工作正常

vb.net gamepad支持,部分工作正常,vb.net,class,input,gamepad-api,Vb.net,Class,Input,Gamepad Api,我在一个论坛上找到了下面的类代码。它适用于gamepad(上、下、左、右),但是缺少所有按钮的代码。有人能填空吗 这项工作: Private Sub joystick1_Up() Handles joystick1.Up moveUp() End Sub 这并不是: Private Sub joystick1_buttonPressed() Handles joystick1.buttonPressed MsgBox(joystick1.btnValue) End Sub 因

我在一个论坛上找到了下面的类代码。它适用于gamepad(上、下、左、右),但是缺少所有按钮的代码。有人能填空吗

这项工作:

Private Sub joystick1_Up() Handles joystick1.Up
    moveUp()
End Sub
这并不是:

Private Sub joystick1_buttonPressed() Handles joystick1.buttonPressed
    MsgBox(joystick1.btnValue)
End Sub
因为没有“按钮按下”事件,我也不知道怎么写

下面是课程:

Imports System.ComponentModel
Imports System.Runtime.InteropServices


Public Class joystick
    Inherits NativeWindow

    Private parent As Form
    Private Const MM_JOY1MOVE As Integer = &H3A0

    ' Public Event Move(ByVal joystickPosition As Point)
    Public btnValue As String
    Public Event Up()
    Public Event Down()
    Public Event Left()
    Public Event Right()

    <StructLayout(LayoutKind.Explicit)> _
    Private Structure JoyPosition
        <FieldOffset(0)> _
        Public Raw As IntPtr
        <FieldOffset(0)> _
        Public XPos As UShort
        <FieldOffset(2)> _
        Public YPos As UShort
    End Structure

    Private Class NativeMethods

        Private Sub New()
        End Sub

        ' This is a "Stub" function - it has no code in its body.
        ' There is a similarly named function inside a dll that comes with windows called
        ' winmm.dll. 
        ' The .Net framework will route calls to this function, through to the dll file.
        <DllImport("winmm", CallingConvention:=CallingConvention.Winapi, EntryPoint:="joySetCapture", SetLastError:=True)> _
        Public Shared Function JoySetCapture(ByVal hwnd As IntPtr, ByVal uJoyID As Integer, ByVal uPeriod As Integer, <MarshalAs(UnmanagedType.Bool)> ByVal changed As Boolean) As Integer
        End Function

    End Class

    Public Sub New(ByVal parent As Form, ByVal joyId As Integer)
        AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
        AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
        AssignHandle(parent.Handle)
        Me.parent = parent
        Dim result As Integer = NativeMethods.JoySetCapture(Me.Handle, joyId, 100, True)
    End Sub

    Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        AssignHandle(DirectCast(sender, Form).Handle)
    End Sub

    Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
        ReleaseHandle()
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = MM_JOY1MOVE Then
            ' Joystick co-ords.
            ' (0,0) (32768,0) (65535, 0) 
            '
            '
            '
            ' (0, 32768) (32768, 32768) (65535, 32768)
            '
            '
            '
            '
            ' (0, 65535) (32768, 65535) (65535, 65535)
            '

            Dim p As JoyPosition
            p.Raw = m.LParam
            ' RaiseEvent Move(New Point(p.XPos, p.YPos))
            If p.XPos > 16384 AndAlso p.XPos < 49152 Then
                ' X is near the centre line.
                If p.YPos < 6000 Then
                    ' Y is near the top.
                    RaiseEvent Up()
                ElseIf p.YPos > 59536 Then
                    ' Y is near the bottom.
                    RaiseEvent Down()
                End If
            Else
                If p.YPos > 16384 AndAlso p.YPos < 49152 Then
                    ' Y is near the centre line
                    If p.XPos < 6000 Then
                        ' X is near the left.
                        RaiseEvent Left()
                    ElseIf p.XPos > 59536 Then
                        ' X is near the right
                        RaiseEvent Right()
                    End If
                End If
            End If
        End If
        If btnValue <> m.WParam.ToString Then
            btnValue = m.WParam.ToString
        End If
        MyBase.WndProc(m)
    End Sub

End Class
导入System.ComponentModel
导入System.Runtime.InteropServices
公共类操纵杆
继承NativeWindow
作为形式的私人家长
私有常量MM_joy1作为整数移动=&H3A0
'公共事件移动(ByVal joystickPosition作为点)
作为字符串的公共BTN值
公开活动
公众活动
公共事件左()
公共事件权()
_
私人机构职位
_
公共Raw As IntPtr
_
作为UShort的公共XPO
_
作为UShort的公共YPO
端部结构
私有类本地方法
私人分新
端接头
'这是一个“存根”函数-它的主体中没有代码。
'在windows附带的dll中有一个名为
'winmm.dll。
'Net framework将通过dll文件将调用路由到此函数。
_
公共共享函数JoySetCapture(ByVal hwnd作为IntPtr,ByVal uJoyID作为Integer,ByVal uPeriod作为Integer,ByVal更改为Boolean)作为Integer
端函数
末级
Public Sub New(ByVal parent作为表单,ByVal joyId作为整数)
AddHandler parent.HandleCreated,AddressOf Me.OnHandleCreated
AddHandler parent.HandleDestroyed,AddressOf Me.OnHandleDestroyed
AssignHandle(parent.Handle)
Me.parent=父母
Dim结果为Integer=NativeMethods.JoySetCapture(Me.Handle,joyId,100,True)
端接头
已创建私有子OnHandleCreated(ByVal发送方作为对象,ByVal e作为事件参数)
AssignHandle(DirectCast(发送方,表单).Handle)
端接头
私有子OnHandleDestroyed(ByVal发送方作为对象,ByVal e作为事件参数)
释放句柄()
端接头
受保护的覆盖子WndProc(ByRef m As System.Windows.Forms.Message)
如果m.Msg=MM_joy1移动,则
“操纵杆公司。
' (0,0) (32768,0) (65535, 0) 
'
'
'
' (0, 32768) (32768, 32768) (65535, 32768)
'
'
'
'
' (0, 65535) (32768, 65535) (65535, 65535)
'
将p调暗为操纵杆位置
p、 原始=m.LParam
'RaiseEvent移动(新点(p.XPos,p.YPos))
如果p.XPos>16384且p.XPos<49152,则
“X在中心线附近。
如果p.YPos<6000,则
“Y接近顶部。
升起
如果p.YPos>59536
“Y接近底部。
升起排气口下降
如果结束
其他的
如果p.YPos>16384且p.YPos<49152,则
“Y在中线附近
如果p.XPos<6000,则
“X在左边附近。
RaiseEvent左()
如果p.XPos>59536,则
“X在右边附近
提高权利
如果结束
如果结束
如果结束
如果结束
如果BTN值为m.WParam.ToString,则
btnValue=m.WParam.ToString
如果结束
MyBase.WndProc(m)
端接头
末级

我将使用XInput代替旧的winmm(或者如果可以的话,使用XNA)

有几种方法可以做到这一点。其中一个步骤是直接使用XInput DLL,如所述。但这仍然相当丑陋。可能“更简单”的方法是使用像或这样存在的包装器库

通过SlimDX或SharpDX使用XInput的优点之一是,它也可以在Windows应用商店应用程序(适用于Windows 8:)中使用

以下是一段来自:

#

VB

然后,您可以使用以下命令获取要使用的状态:

var state = controller.GetState();
您会注意到,XInput使用了更多的轮询模型,因此您需要偶尔检查是否按下按钮来检测这种情况。如果您需要连续轮询,您可能可以启动一个新任务来执行此操作

Dim controllers As New List(Of Controller)
controllers.Add(New Controller(UserIndex.One))
controllers.Add(New Controller(UserIndex.Two))
controllers.Add(New Controller(UserIndex.Three))
controllers.Add(New Controller(UserIndex.Four))

Dim controller as Controller = Nothing; 
For Each selectController In controllers
    If selectController.IsConnected Then
        controller = selectController
        Exit For
    End If
Next
var state = controller.GetState();