Click here to Skip to main content
15,895,815 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 static int m_timeout_milliseconds = 500;
    private static long m_timeout_ticks = System.TimeSpan.TicksPerMillisecond * m_timeout_milliseconds;
    private System.DateTime m_LastKeydownMessageDateTime = new System.DateTime(0);
	private UdpKeyPostManager m_UdpKeyPostManager = null;
    private System.IntPtr m_WindowHandle = System.IntPtr.Zero;
	private UdpKeyInfo m_UdpKeyInfo = null;
	//private bool m_Validated = false;
	private bool m_BackgroundWorker_cancelled = false;
	private bool m_BackgroundWorker_postedOne = false;
	private bool m_BackgroundWorker_isFinished = false;
    private System.ComponentModel.BackgroundWorker m_BackgroundWorker = new System.ComponentModel.BackgroundWorker(); //Asynchronous Non-Blocking Background Worker
	private void m_BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
	{
		try {
            while (!((System.DateTime.Now.Ticks - m_LastKeydownMessageDateTime.Ticks) > m_timeout_ticks))
            {
				System.Threading.Thread.Sleep(m_timeout_milliseconds);
				lock (m_UdpKeyPostManager) {
					m_LastKeydownMessageDateTime = m_UdpKeyPostManager.LastKeydown(m_WindowHandle, m_UdpKeyInfo);
				}
			}
		} catch (System.Exception ex) {
			System.Diagnostics.Debug.WriteLine(ex.ToString());
		}
	}

	private void m_BackgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
	{
		System.Diagnostics.Debug.WriteLine("completed;    cancelled: " + m_BackgroundWorker_cancelled.ToString() + "     posted: " + m_BackgroundWorker_postedOne.ToString());
		if (((!m_BackgroundWorker_cancelled) && (!m_BackgroundWorker_postedOne))) {
			System.Diagnostics.Debug.WriteLine("keyup timeout");
            RawInputHook.PostMessage(m_WindowHandle, System.Convert.ToInt32(m_UdpKeyInfo.Message | System.Convert.ToInt32(1)), m_UdpKeyInfo.VKey, m_UdpKeyInfo.lParamKeyUp);
			//keyup
			m_BackgroundWorker_postedOne = true;
			lock (m_UdpKeyPostManager) {
				m_UdpKeyPostManager.Remove(m_WindowHandle.ToString("X16") + "," + m_UdpKeyInfo.Device.ToString("X16") + "," + m_UdpKeyInfo.VKey.ToString("X4"));
			}
		}
		m_BackgroundWorker_isFinished = true;
	}

    public UdpKeyPost(UdpKeyPostManager KeyPostManager, System.IntPtr WindowHandle, UdpKeyInfo KeyInfo)
	{
        m_BackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(m_BackgroundWorker_DoWork);
        m_BackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(m_BackgroundWorker_RunWorkerCompleted);
		m_WindowHandle = WindowHandle;
		m_UdpKeyInfo = KeyInfo;
		m_UdpKeyPostManager = KeyPostManager;
		m_BackgroundWorker.WorkerReportsProgress = false;
		m_BackgroundWorker.WorkerSupportsCancellation = true;
		if (((m_WindowHandle !=System.IntPtr.Zero) && (m_UdpKeyInfo.Valid))) {
			//key down event
			if ((m_UdpKeyInfo.Message &System.Convert.ToUInt32(1)) == 0) {
                RawInputHook.PostMessage(m_WindowHandle, m_UdpKeyInfo.Message, m_UdpKeyInfo.VKey, m_UdpKeyInfo.lParam);
//				m_Validated = true;
                m_LastKeydownMessageDateTime = System.DateTime.Now;
			}
		}
		m_BackgroundWorker.RunWorkerAsync();
	}

	public void Cancel()
	{
		m_BackgroundWorker_cancelled = true;
		m_BackgroundWorker.CancelAsync();
		m_BackgroundWorker_isFinished = true;
	}

	public bool IsFinished {
		get { return m_BackgroundWorker_isFinished; }
	}
}


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