Skip to content

Commit

Permalink
Refactored KeyEvent and KeyEventEventArgs into a single class
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Oct 20, 2023
1 parent f3ee290 commit 88a0065
Show file tree
Hide file tree
Showing 101 changed files with 1,998 additions and 2,018 deletions.
37 changes: 20 additions & 17 deletions Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static List<CultureInfo> GetSupportedCultures ()
#region Initialization (Init/Shutdown)

/// <summary>
/// Initializes a new instance of <see cref="Terminal.Gui"/> Application.
/// Initializes a new instance of a <see cref="Terminal.Gui"/> Application.
/// </summary>
/// <para>
/// Call this method once per instance (or after <see cref="Shutdown"/> has been called).
Expand Down Expand Up @@ -1064,7 +1064,6 @@ public static void GrabMouse (View view)
if (!OnGrabbingMouse (view)) {
OnGrabbedMouse (view);
_mouseGrabView = view;
//Driver.UncookMouse ();
}
}

Expand Down Expand Up @@ -1121,8 +1120,8 @@ static void OnUnGrabbedMouse (View view)
/// </summary>
/// <remarks>
/// <para>
/// Use this event to receive mouse events in screen coordinates. Use <see cref="Responder.MouseEvent"/> to receive
/// mouse events relative to a <see cref="View"/>'s bounds.
/// Use this event to receive all mouse events in screen coordinates. Use <see cref="Responder.MouseEvent"/> to receive
/// mouse events specific to a <see cref="View"/>'s bounds.
/// </para>
/// <para>
/// The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.
Expand Down Expand Up @@ -1279,7 +1278,7 @@ static void OnAlternateBackwardKeyChanged (KeyChangedEventArgs oldKey)
static Key _quitKey = Key.Q | Key.CtrlMask;

/// <summary>
/// Gets or sets the key to quit the application.
/// Gets or sets the key to quit the application. The default is Ctrl-Q.
/// </summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope)), JsonConverter (typeof (KeyJsonConverter))]
public static Key QuitKey {
Expand All @@ -1302,13 +1301,17 @@ static void OnQuitKeyChanged (KeyChangedEventArgs e)

/// <summary>
/// Event fired after a key has been pressed and released.
/// <para>Set <see cref="KeyEventEventArgs.Handled"/> to <see langword="true"/> to suppress the event.</para>
/// <para>Set <see cref="KeyEventArgs.Handled"/> to <see langword="true"/> to suppress the event.</para>
/// </summary>
/// <seealso cref="Responder.KeyPressed"/>.
/// <remarks>
/// <para>
///
/// </para>
/// All drivers support firing the <see cref="KeyPressed"/> event. Some drivers (Curses)
/// do not support firing the <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
/// </remarks>
public static event EventHandler<KeyEventEventArgs> KeyPressed;
public static event EventHandler<KeyEventArgs> KeyPressed;

/// <summary>
/// Called after a key has been pressed and released. Fires the <see cref="KeyPressed"/> event.
Expand All @@ -1319,7 +1322,7 @@ static void OnQuitKeyChanged (KeyChangedEventArgs e)
/// </summary>
/// <param name="a"></param>
/// <returns><see langword="true"/> if the key was handled.</returns>
public static bool OnKeyPressed (KeyEventEventArgs a)
public static bool OnKeyPressed (KeyEventArgs a)
{
KeyPressed?.Invoke (null, a);
if (a.Handled) {
Expand All @@ -1328,15 +1331,15 @@ public static bool OnKeyPressed (KeyEventEventArgs a)

var chain = _topLevels.ToList ();
foreach (var topLevel in chain) {
if (topLevel.ProcessHotKey (a.KeyEvent)) {
if (topLevel.ProcessHotKey (a)) {
return true;
}
if (topLevel.Modal)
break;
}

foreach (var topLevel in chain) {
if (topLevel.ProcessKey (a.KeyEvent)) {
if (topLevel.OnKeyPressed (a)) {
return true;
}
if (topLevel.Modal)
Expand All @@ -1345,7 +1348,7 @@ public static bool OnKeyPressed (KeyEventEventArgs a)

foreach (var topLevel in chain) {
// Process the key normally
if (topLevel.ProcessColdKey (a.KeyEvent)) {
if (topLevel.ProcessColdKey (a)) {
return true;
}
if (topLevel.Modal)
Expand All @@ -1361,18 +1364,18 @@ public static bool OnKeyPressed (KeyEventEventArgs a)
/// All drivers support firing the <see cref="KeyPressed"/> event. Some drivers (Curses)
/// do not support firing the <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
/// </remarks>
public static event EventHandler<KeyEventEventArgs> KeyDown;
public static event EventHandler<KeyEventArgs> KeyDown;

/// <summary>
/// Called when a key is pressed (and not yet released). Fires the <see cref="KeyDown"/> event.
/// </summary>
/// <param name="a"></param>
public static void OnKeyDown (KeyEventEventArgs a)
public static void OnKeyDown (KeyEventArgs a)
{
KeyDown?.Invoke (null, a);
var chain = _topLevels.ToList ();
foreach (var topLevel in chain) {
if (topLevel.OnKeyDown (a.KeyEvent))
if (topLevel.OnKeyDown (a))
return;
if (topLevel.Modal)
break;
Expand All @@ -1386,18 +1389,18 @@ public static void OnKeyDown (KeyEventEventArgs a)
/// All drivers support firing the <see cref="KeyPressed"/> event. Some drivers (Curses)
/// do not support firing the <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
/// </remarks>
public static event EventHandler<KeyEventEventArgs> KeyUp;
public static event EventHandler<KeyEventArgs> KeyUp;

/// <summary>
/// Called when a key is released. Fires the <see cref="KeyUp"/> event.
/// </summary>
/// <param name="a"></param>
public static void OnKeyUp (KeyEventEventArgs a)
public static void OnKeyUp (KeyEventArgs a)
{
KeyUp?.Invoke (null, a);
var chain = _topLevels.ToList ();
foreach (var topLevel in chain) {
if (topLevel.OnKeyUp (a.KeyEvent))
if (topLevel.OnKeyUp (a))
return;
if (topLevel.Modal)
break;
Expand Down
12 changes: 6 additions & 6 deletions Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,35 +447,35 @@ public virtual Attribute MakeColor (Color foreground, Color background)
/// <summary>
/// Event fired after a key has been pressed and released.
/// </summary>
public event EventHandler<KeyEventEventArgs> KeyPressed;
public event EventHandler<KeyEventArgs> KeyPressed;

/// <summary>
/// Called after a key has been pressed and released. Fires the <see cref="KeyPressed"/> event.
/// </summary>
/// <param name="a"></param>
public void OnKeyPressed (KeyEventEventArgs a) => KeyPressed?.Invoke(this, a);
public void OnKeyPressed (KeyEventArgs a) => KeyPressed?.Invoke(this, a);

/// <summary>
/// Event fired when a key is released.
/// </summary>
public event EventHandler<KeyEventEventArgs> KeyUp;
public event EventHandler<KeyEventArgs> KeyUp;

/// <summary>
/// Called when a key is released. Fires the <see cref="KeyUp"/> event.
/// </summary>
/// <param name="a"></param>
public void OnKeyUp (KeyEventEventArgs a) => KeyUp?.Invoke (this, a);
public void OnKeyUp (KeyEventArgs a) => KeyUp?.Invoke (this, a);

/// <summary>
/// Event fired when a key is pressed.
/// </summary>
public event EventHandler<KeyEventEventArgs> KeyDown;
public event EventHandler<KeyEventArgs> KeyDown;

/// <summary>
/// Called when a key is pressed. Fires the <see cref="KeyDown"/> event.
/// </summary>
/// <param name="a"></param>
public void OnKeyDown (KeyEventEventArgs a) => KeyDown?.Invoke (this, a);
public void OnKeyDown (KeyEventArgs a) => KeyDown?.Invoke (this, a);

/// <summary>
/// Event fired when a mouse event occurs.
Expand Down
50 changes: 25 additions & 25 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ internal void ProcessInput ()
int wch2 = wch;

while (wch2 == Curses.KeyMouse) {
KeyEvent key = null;
KeyEventArgs key = null;
ConsoleKeyInfo [] cki = new ConsoleKeyInfo [] {
new ConsoleKeyInfo ((char)Key.Esc, 0, false, false, false),
new ConsoleKeyInfo ('[', 0, false, false, false),
Expand Down Expand Up @@ -491,9 +491,9 @@ internal void ProcessInput ()
wch -= 60;
k = Key.ShiftMask | Key.AltMask | MapCursesKey (wch);
}
OnKeyDown (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyDown (new KeyEventArgs (k, MapKeyModifiers (k)));
OnKeyUp (new KeyEventArgs (k, MapKeyModifiers (k)));
OnKeyPressed (new KeyEventArgs (k, MapKeyModifiers (k)));
return;
}

Expand All @@ -507,7 +507,7 @@ internal void ProcessInput ()
k = Key.AltMask | MapCursesKey (wch);
}
if (code == 0) {
KeyEvent key = null;
KeyEventArgs key = null;

// The ESC-number handling, debatable.
// Simulates the AltMask itself by pressing Alt + Space.
Expand Down Expand Up @@ -543,19 +543,19 @@ internal void ProcessInput ()
k = (Key)((uint)(Key.AltMask | Key.CtrlMask) + wch2);
}
}
key = new KeyEvent (k, MapKeyModifiers (k));
OnKeyDown (new KeyEventEventArgs (key));
OnKeyUp (new KeyEventEventArgs (key));
OnKeyPressed (new KeyEventEventArgs (key));
key = new KeyEventArgs (k, MapKeyModifiers (k));
OnKeyDown (key);
OnKeyUp (key);
OnKeyPressed (key);
} else {
k = Key.Esc;
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyPressed (new KeyEventArgs (k, MapKeyModifiers (k)));
}
} else if (wch == Curses.KeyTab) {
k = MapCursesKey (wch);
OnKeyDown (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyDown (new KeyEventArgs (k, MapKeyModifiers (k)));
OnKeyUp (new KeyEventArgs (k, MapKeyModifiers (k)));
OnKeyPressed (new KeyEventArgs (k, MapKeyModifiers (k)));
} else {
// Unfortunately there are no way to differentiate Ctrl+alfa and Ctrl+Shift+alfa.
k = (Key)wch;
Expand All @@ -568,21 +568,21 @@ internal void ProcessInput ()
} else if (wch >= (uint)Key.A && wch <= (uint)Key.Z) {
_keyModifiers.Shift = true;
}
OnKeyDown (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (k, MapKeyModifiers (k))));
OnKeyDown (new KeyEventArgs (k, MapKeyModifiers (k)));
OnKeyUp (new KeyEventArgs (k, MapKeyModifiers (k)));
OnKeyPressed (new KeyEventArgs (k, MapKeyModifiers (k)));
}
// Cause OnKeyUp and OnKeyPressed. Note that the special handling for ESC above
// will not impact KeyUp.
// This is causing ESC firing even if another keystroke was handled.
//if (wch == Curses.KeyTab) {
// keyUpHandler (new KeyEvent (MapCursesKey (wch), keyModifiers));
// keyUpHandler (new KeyEventArgs (MapCursesKey (wch), keyModifiers));
//} else {
// keyUpHandler (new KeyEvent ((Key)wch, keyModifiers));
// keyUpHandler (new KeyEventArgs ((Key)wch, keyModifiers));
//}
}

void HandleEscSeqResponse (ref int code, ref Key k, ref int wch2, ref KeyEvent key, ref ConsoleKeyInfo [] cki)
void HandleEscSeqResponse (ref int code, ref Key k, ref int wch2, ref KeyEventArgs key, ref ConsoleKeyInfo [] cki)
{
ConsoleKey ck = 0;
ConsoleModifiers mod = 0;
Expand All @@ -603,9 +603,9 @@ void HandleEscSeqResponse (ref int code, ref Key k, ref int wch2, ref KeyEvent k
} else {
k = ConsoleKeyMapping.MapConsoleKeyToKey (consoleKeyInfo.Key, out _);
k = ConsoleKeyMapping.MapKeyModifiers (consoleKeyInfo, k);
key = new KeyEvent (k, MapKeyModifiers (k));
OnKeyDown (new KeyEventEventArgs (key));
OnKeyPressed (new KeyEventEventArgs (key));
key = new KeyEventArgs (k, MapKeyModifiers (k));
OnKeyDown (key);
OnKeyPressed (key);
}
} else {
cki = EscSeqUtils.ResizeArray (consoleKeyInfo, cki);
Expand Down Expand Up @@ -778,9 +778,9 @@ public override void SendKeys (char keyChar, ConsoleKey consoleKey, bool shift,
key |= Key.CtrlMask;
km.Ctrl = control;
}
OnKeyDown (new KeyEventEventArgs (new KeyEvent (key, km)));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (key, km)));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (key, km)));
OnKeyDown (new KeyEventArgs (key, km));
OnKeyPressed (new KeyEventArgs (key, km));
OnKeyUp (new KeyEventArgs (key, km));
}


Expand Down
10 changes: 5 additions & 5 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,15 @@ void ProcessInput (ConsoleKeyInfo consoleKey)
var map = MapKey (consoleKey);
if (map == (Key)0xffffffff) {
if ((consoleKey.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
OnKeyDown(new KeyEventEventArgs(new KeyEvent (map, keyModifiers)));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (map, keyModifiers)));
OnKeyDown(new KeyEventArgs (map, keyModifiers));
OnKeyUp (new KeyEventArgs (map, keyModifiers));
}
return;
}

OnKeyDown (new KeyEventEventArgs (new KeyEvent (map, keyModifiers)));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (map, keyModifiers)));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (map, keyModifiers)));
OnKeyDown (new KeyEventArgs (map, keyModifiers));
OnKeyUp (new KeyEventArgs (map, keyModifiers));
OnKeyPressed (new KeyEventArgs (map, keyModifiers));
}

/// <inheritdoc/>
Expand Down
10 changes: 5 additions & 5 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,12 +1125,12 @@ void ProcessInput (NetEvents.InputResult inputEvent)
return;
}
if (map == Key.Null) {
OnKeyDown (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyDown (new KeyEventArgs (map, _keyModifiers));
OnKeyUp (new KeyEventArgs (map, _keyModifiers));
} else {
OnKeyDown (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyUp (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyDown (new KeyEventArgs (map, _keyModifiers));
OnKeyUp (new KeyEventArgs (map, _keyModifiers));
OnKeyPressed (new KeyEventArgs (map, _keyModifiers));
}
break;
case NetEvents.EventType.Mouse:
Expand Down
28 changes: 14 additions & 14 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
//System.Diagnostics.Debug.WriteLine ($"wVirtualScanCode: {ke.wVirtualScanCode}");

if (map == (Key)0xffffffff) {
KeyEvent key = new KeyEvent ();
KeyEventArgs key = new KeyEventArgs ();

// Shift = VK_SHIFT = 0x10
// Ctrl = VK_CONTROL = 0x11
Expand All @@ -926,17 +926,17 @@ internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
WindowsConsole.ControlKeyState.LeftControlPressed |
WindowsConsole.ControlKeyState.EnhancedKey:
case WindowsConsole.ControlKeyState.EnhancedKey:
key = new KeyEvent (Key.CtrlMask | Key.AltMask, _keyModifiers);
key = new KeyEventArgs (Key.CtrlMask | Key.AltMask, _keyModifiers);
break;
case WindowsConsole.ControlKeyState.LeftAltPressed:
key = new KeyEvent (Key.AltMask, _keyModifiers);
key = new KeyEventArgs (Key.AltMask, _keyModifiers);
break;
case WindowsConsole.ControlKeyState.RightControlPressed:
case WindowsConsole.ControlKeyState.LeftControlPressed:
key = new KeyEvent (Key.CtrlMask, _keyModifiers);
key = new KeyEventArgs (Key.CtrlMask, _keyModifiers);
break;
case WindowsConsole.ControlKeyState.ShiftPressed:
key = new KeyEvent (Key.ShiftMask, _keyModifiers);
key = new KeyEventArgs (Key.ShiftMask, _keyModifiers);
break;
case WindowsConsole.ControlKeyState.NumlockOn:
break;
Expand All @@ -946,28 +946,28 @@ internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
break;
default:
key = inputEvent.KeyEvent.wVirtualKeyCode switch {
0x10 => new KeyEvent (Key.ShiftMask, _keyModifiers),
0x11 => new KeyEvent (Key.CtrlMask, _keyModifiers),
0x12 => new KeyEvent (Key.AltMask, _keyModifiers),
_ => new KeyEvent (Key.Unknown, _keyModifiers)
0x10 => new KeyEventArgs (Key.ShiftMask, _keyModifiers),
0x11 => new KeyEventArgs (Key.CtrlMask, _keyModifiers),
0x12 => new KeyEventArgs (Key.AltMask, _keyModifiers),
_ => new KeyEventArgs (Key.Unknown, _keyModifiers)
};
break;
}

if (inputEvent.KeyEvent.bKeyDown) {
OnKeyDown (new KeyEventEventArgs (key));
OnKeyDown (key);
} else {
OnKeyUp (new KeyEventEventArgs (key));
OnKeyUp (key);
}
} else {
if (inputEvent.KeyEvent.bKeyDown) {
// May occurs using SendKeys
_keyModifiers ??= new KeyModifiers ();
// Key Down - Fire KeyDown Event and KeyPressed Event
OnKeyDown (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyDown (new KeyEventArgs (map, _keyModifiers));
} else {
OnKeyUp (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyPressed (new KeyEventEventArgs (new KeyEvent (map, _keyModifiers)));
OnKeyUp (new KeyEventArgs (map, _keyModifiers));
OnKeyPressed (new KeyEventArgs (map, _keyModifiers));
}
}
if (!inputEvent.KeyEvent.bKeyDown && inputEvent.KeyEvent.dwControlKeyState == 0) {
Expand Down
Loading

0 comments on commit 88a0065

Please sign in to comment.