Client LuaCsForBarotrauma
EventInput.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Input;
3 
4 namespace EventInput
5 {
6  public readonly record struct CharacterEventArgs(char Character, long Param)
7  {
8  public long RepeatCount => Param & 0xffff;
9  public bool ExtendedKey => (Param & (1 << 24)) > 0;
10  public bool AltPressed => (Param & (1 << 29)) > 0;
11  public bool PreviousState => (Param & (1 << 30)) > 0;
12  public bool TransitionState => (Param & (1 << 31)) > 0;
13  }
14 
15  public readonly record struct KeyEventArgs(Keys KeyCode, char Character);
16 
17  public delegate void CharEnteredHandler(object sender, CharacterEventArgs e);
18  public delegate void KeyEventHandler(object sender, KeyEventArgs e);
19  public delegate void EditingTextHandler(object sender, TextEditingEventArgs e);
20 
21  public static class EventInput
22  {
26  public static event CharEnteredHandler CharEntered;
27 
31  public static event KeyEventHandler KeyDown;
32 
36  public static event KeyEventHandler KeyUp;
37 
41  public static event EditingTextHandler EditingText;
42 
43 
44  static bool initialized;
45 
50  public static void Initialize(GameWindow window)
51  {
52  if (initialized)
53  {
54  return;
55  }
56 
57  window.TextInput += ReceiveInput;
58  window.KeyDown += ReceiveKeyDown;
59  window.TextEditing += ReceiveTextEditing;
60 
61  initialized = true;
62  }
63 
64  private static void ReceiveInput(object sender, TextInputEventArgs e)
65  {
66  OnCharEntered(e.Character);
67  }
68 
69  private static void ReceiveKeyDown(object sender, TextInputEventArgs e)
70  {
71  KeyDown?.Invoke(sender, new KeyEventArgs(e.Key, e.Character));
72  }
73 
74  private static void ReceiveTextEditing(object sender, TextEditingEventArgs e)
75  {
76  EditingText?.Invoke(sender, e);
77  }
78 
79  public static void OnCharEntered(char character)
80  {
81  CharEntered?.Invoke(null, new CharacterEventArgs(character, 0));
82  }
83  }
84 }
delegate void CharEnteredHandler(object sender, CharacterEventArgs e)
delegate void EditingTextHandler(object sender, TextEditingEventArgs e)
readonly record struct KeyEventArgs(Keys KeyCode, char Character)
readonly record struct CharacterEventArgs(char Character, long Param)
Definition: EventInput.cs:6
delegate void KeyEventHandler(object sender, KeyEventArgs e)