SendKey到VB.net中的游戏

时间:2018-02-10 15:08:04

标签: vb.net winforms udp sendkeys

快速提问,我有一个WinForm vb.net项目,它根据收到的UDP消息使用SendKey方法发送键击。我的代码在使用记事本或类似的东西时就像魅力一样,但是,我的应用程序的最终目标是将这些命令发送到游戏(在这种情况下是Elite Dangerous)但它不起作用......游戏永远不会收到击键。 知道为什么吗?游戏是专注的应用程序,因为我正在播放它,如果我切换到我的应用程序,我看到收到UDP消息并发送命令,如果我关注记事本,它可以工作,但不适用于游戏:(

这是我的代码:

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.ComponentModel

Public Class Form1

    Private Shared listenPort As Integer
    Private Shared HostIP As IPAddress
    Public CheckResult As Boolean = False
    Public KeystrokeMessage As String = ""
    Public cpt As Integer = 0
    Dim p() As Process

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Disable the Start button until a local IP is selected
        StartStopBtn.Enabled = False

        ' ####################################
        ' ## Get list of local IP addresses ##
        ' ####################################
        Dim hostname As String
        'get host name
        hostname = System.Net.Dns.GetHostName()
        'get a list of IP addresses for the give host name
        Dim hostentry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(hostname)
        'List all IP used on the PC in the ComboBox
        For Each ip As System.Net.IPAddress In hostentry.AddressList
            If ip.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
                ComboBox1.Items.Add(ip.ToString())
            End If
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ' If an IP address is selected, the Start button is enabled
        If ComboBox1.SelectedItem <> "- Please select -" Then
            StartStopBtn.Enabled = True
        End If
    End Sub

    Private Sub StartStopBtn_Click(sender As Object, e As EventArgs) Handles StartStopBtn.Click
        Select Case StartStopBtn.Text
            Case "Start Listening"
                ' Update the interface
                StartStopBtn.Text = "Stop Listening"
                ComboBox1.Enabled = False
                UDPTextBox.Enabled = False
                ' Start listening to UDP messages
                listenPort = CInt(UDPTextBox.Text)
                If Not (ComboBox1.SelectedItem = "Any") Then
                    Try
                        HostIP = IPAddress.Parse(ComboBox1.SelectedItem)
                    Catch ex As Exception
                        MsgBox("error")
                    End Try
                End If

                If Not BackgroundWorker1.IsBusy = True Then
                    BackgroundWorker1.RunWorkerAsync()
                End If

            Case "Stop Listening"
                ' Update the interface
                StartStopBtn.Text = "Start Listening"
                ComboBox1.Enabled = True
                UDPTextBox.Enabled = True
                ' Stop listening to UDP messages
                BackgroundWorker1.CancelAsync()
                BackgroundWorker1.CancelAsync()
        End Select
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim done As Boolean = False
        Dim listener As New UdpClient(listenPort)
        Dim groupEP As New IPEndPoint(HostIP, listenPort)
        Dim mystring As String

        Try
            While Not done
                If BackgroundWorker1.CancellationPending = True Then
                    e.Cancel = True
                    listener.Close()
                    Exit While
                Else
                    If (listener.Available > 0) Then
                        Dim bytes As Byte() = listener.Receive(groupEP)
                        KeystrokeMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length)
                        mystring = "From " & groupEP.ToString() & " : " & KeystrokeMessage
                        worker.ReportProgress(0, mystring)
                    End If
                End If
            End While

        Catch ex As Exception

        Finally
            listener.Close()
        End Try

    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        If (BackgroundWorker1.CancellationPending = False) Then
            MessageTextBox.Text = (e.UserState.ToString)
            SendKeystroke()
        End If
        Update()
    End Sub

    Private Sub SendKeystroke()
        ' Check the content of the received UDP message
        Select Case KeystrokeMessage.Substring(0, 8)
            ' If the message starts by "SendKey." then remote the header and send the rest of the message
            ' ex: if the message is "SendKey.A" the process will send A 
            Case "SendKey."
                SendKeys.SendWait(KeystrokeMessage.Substring(8))
            ' If not, just display the message in the Textbox with the mention "Invalid command" and do nothing else
            Case Else
                MessageTextBox.Text = MessageTextBox.Text & " (Invalid command!)"
        End Select
    End Sub

End Class

非常感谢:)

0 个答案:

没有答案