Click here to Skip to main content
15,893,904 members
Articles / Desktop Programming / Win32

Mirror keys for multiboxing MMORPG games like WOW/LOTRO

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Mar 2013CPOL5 min read 20.7K   800   3  
Raw input 64-bit .NET classes.
Public Class UdpKeyPost
    Private Shared m_timeout_milliseconds As Integer = 500
    Private Shared m_timeout_ticks As Long = TimeSpan.TicksPerMillisecond * m_timeout_milliseconds
    Private m_LastKeydownMessageDateTime As DateTime = New DateTime(0)
    Private m_UdpKeyPostManager As UdpKeyPostManager = Nothing
    Private m_WindowHandle As IntPtr = IntPtr.Zero
    Private m_UdpKeyInfo As UdpKeyInfo = Nothing
    Private m_Validated As Boolean = False
    Private m_BackgroundWorker_cancelled As Boolean = False
    Private m_BackgroundWorker_postedOne As Boolean = False
    Private m_BackgroundWorker_isFinished As Boolean = False
    Private WithEvents m_BackgroundWorker As New System.ComponentModel.BackgroundWorker 'Asynchronous Non-Blocking Background Worker
    Private Sub m_BackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles m_BackgroundWorker.DoWork
        Try
            Do Until (Now.Ticks - m_LastKeydownMessageDateTime.Ticks) > m_timeout_ticks
                System.Threading.Thread.Sleep(m_timeout_milliseconds)
                SyncLock m_UdpKeyPostManager
                    m_LastKeydownMessageDateTime = m_UdpKeyPostManager.LastKeydown(m_WindowHandle, m_UdpKeyInfo)
                End SyncLock
            Loop
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
        End Try
    End Sub

    Private Sub m_BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles m_BackgroundWorker.RunWorkerCompleted
        Debug.WriteLine("completed;    cancelled: " & m_BackgroundWorker_cancelled.ToString & "     posted: " & m_BackgroundWorker_postedOne.ToString)
        If ((Not m_BackgroundWorker_cancelled) AndAlso (Not m_BackgroundWorker_postedOne)) Then
            Debug.WriteLine("keyup timeout")
            RawInputHook.PostMessage(m_WindowHandle, CInt(m_UdpKeyInfo.Message Or CInt(1)), New IntPtr(m_UdpKeyInfo.VKey), New IntPtr(m_UdpKeyInfo.lParamKeyUp)) 'keyup
            m_BackgroundWorker_postedOne = True
            SyncLock m_UdpKeyPostManager
                m_UdpKeyPostManager.Remove(m_WindowHandle.ToString("X16") & "," & m_UdpKeyInfo.Device.ToString("X16") & "," & m_UdpKeyInfo.VKey.ToString("X4"))
            End SyncLock
        End If
        m_BackgroundWorker_isFinished = True
    End Sub

    Public Sub New(ByVal KeyPostManager As UdpKeyPostManager, ByVal WindowHandle As IntPtr, ByVal KeyInfo As UdpKeyInfo)
        m_WindowHandle = WindowHandle
        m_UdpKeyInfo = KeyInfo
        m_UdpKeyPostManager = KeyPostManager
        m_BackgroundWorker.WorkerReportsProgress = False
        m_BackgroundWorker.WorkerSupportsCancellation = True
        If ((m_WindowHandle <> IntPtr.Zero) AndAlso (m_UdpKeyInfo.Valid)) Then
            If (m_UdpKeyInfo.Message And CUInt(1)) = 0 Then 'key down event
                RawInputHook.PostMessage(m_WindowHandle, m_UdpKeyInfo.Message, New IntPtr(m_UdpKeyInfo.VKey), New IntPtr(m_UdpKeyInfo.lParam))
                m_Validated = True
                m_LastKeydownMessageDateTime = Now
            End If
        End If
        m_BackgroundWorker.RunWorkerAsync()
    End Sub

    Public Sub Cancel()
        m_BackgroundWorker_cancelled = True
        m_BackgroundWorker.CancelAsync()
        m_BackgroundWorker_isFinished = True
    End Sub

    Public ReadOnly Property IsFinished() As Boolean
        Get
            Return m_BackgroundWorker_isFinished
        End Get
    End Property
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions