Files
Terminal.Gui/UICatalog/Scenarios/Keys.cs
Tig 6851b42a49 Fixes #2921 - MainLoop refactoring (#2922)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge
2023-10-21 08:06:04 -07:00

178 lines
5.4 KiB
C#

using System.Text;
using System.Collections.Generic;
using Terminal.Gui;
namespace UICatalog.Scenarios {
[ScenarioMetadata (Name: "Keys", Description: "Shows keyboard input handling.")]
[ScenarioCategory ("Mouse and Keyboard")]
public class Keys : Scenario {
class TestWindow : Window {
public List<string> _processKeyList = new List<string> ();
public List<string> _processHotKeyList = new List<string> ();
public List<string> _processColdKeyList = new List<string> ();
public override bool ProcessKey (KeyEvent keyEvent)
{
_processKeyList.Add (keyEvent.ToString ());
return base.ProcessKey (keyEvent);
}
public override bool ProcessHotKey (KeyEvent keyEvent)
{
_processHotKeyList.Add (keyEvent.ToString ());
return base.ProcessHotKey (keyEvent);
}
public override bool ProcessColdKey (KeyEvent keyEvent)
{
_processColdKeyList.Add (keyEvent.ToString ());
return base.ProcessColdKey (keyEvent);
}
}
public override void Init ()
{
Application.Init ();
ConfigurationManager.Themes.Theme = Theme;
ConfigurationManager.Apply ();
Win = new TestWindow () {
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
ColorScheme = Colors.ColorSchemes [TopLevelColorScheme],
};
Application.Top.Add (Win);
}
public override void Setup ()
{
// Type text here: ______
var editLabel = new Label ("Type text here:") {
X = 0,
Y = 0,
};
Win.Add (editLabel);
var edit = new TextField ("") {
X = Pos.Right (editLabel) + 1,
Y = Pos.Top (editLabel),
Width = Dim.Fill (2),
};
Win.Add (edit);
// Last KeyPress: ______
var keyPressedLabel = new Label ("Last Application.KeyPress:") {
X = Pos.Left (editLabel),
Y = Pos.Top (editLabel) + 2,
};
Win.Add (keyPressedLabel);
var labelKeypress = new Label ("") {
X = Pos.Left (edit),
Y = Pos.Top (keyPressedLabel),
TextAlignment = Terminal.Gui.TextAlignment.Centered,
ColorScheme = Colors.Error,
AutoSize = true
};
Win.Add (labelKeypress);
Win.KeyPressed += (s, e) => labelKeypress.Text = e.KeyEvent.ToString ();
// Key stroke log:
var keyLogLabel = new Label ("Key event log:") {
X = Pos.Left (editLabel),
Y = Pos.Top (editLabel) + 4,
};
Win.Add (keyLogLabel);
var fakeKeyPress = new KeyEvent (Key.CtrlMask | Key.A, new KeyModifiers () {
Alt = true,
Ctrl = true,
Shift = true
});
var maxLogEntry = $"Key{"",-5}: {fakeKeyPress}".Length;
var yOffset = (Application.Top == Application.Top ? 1 : 6);
var keyEventlist = new List<string> ();
var keyEventListView = new ListView (keyEventlist) {
X = 0,
Y = Pos.Top (keyLogLabel) + yOffset,
Width = Dim.Percent (30),
Height = Dim.Fill (),
};
keyEventListView.ColorScheme = Colors.TopLevel;
Win.Add (keyEventListView);
// ProcessKey log:
var processKeyLogLabel = new Label ("ProcessKey log:") {
X = Pos.Right (keyEventListView) + 1,
Y = Pos.Top (editLabel) + 4,
};
Win.Add (processKeyLogLabel);
maxLogEntry = $"{fakeKeyPress}".Length;
yOffset = (Application.Top == Application.Top ? 1 : 6);
var processKeyListView = new ListView (((TestWindow)Win)._processKeyList) {
X = Pos.Left (processKeyLogLabel),
Y = Pos.Top (processKeyLogLabel) + yOffset,
Width = Dim.Percent (30),
Height = Dim.Fill (),
};
processKeyListView.ColorScheme = Colors.TopLevel;
Win.Add (processKeyListView);
// ProcessHotKey log:
// BUGBUG: Label is not positioning right with Pos, so using TextField instead
var processHotKeyLogLabel = new Label ("ProcessHotKey log:") {
X = Pos.Right (processKeyListView) + 1,
Y = Pos.Top (editLabel) + 4,
};
Win.Add (processHotKeyLogLabel);
yOffset = (Application.Top == Application.Top ? 1 : 6);
var processHotKeyListView = new ListView (((TestWindow)Win)._processHotKeyList) {
X = Pos.Left (processHotKeyLogLabel),
Y = Pos.Top (processHotKeyLogLabel) + yOffset,
Width = Dim.Percent (20),
Height = Dim.Fill (),
};
processHotKeyListView.ColorScheme = Colors.TopLevel;
Win.Add (processHotKeyListView);
// ProcessColdKey log:
// BUGBUG: Label is not positioning right with Pos, so using TextField instead
var processColdKeyLogLabel = new Label ("ProcessColdKey log:") {
X = Pos.Right (processHotKeyListView) + 1,
Y = Pos.Top (editLabel) + 4,
};
Win.Add (processColdKeyLogLabel);
yOffset = (Application.Top == Application.Top ? 1 : 6);
var processColdKeyListView = new ListView (((TestWindow)Win)._processColdKeyList) {
X = Pos.Left (processColdKeyLogLabel),
Y = Pos.Top (processColdKeyLogLabel) + yOffset,
Width = Dim.Percent (20),
Height = Dim.Fill (),
};
Application.KeyDown += (s, a) => KeyDownPressUp (a, "Down");
Application.KeyPressed += (s, a) => KeyDownPressUp (a, "Press");
Application.KeyUp += (s, a) => KeyDownPressUp (a, "Up");
void KeyDownPressUp (KeyEventEventArgs args, string updown)
{
// BUGBUG: KeyEvent.ToString is badly broken
var msg = $"Key{updown,-5}: {args.KeyEvent}";
keyEventlist.Add (msg);
keyEventListView.MoveDown ();
processKeyListView.MoveDown ();
processColdKeyListView.MoveDown ();
processHotKeyListView.MoveDown ();
}
processColdKeyListView.ColorScheme = Colors.TopLevel;
Win.Add (processColdKeyListView);
}
}
}