瀏覽代碼

Fix for platforms like Android using reflection

Jean-David Moisan 6 年之前
父節點
當前提交
29b7575c54
共有 2 個文件被更改,包括 45 次插入5 次删除
  1. 14 5
      Source/InputHelper.cs
  2. 31 0
      Source/KeyCharacter.cs

+ 14 - 5
Source/InputHelper.cs

@@ -1,4 +1,6 @@
+using System;
 using System.Collections.Generic;
+using System.Reflection;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Input;
 using Microsoft.Xna.Framework.Input.Touch;
@@ -72,7 +74,7 @@ namespace Apos.Input {
         /// <summary>
         /// Useful for handling text inputs from any keyboard layouts. This is useful when coding textboxes.
         /// </summary>
-        public static List<TextInputEventArgs> TextEvents => _textEvents;
+        public static List<KeyCharacter> TextEvents => _textEvents;
 
         // Group: Public Functions
 
@@ -129,8 +131,15 @@ namespace Apos.Input {
 
             _initiated = true;
 
-            Window.TextInput += processTextInput;
-            _textEvents = new List<TextInputEventArgs>();
+            //This is boring but whatever, it only gets called once.
+            //MonoGame doesn't offer TextInput under some platforms.
+            Type t = Window.GetType();
+            EventInfo e = t.GetEvent("TextInput");
+            if (e != null) {
+                Window.TextInput += processTextInput;
+            }
+
+            _textEvents = new List<KeyCharacter>();
         }
         /// <summary>
         /// This function receives TextInput events from the game window.
@@ -138,7 +147,7 @@ namespace Apos.Input {
         /// <param name="sender">This gets ignored.</param>
         /// <param name="e">Contains a character and a key.</param>
         private static void processTextInput(object sender, TextInputEventArgs e) {
-            _textEvents.Add(e);
+            _textEvents.Add(new KeyCharacter(e.Key, e.Character));
         }
 
         // Group: Private Variables
@@ -186,6 +195,6 @@ namespace Apos.Input {
         /// <summary>
         /// Useful for handling text inputs from any keyboard layouts. This is useful when coding textboxes.
         /// </summary>
-        private static List<TextInputEventArgs> _textEvents;
+        private static List<KeyCharacter> _textEvents;
     }
 }

+ 31 - 0
Source/KeyCharacter.cs

@@ -0,0 +1,31 @@
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Stores a key with the character it produces.
+    /// </summary>
+    public class KeyCharacter {
+
+        // Group: Constructors
+
+        /// <param name="key">The key that was pressed on the keyboard.</param>
+        /// <param name="character">A char that can be used in a string or textbox.</param>
+        public KeyCharacter(Keys key, char character) {
+            Key = key;
+            Character = character;
+        }
+
+        // Group: Public Variables
+
+        /// <value>The key that was pressed on the keyboard.</value>
+        public Keys Key {
+            get;
+            set;
+        }
+        /// <value>A char that can be used in a string or textbox.</value>
+        public char Character {
+            get;
+            set;
+        }
+    }
+}