Advertisement
Guest User

Untitled

a guest
Mar 24th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.67 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. //http://www.gamedev.net/topic/457783-xna-getting-text-from-keyboard/
  5. namespace Microsoft.Xna.Framework.Input {
  6.  
  7.     public class CharacterEventArgs : EventArgs {
  8.  
  9.         private readonly char character;
  10.         private readonly int lParam;
  11.  
  12.         public CharacterEventArgs(char character, int lParam) {
  13.             this.character = character;
  14.             this.lParam = lParam;
  15.         }
  16.  
  17.         public char Character { get { return character; } }
  18.         public int Param { get { return lParam; } }
  19.         public int RepeatCount { get { return lParam & 0xffff; } }
  20.         public bool ExtendedKey { get { return (lParam & (1 << 24)) > 0; } }
  21.         public bool AltPressed { get { return (lParam & (1 << 29)) > 0; } }
  22.         public bool PreviousState { get { return (lParam & (1 << 30)) > 0; } }
  23.         public bool TransitionState { get { return (lParam & (1 << 31)) > 0; } }
  24.  
  25.     }
  26.  
  27.     public class KeyEventArgs : EventArgs {
  28.  
  29.         private Keys keyCode;
  30.  
  31.         public KeyEventArgs(Keys keyCode) {
  32.             this.keyCode = keyCode;
  33.         }
  34.  
  35.         public Keys KeyCode { get { return keyCode; } }
  36.     }
  37.  
  38.     public class MouseEventArgs : EventArgs {
  39.         private MouseButton button;
  40.         private int clicks;
  41.         private int x;
  42.         private int y;
  43.         private int delta;
  44.  
  45.         public MouseButton Button { get { return button; } }
  46.         public int Clicks { get { return clicks; } }
  47.         public int X { get { return x; } }
  48.         public int Y { get { return y; } }
  49.         public Point Location { get { return new Point(x, y); } }
  50.         public int Delta { get { return delta; } }
  51.  
  52.         public MouseEventArgs(MouseButton button, int clicks, int x, int y, int delta) {
  53.             this.button = button;
  54.             this.clicks = clicks;
  55.             this.x = x;
  56.             this.y = y;
  57.             this.delta = delta;
  58.         }
  59.  
  60.     }
  61.  
  62.     public delegate void CharEnteredHandler(object sender, CharacterEventArgs e);
  63.     public delegate void KeyEventHandler(object sender, KeyEventArgs e);
  64.     public delegate void MouseEventHandler(object sender, MouseEventArgs e);
  65.  
  66.     /// <summary>   /// Mouse Key Flags from WinUser.h for mouse related WM messages.</summary>
  67.     [Flags]
  68.     public enum MouseKeys {
  69.         LButton = 0x01,
  70.         RButton = 0x02,
  71.         Shift = 0x04,
  72.         Control = 0x08,
  73.         MButton = 0x10,
  74.         XButton1 = 0x20,
  75.         XButton2 = 0x40
  76.     }
  77.     public enum MouseButton {
  78.         None,
  79.         Left,
  80.         Right,
  81.         Middle,
  82.         X1,
  83.         X2
  84.     }
  85.  
  86.     public static class InputSystem {
  87.  
  88.         #region Events
  89.  
  90.         /// <summary>Event raised when a character has been entered.</summary>     
  91.         public static event CharEnteredHandler CharEntered;
  92.  
  93.         /// <summary>Event raised when a key has been pressed down. May fire multiple times due to keyboard repeat.</summary>      
  94.         public static event KeyEventHandler KeyDown;
  95.  
  96.         /// <summary>Event raised when a key has been released.</summary>
  97.         public static event KeyEventHandler KeyUp;
  98.  
  99.         /// <summary>Event raised when a mouse button is pressed.</summary>
  100.         public static event MouseEventHandler MouseDown;
  101.  
  102.         /// <summary>Event raised when a mouse button is released.</summary>   
  103.         public static event MouseEventHandler MouseUp;
  104.  
  105.         /// <summary>Event raised when the mouse changes location.</summary>
  106.         public static event MouseEventHandler MouseMove;
  107.  
  108.         /// <summary>Event raised when the mouse has hovered in the same location for a short period of time.</summary>    
  109.         public static event MouseEventHandler MouseHover;
  110.  
  111.         /// <summary>Event raised when the mouse wheel has been moved.</summary>   
  112.         public static event MouseEventHandler MouseWheel;
  113.  
  114.         /// <summary>Event raised when a mouse button has been double clicked.</summary>   
  115.         public static event MouseEventHandler MouseDoubleClick;
  116.  
  117.         #endregion
  118.  
  119.         delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
  120.         public static bool Initialized { get; private set; }
  121.         static IntPtr prevWndProc;
  122.         static WndProc hookProcDelegate;
  123.         static IntPtr hIMC;
  124.  
  125.         #region Win32 Constants
  126.         const int GWL_WNDPROC = -4;
  127.         const int WM_KEYDOWN = 0x100;
  128.         const int WM_KEYUP = 0x101;
  129.         const int WM_CHAR = 0x102;
  130.         const int WM_IME_SETCONTEXT = 0x281;
  131.         const int WM_INPUTLANGCHANGE = 0x51;
  132.         const int WM_GETDLGCODE = 0x87;
  133.         const int WM_IME_COMPOSITION = 0x10F;
  134.         const int DLGC_WANTALLKEYS = 4;
  135.         const int WM_MOUSEMOVE = 0x200;
  136.         const int WM_LBUTTONDOWN = 0x201;
  137.         const int WM_LBUTTONUP = 0x202;
  138.         const int WM_LBUTTONDBLCLK = 0x203;
  139.         const int WM_RBUTTONDOWN = 0x204;
  140.         const int WM_RBUTTONUP = 0x205;
  141.         const int WM_RBUTTONDBLCLK = 0x206;
  142.         const int WM_MBUTTONDOWN = 0x207;
  143.         const int WM_MBUTTONUP = 0x208;
  144.         const int WM_MBUTTONDBLCLK = 0x209;
  145.         const int WM_MOUSEWHEEL = 0x20A;
  146.         const int WM_XBUTTONDOWN = 0x20B;
  147.         const int WM_XBUTTONUP = 0x20C;
  148.         const int WM_XBUTTONDBLCLK = 0x20D;
  149.         const int WM_MOUSEHOVER = 0x2A1;
  150.         #endregion
  151.  
  152.         #region DLL Imports
  153.         [DllImport("Imm32.dll")]
  154.         static extern IntPtr ImmGetContext(IntPtr hWnd);
  155.  
  156.         [DllImport("Imm32.dll")]
  157.         static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
  158.  
  159.         [DllImport("user32.dll")]
  160.         static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  161.  
  162.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  163.         static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  164.         #endregion
  165.  
  166.         public static Point MouseLocation {
  167.             get {
  168.                 MouseState state = Mouse.GetState();
  169.                 return new Point(state.X, state.Y);
  170.             }
  171.         }
  172.  
  173.         public static bool ShiftDown {
  174.             get {
  175.                 KeyboardState state = Keyboard.GetState();
  176.                 return state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift);
  177.             }
  178.         }
  179.  
  180.         public static bool CtrlDown {
  181.             get {
  182.                 KeyboardState state = Keyboard.GetState();
  183.                 return state.IsKeyDown(Keys.LeftControl) || state.IsKeyDown(Keys.RightControl);
  184.             }
  185.         }
  186.  
  187.         public static bool AltDown {
  188.             get {
  189.                 KeyboardState state = Keyboard.GetState();
  190.                 return state.IsKeyDown(Keys.LeftAlt) || state.IsKeyDown(Keys.RightAlt);
  191.             }
  192.         }
  193.  
  194.         /// <summary>Initialize the TextInput with the given GameWindow.</summary> 
  195.         /// <param name="window">The XNA window to which text input should be linked.</param>  
  196.         public static void Initialize(GameWindow window) {
  197.             if (Initialized) { return; }
  198.  
  199.             hookProcDelegate = HookProc;
  200.             prevWndProc = (IntPtr)SetWindowLong(window.Handle, GWL_WNDPROC, (int)Marshal.GetFunctionPointerForDelegate(hookProcDelegate));
  201.             hIMC = ImmGetContext(window.Handle);
  202.  
  203.             Initialized = true;
  204.         }
  205.  
  206.         static IntPtr HookProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) {
  207.             IntPtr returnCode = CallWindowProc(prevWndProc, hWnd, msg, wParam, lParam);
  208.  
  209.             switch (msg) {
  210.                 case WM_GETDLGCODE:
  211.                     returnCode = (IntPtr)(returnCode.ToInt32() | DLGC_WANTALLKEYS);
  212.                     break;
  213.                 case WM_KEYDOWN:
  214.                     if (KeyDown != null)
  215.                         KeyDown(null, new KeyEventArgs((Keys)wParam));
  216.                     break;
  217.                 case WM_KEYUP:
  218.                     if (KeyUp != null)
  219.                         KeyUp(null, new KeyEventArgs((Keys)wParam));
  220.                     break;
  221.                 case WM_CHAR:
  222.                     if (CharEntered != null)
  223.                         CharEntered(null, new CharacterEventArgs((char)wParam, lParam.ToInt32()));
  224.                     break;
  225.                 case WM_IME_SETCONTEXT:
  226.                     if (wParam.ToInt32() == 1)
  227.                         ImmAssociateContext(hWnd, hIMC);
  228.                     break;
  229.                 case WM_INPUTLANGCHANGE:
  230.                     ImmAssociateContext(hWnd, hIMC);
  231.                     returnCode = (IntPtr)1;
  232.                     break;
  233.  
  234.                 // Mouse messages          
  235.                 case WM_MOUSEMOVE:
  236.                     if (MouseMove != null) {
  237.                         short x, y;
  238.                         MouseLocationFromLParam(lParam.ToInt32(), out x, out y);
  239.                         MouseMove(null, new MouseEventArgs(MouseButton.None, 0, x, y, 0));
  240.                     }
  241.                     break;
  242.                 case WM_MOUSEHOVER:
  243.                     if (MouseHover != null) {
  244.                         short x, y;
  245.                         MouseLocationFromLParam(lParam.ToInt32(), out x, out y);
  246.                         MouseHover(null, new MouseEventArgs(MouseButton.None, 0, x, y, 0));
  247.                     }
  248.                     break;
  249.                 case WM_MOUSEWHEEL:
  250.                     if (MouseWheel != null) {
  251.                         short x, y;
  252.                         MouseLocationFromLParam(lParam.ToInt32(), out x, out y);
  253.                         MouseWheel(null, new MouseEventArgs(MouseButton.None, 0, x, y, (wParam.ToInt32() >> 16) / 120));
  254.                     }
  255.                     break;
  256.                 case WM_LBUTTONDOWN:
  257.                     RaiseMouseDownEvent(MouseButton.Left, wParam.ToInt32(), lParam.ToInt32());
  258.                     break;
  259.                 case WM_LBUTTONUP:
  260.                     RaiseMouseUpEvent(MouseButton.Left, wParam.ToInt32(), lParam.ToInt32());
  261.                     break;
  262.                 case WM_LBUTTONDBLCLK:
  263.                     RaiseMouseDblClickEvent(MouseButton.Left, wParam.ToInt32(), lParam.ToInt32());
  264.                     break;
  265.                 case WM_RBUTTONDOWN:
  266.                     RaiseMouseDownEvent(MouseButton.Right, wParam.ToInt32(), lParam.ToInt32());
  267.                     break;
  268.                 case WM_RBUTTONUP:
  269.                     RaiseMouseUpEvent(MouseButton.Right, wParam.ToInt32(), lParam.ToInt32());
  270.                     break;
  271.                 case WM_RBUTTONDBLCLK:
  272.                     RaiseMouseDblClickEvent(MouseButton.Right, wParam.ToInt32(), lParam.ToInt32());
  273.                     break;
  274.                 case WM_MBUTTONDOWN:
  275.                     RaiseMouseDownEvent(MouseButton.Middle, wParam.ToInt32(), lParam.ToInt32());
  276.                     break;
  277.                 case WM_MBUTTONUP:
  278.                     RaiseMouseUpEvent(MouseButton.Middle, wParam.ToInt32(), lParam.ToInt32());
  279.                     break;
  280.                 case WM_MBUTTONDBLCLK:
  281.                     RaiseMouseDblClickEvent(MouseButton.Middle, wParam.ToInt32(), lParam.ToInt32());
  282.                     break;
  283.                 case WM_XBUTTONDOWN:
  284.                     if ((wParam.ToInt32() & 0x10000) != 0) {
  285.                         RaiseMouseDownEvent(MouseButton.X1, wParam.ToInt32(), lParam.ToInt32());
  286.                     } else if ((wParam.ToInt32() & 0x20000) != 0) {
  287.                         RaiseMouseDownEvent(MouseButton.X2, wParam.ToInt32(), lParam.ToInt32());
  288.                     }
  289.                     break;
  290.                 case WM_XBUTTONUP:
  291.                     if ((wParam.ToInt32() & 0x10000) != 0) {
  292.                         RaiseMouseUpEvent(MouseButton.X1, wParam.ToInt32(), lParam.ToInt32());
  293.                     } else if ((wParam.ToInt32() & 0x20000) != 0) {
  294.                         RaiseMouseUpEvent(MouseButton.X2, wParam.ToInt32(), lParam.ToInt32());
  295.                     }
  296.                     break;
  297.                 case WM_XBUTTONDBLCLK:
  298.                     if ((wParam.ToInt32() & 0x10000) != 0) {
  299.                         RaiseMouseDblClickEvent(MouseButton.X1, wParam.ToInt32(), lParam.ToInt32());
  300.                     } else if ((wParam.ToInt32() & 0x20000) != 0) {
  301.                         RaiseMouseDblClickEvent(MouseButton.X2, wParam.ToInt32(), lParam.ToInt32());
  302.                     }
  303.                     break;
  304.             }
  305.  
  306.             return returnCode;
  307.         }
  308.  
  309.         #region Mouse Message Helpers
  310.         static void RaiseMouseDownEvent(MouseButton button, int wParam, int lParam) {
  311.             if (MouseDown != null) {
  312.                 short x, y;
  313.                 MouseLocationFromLParam(lParam, out x, out y);
  314.                 MouseDown(null, new MouseEventArgs(button, 1, x, y, 0));
  315.             }
  316.         }
  317.  
  318.         static void RaiseMouseUpEvent(MouseButton button, int wParam, int lParam) {
  319.             if (MouseUp != null) {
  320.                 short x, y;
  321.                 MouseLocationFromLParam(lParam, out x, out y);
  322.                 MouseUp(null, new MouseEventArgs(button, 1, x, y, 0));
  323.             }
  324.         }
  325.  
  326.         static void RaiseMouseDblClickEvent(MouseButton button, int wParam, int lParam) {
  327.             if (MouseDoubleClick != null) {
  328.                 short x, y;
  329.                 MouseLocationFromLParam(lParam, out x, out y);
  330.                 MouseDoubleClick(null, new MouseEventArgs(button, 1, x, y, 0));
  331.             }
  332.         }
  333.  
  334.         static void MouseLocationFromLParam(int lParam, out short x, out short y) {
  335.             // Cast to signed shorts to get sign extension on negative coordinates (of course this would only be possible if mouse capture was enabled).   
  336.             x = (short)(lParam & 0xFFFF);
  337.             y = (short)(lParam >> 16);
  338.         }
  339.         #endregion
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement