Files
Terminal.Gui/UICatalog/Scenarios/SendKeys.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

127 lines
2.8 KiB
C#

using System;
using Terminal.Gui;
namespace UICatalog.Scenarios {
[ScenarioMetadata (Name: "SendKeys", Description: "SendKeys sample - Send key combinations.")]
[ScenarioCategory ("Mouse and Keyboard")]
public class SendKeys : Scenario {
public override void Setup ()
{
var label = new Label ("Insert the text to send:") {
X = Pos.Center (),
Y = Pos.Center () - 6
};
Win.Add (label);
var txtInput = new TextField ("MockKeyPresses") {
X = Pos.Center (),
Y = Pos.Center () - 5,
Width = 20
};
Win.Add (txtInput);
var ckbShift = new CheckBox ("Shift") {
X = Pos.Center (),
Y = Pos.Center () - 4
};
Win.Add (ckbShift);
var ckbAlt = new CheckBox ("Alt") {
X = Pos.Center (),
Y = Pos.Center () - 3
};
Win.Add (ckbAlt);
var ckbControl = new CheckBox ("Control") {
X = Pos.Center (),
Y = Pos.Center () - 2
};
Win.Add (ckbControl);
label = new Label ("Result keys:") {
X = Pos.Center (),
Y = Pos.Center () + 1
};
Win.Add (label);
var txtResult = new TextField () {
X = Pos.Center (),
Y = Pos.Center () + 2,
Width = 20,
};
Win.Add (txtResult);
var rKeys = "";
var rControlKeys = "";
var IsShift = false;
var IsAlt = false;
var IsCtrl = false;
txtResult.KeyPressed += (s, e) => {
rKeys += (char)e.KeyEvent.Key;
if (!IsShift && e.KeyEvent.IsShift) {
rControlKeys += " Shift ";
IsShift = true;
}
if (!IsAlt && e.KeyEvent.IsAlt) {
rControlKeys += " Alt ";
IsAlt = true;
}
if (!IsCtrl && e.KeyEvent.IsCtrl) {
rControlKeys += " Ctrl ";
IsCtrl = true;
}
};
var lblShippedKeys = new Label () {
X = Pos.Center (),
Y = Pos.Center () + 3,
AutoSize = true
};
Win.Add (lblShippedKeys);
var lblShippedControlKeys = new Label () {
X = Pos.Center (),
Y = Pos.Center () + 5,
AutoSize = true
};
Win.Add (lblShippedControlKeys);
var button = new Button ("Process keys") {
X = Pos.Center (),
Y = Pos.Center () + 7,
IsDefault = true
};
Win.Add (button);
void ProcessInput ()
{
rKeys = "";
rControlKeys = "";
txtResult.Text = "";
IsShift = false;
IsAlt = false;
IsCtrl = false;
txtResult.SetFocus ();
foreach (var r in txtInput.Text) {
var ck = char.IsLetter (r)
? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
Application.Driver.SendKeys (r, ck, (bool)ckbShift.Checked,
(bool)ckbAlt.Checked, (bool)ckbControl.Checked);
}
lblShippedKeys.Text = rKeys;
lblShippedControlKeys.Text = rControlKeys;
txtInput.SetFocus ();
}
button.Clicked += (s,e) => ProcessInput ();
Win.KeyPressed += (s, e) => {
if (e.KeyEvent.Key == Key.Enter) {
ProcessInput ();
e.Handled = true;
}
};
}
}
}