Files
Terminal.Gui/UICatalog/Scenarios/SendKeys.cs
Tig 16055c53b0 Fixes #3039. Fix View.HotKey (#3249)
* Added View.DefaultCommand etc... Started on dedicated scenario

* Fixed un-shifted hotkeys -> Fixed Key Equals. Fixed WindowsDriver passing wrong key. Etc.

* Fixed Key Bindings and HotKeys

* Fixed Key Bindings and HotKeys

* Label now correctly supports hotkey

* Disabled unix hot keys because they are annoying and get in the way

* Updated nuget. fixed warnings

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Changed TextChangingEventArgs to inherit from CancelEventArgs

* TextChangingEventArgs -> TextEventArgs

* Simplified Text events by having only on args class

* Fixed unit tests fail

* Simplified by removing TitleEventArgs

* POC of Title being primary for hotkey. Label and Button hacked to work

* POC of Title being primary for hotkey. Label and Button hacked to work - all unit tests pass

* Dropped Microsoft.NETFramework.ReferenceAssemblies

* Fixed Dialogs scenario hotkeys

* Fixed build warnings

* Fixed Border Title render bug

* Regiggering default command handling

* Regiggering default command handling

* Checkbox clean up

* Added StateEventArgs POC

* Command.Default -> Command.HotKey

* Command.Default -> Command.HotKey - fixed TableView

* Command.Default -> Command.HotKey - fixed TableView

* Updated reactive example

* Fixed Toplevel.BringOverlappedTopToFront - was reordering SubViews when it shouldn't

* WIP - broke

* Finished impl of StateEventArgs

* Deleted ToggleEventArgs.cs. Added StateEventArgs.cs

* XML doc fix

* Removed old code

* Removed commented out code

* Label.Clicked -> Label.Accept (missed this before)

* Removed Labels as Buttons scenario as it's not really  useful

* Moved SubView tests to own file

* Moved SubView tests to own file

* Simplified Text test

* Added OnAccept test

* Deleted DefaultCommand

* Modernized CheckBox

* New button test

* Cleaned up RadioGroup; added tests

* KeyCode->Key in ListView

* Added ListView unit tests

* ListView now does Accept correctly

* TreeView now does Accept correctly

* Cleaned up some TextField tests

* TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Fixed ComboBox to deal with TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Removed un-needed using statement
2024-02-22 15:11:26 -07:00

113 lines
3.9 KiB
C#

using System;
using Terminal.Gui;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("SendKeys", "SendKeys sample - Send key combinations.")]
[ScenarioCategory ("Mouse and Keyboard")]
public class SendKeys : Scenario
{
public override void Setup ()
{
var label = new Label { X = Pos.Center (), Y = Pos.Center () - 6, Text = "Insert the text to send:" };
Win.Add (label);
var txtInput = new TextField { X = Pos.Center (), Y = Pos.Center () - 5, Width = 20, Text = "MockKeyPresses" };
Win.Add (txtInput);
var ckbShift = new CheckBox { X = Pos.Center (), Y = Pos.Center () - 4, Text = "Shift" };
Win.Add (ckbShift);
var ckbAlt = new CheckBox { X = Pos.Center (), Y = Pos.Center () - 3, Text = "Alt" };
Win.Add (ckbAlt);
var ckbControl = new CheckBox { X = Pos.Center (), Y = Pos.Center () - 2, Text = "Control" };
Win.Add (ckbControl);
label = new Label { X = Pos.Center (), Y = Pos.Center () + 1, Text = "Result keys:" };
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.KeyDown += (s, e) =>
{
rKeys += (char)e.KeyCode;
if (!IsShift && e.IsShift)
{
rControlKeys += " Shift ";
IsShift = true;
}
if (!IsAlt && e.IsAlt)
{
rControlKeys += " Alt ";
IsAlt = true;
}
if (!IsCtrl && e.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 { X = Pos.Center (), Y = Pos.Center () + 7, IsDefault = true, Text = "Process keys" };
Win.Add (button);
void ProcessInput ()
{
rKeys = "";
rControlKeys = "";
txtResult.Text = "";
IsShift = false;
IsAlt = false;
IsCtrl = false;
txtResult.SetFocus ();
foreach (char r in txtInput.Text)
{
ConsoleKey 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.Accept += (s, e) => ProcessInput ();
Win.KeyDown += (s, e) =>
{
if (e.KeyCode == KeyCode.Enter)
{
ProcessInput ();
e.Handled = true;
}
};
}
}