We need forms and interop (call dll) so need to either edit our own and/or template version of Program.cs with these replacement lines in the header:
#if WINDOWS
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
#endif
In target/modules/xna/native/xnagame.cs replace last blocks of public virtual void Run() implementation with this:
#if WINDOWS
_form = System.Windows.Forms.Form.FromHandle(_app.Window.Handle) as System.Windows.Forms.Form;
if (MonkeyConfig.XNA_WINDOW_FULLSCREEN != "1")
{
_form.FormClosing+=FormClosing;
}
MessageFilter keyboardFilter = new MessageFilter();
System.Windows.Forms.Application.AddMessageFilter(keyboardFilter);
#endif
and add this code where we intercept the windows message pump for our form and feed monkey non xna based keyboard events:
public class MessageFilter : IMessageFilter{
[DllImport("user32.dll")]
static extern bool TranslateMessage(ref Message lpMsg);
const int WM_CHAR = 0x0102;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
public bool PreFilterMessage(ref Message m){
switch(m.Msg){
case WM_CHAR:
int wp = m.WParam.ToInt32();
if (wp > 31)
{
Game().KeyEvent(3, wp);
}
return true;
case WM_KEYDOWN:
if(TranslateMessage(ref m)){
Game().KeyEvent(1,m.WParam.ToInt32());
}
return true;
case WM_KEYUP:
if (TranslateMessage(ref m)){
Game().KeyEvent(2, m.WParam.ToInt32());
}
return true;
default:
break;
}
return false;
}
}
and replace PollKeyboard and PollMouse with these
|