Files
Terminal.Gui/UICatalog/Scenarios/Keys.cs
Charlie Kindel 5b845307a2 UI catalog (#387)
* key down/up support

* line endings?

* line endings

* KeyDown/Up support

* line endings

* line endings

* Revert "Drop NuGet restore"

This reverts commit 5c7a0d05f0.

* Revert "Revert "Drop NuGet restore""

This reverts commit 2dc5fce865.

* updated demo

* defined styles

* Smarter StatusBar bottom tracking.

* Prepping for https://github.com/migueldeicaza/gui.cs/issues/376

* Oops.

* Fixed StatusBar 'snap to bottom'

* line endings

* Revert "Fixed StatusBar 'snap to bottom'"

This reverts commit 9a91c957e2.

* started UICatalog project

* Initial working POC.

* Fix newlines

* merge

* textalignment demo tweaks

* textalignment demo tweaks

* Unicode Menu Scenario

* not sure why this keeps changing

* re-added project to .sln file

* re-enabled status bar

* moved scenarios to dir

* building a dim and pos demo

* terminal.sln

* progress...barely

* fixed exit

* progress with some underlying fixes to Label

* added readme

* fixes build issue

* launch

* made default colors readable on Windows

* major UI Catalog upgrade

* added more demos and updated readme

* refactored and added more tests

* added ref to Issue #437

* added OnKeyUp support to Curses and Net drivers

* more tweaks - grab PR #438 first

* Added a OpenSelectedItem event to the ListView #429

* updates

* moved KeyUpHandler out of special ESC stuff

* more tweaks & improvements

* testing top window bug

* supported OpenSelectedItem

* lots of updates

* fixed regression, fixed #444

* better button scenario

* tweaks

* add Ready event to Toplevel

* dotfx .gitignroe

* ready for ready

* updated colors based on feedback; consolodated config code

* tweaked readme

* readme

* Added Editor demonstrating TextView

* Added Editor demonstrating TextView

* added hexeditor scenario

Co-authored-by: Miguel de Icaza <miguel@gnome.org>
Co-authored-by: BDisp <bd.bdisp@gmail.com>
2020-05-20 13:37:12 -04:00

181 lines
5.2 KiB
C#

using NStack;
using System.Collections.Generic;
using Terminal.Gui;
namespace UICatalog {
[ScenarioMetadata (Name: "Keys", Description: "Shows how to handle keyboard input")]
[ScenarioCategory ("Input")]
class Keys : Scenario {
static List<string> _processKeyList = new List<string> ();
static List<string> _processHotKeyList = new List<string> ();
static List<string> _processColdKeyList = new List<string> ();
class TestWindow : Window {
public TestWindow (ustring title = null) : base (title)
{
}
public TestWindow (Rect frame, ustring title = null) : base (frame, title)
{
}
public TestWindow (ustring title = null, int padding = 0) : base (title, padding)
{
}
public TestWindow (Rect frame, ustring title = null, int padding = 0) : base (frame, title, padding)
{
}
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.ProcessHotKey (keyEvent);
}
}
public override void Init (Toplevel top)
{
Top = top;
Win = new TestWindow ($"CTRL-Q to Close - Scenario: {GetName ()}") {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill ()
};
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 KeyPress:") {
X = Pos.Left (editLabel),
Y = Pos.Top (editLabel) + 2,
};
Win.Add (keyPressedLabel);
// BUGBUG: Label is not positioning right with Pos, so using TextField instead
var labelKeypress = new TextField ("") {
X = Pos.Right (keyPressedLabel) + 1,
Y = Pos.Top (keyPressedLabel),
Width = 20,
//TextAlignment = Terminal.Gui.TextAlignment.Left,
ColorScheme = Colors.Error,
};
Win.Add (labelKeypress);
Win.OnKeyPress += (KeyEvent keyEvent) => labelKeypress.Text = keyEvent.ToString ();
// Key stroke log:
var keyLogLabel = new Label ("Key stroke log:") {
X = Pos.Left (editLabel),
Y = Pos.Top (editLabel) + 4,
};
Win.Add (keyLogLabel);
var yOffset = (Top == Application.Top ? 1 : 6);
var keyStrokelist = new List<string> ();
var keyStrokeListView = new ListView (keyStrokelist) {
X = 0,
Y = Pos.Top (keyLogLabel) + yOffset,
Width = 25,
Height = Dim.Fill (),
};
keyStrokeListView.ColorScheme = Colors.TopLevel;
Win.Add (keyStrokeListView);
void KeyDownPressUp (KeyEvent keyEvent, string updown)
{
var msg = $"Key{updown,-5}: {keyEvent.ToString ()}";
keyStrokelist.Add (msg);
keyStrokeListView.MoveDown ();
}
Win.OnKeyDown += (KeyEvent keyEvent) => KeyDownPressUp (keyEvent, "Down");
Win.OnKeyPress += (KeyEvent keyEvent) => KeyDownPressUp (keyEvent, "Press");
Win.OnKeyUp += (KeyEvent keyEvent) => KeyDownPressUp (keyEvent, "Up");
// ProcessKey log:
// BUGBUG: Label is not positioning right with Pos, so using TextField instead
var processKeyLogLabel = new Label ("ProcessKey log:") {
X = Pos.Right (keyStrokeListView) + 1,
Y = Pos.Top (editLabel) + 4,
};
Win.Add (processKeyLogLabel);
yOffset = (Top == Application.Top ? 1 : 6);
var processKeyListView = new ListView (_processKeyList) {
X = Pos.Left (processKeyLogLabel),
Y = Pos.Top (processKeyLogLabel) + yOffset,
Width = 25,
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 = (Top == Application.Top ? 1 : 6);
var processHotKeyListView = new ListView (_processHotKeyList) {
X = Pos.Left (processHotKeyLogLabel),
Y = Pos.Top (processHotKeyLogLabel) + yOffset,
Width = 25,
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 = (Top == Application.Top ? 1 : 6);
var processColdKeyListView = new ListView (_processColdKeyList) {
X = Pos.Left (processColdKeyLogLabel),
Y = Pos.Top (processColdKeyLogLabel) + yOffset,
Width = 25,
Height = Dim.Fill (),
};
processColdKeyListView.ColorScheme = Colors.TopLevel;
Win.Add (processColdKeyListView);
}
}
}