Comments (3)
Assuming we are talking about v1 here.
There are 3 main ways to respond to key strokes.
Global event handler:
This is used when you want to handle key logic regardless of which control has focus. This is always first to be called.
Application.RootKeyEvent += (e) =>
{
if (e.Key == (Key.CtrlMask | Key.U))
{
Debug.WriteLine("Hey");
}
// Let the other handlers also run (or return true to consume event)
return false;
};
View Key Events
This is used when you have arbitrary logic that you want to execute when a combination or key is pressed while a given View
is focused.
var b = new Button { Text="ClickMe"};
b.KeyUp += (e) =>
{
if (e.KeyEvent.Key == (Key.CtrlMask | Key.U))
{
Debug.WriteLine("Hi");
// Let the other handlers also run (or true to consume event)
e.Handled = false;
}
};
Keybindings
When creating a new type of View (e.g. by subclassing), you can implement any of the generic 'commands'.
The Command system establishes a common set of shared operations that many views may implement. For example Expand in a combo box is very different from Expand in a TreeView.
For example:
class MyButton : Button
{
public MyButton()
{
// My control can be Expanded (Command.Expand)
AddCommand(Command.Expand, () => {
// This is the code to execute when it is Expanded
Debug.WriteLine("Hello");
return true;
});
// By default the combination to expand this control is Ctrl+U
AddKeyBinding(Key.CtrlMask | Key.U, Command.Expand);
}
}
The reason for having an enum representing 'generic' commands is so that end users of Terminal.Gui applications can remap keybindings application wide without having to have access to the source code (or make program changes).
For example see UICatalog
Runtime keybinding configuration example in UICatalog
Example combining all 3
Here is a code sample that combines all 3 methods.
using System.Diagnostics;
using Terminal.Gui;
public class Program
{
static void Main(string[] args)
{
Application.Init();
var w = new Window();
var b = new MyButton { Text="ClickMe"};
w.Add(b);
b.KeyUp += (e) =>
{
if (e.KeyEvent.Key == (Key.CtrlMask | Key.U))
{
Debug.WriteLine("Hi");
// Let the other handlers also run
e.Handled = false;
}
};
Application.RootKeyEvent += (e) =>
{
if (e.Key == (Key.CtrlMask | Key.U))
{
Debug.WriteLine("Hey");
}
// Let the other handlers also run
return false;
};
// Run the application loop.
Application.Run(w);
Application.Shutdown();
}
class MyButton : Button
{
public MyButton()
{
AddCommand(Command.Expand, () => {
Debug.WriteLine("Hello");
return true;
});
AddKeyBinding(Key.CtrlMask | Key.U, Command.Expand);
}
}
}
from terminal.gui.
You could add new Commands but it is a bit hacky:
Either
public enum ExtendedCommand
{
ReservedByTerminalGui = 999,
ShootMainCannon,
ActivateThrusters,
}
class MyButton : Button
{
public MyButton()
{
AddCommand((Command)ExtendedCommand.ShootMainCannon, () => {
Debug.WriteLine("Hello");
return true;
});
AddKeyBinding(Key.CtrlMask | Key.U, (Command)ExtendedCommand.ShootMainCannon);
}
}
Or to be more verbose and brittle (but more correct) you could also do:
public enum ExtendedCommand
{
LineDown = Command.LineDown,
LineDownExtend = Command.LineDownExtend,
LineDownToLastBranch = Command.LineDownToLastBranch,
ScrollDown = Command.ScrollDown,
LineUp = Command.LineUp,
LineUpExtend = Command.LineUpExtend,
LineUpToFirstBranch = Command.LineUpToFirstBranch,
ScrollUp = Command.ScrollUp,
Left = Command.Left,
ScrollLeft = Command.ScrollLeft,
LeftExtend = Command.LeftExtend,
Right = Command.Right,
ScrollRight = Command.ScrollRight,
RightExtend = Command.RightExtend,
WordLeft = Command.WordLeft,
WordLeftExtend = Command.WordLeftExtend,
WordRight = Command.WordRight,
WordRightExtend = Command.WordRightExtend,
CutToEndLine = Command.CutToEndLine,
CutToStartLine = Command.CutToStartLine,
KillWordForwards = Command.KillWordForwards,
KillWordBackwards = Command.KillWordBackwards,
ToggleOverwrite = Command.ToggleOverwrite,
EnableOverwrite = Command.EnableOverwrite,
DisableOverwrite = Command.DisableOverwrite,
PageDown = Command.PageDown,
PageDownExtend = Command.PageDownExtend,
PageUp = Command.PageUp,
PageUpExtend = Command.PageUpExtend,
TopHome = Command.TopHome,
TopHomeExtend = Command.TopHomeExtend,
BottomEnd = Command.BottomEnd,
BottomEndExtend = Command.BottomEndExtend,
OpenSelectedItem = Command.OpenSelectedItem,
ToggleChecked = Command.ToggleChecked,
Accept = Command.Accept,
ToggleExpandCollapse = Command.ToggleExpandCollapse,
Expand = Command.Expand,
ExpandAll = Command.ExpandAll,
Collapse = Command.Collapse,
CollapseAll = Command.CollapseAll,
Cancel = Command.Cancel,
UnixEmulation = Command.UnixEmulation,
DeleteCharRight = Command.DeleteCharRight,
DeleteCharLeft = Command.DeleteCharLeft,
SelectAll = Command.SelectAll,
DeleteAll = Command.DeleteAll,
StartOfLine = Command.StartOfLine,
StartOfLineExtend = Command.StartOfLineExtend,
EndOfLine = Command.EndOfLine,
EndOfLineExtend = Command.EndOfLineExtend,
StartOfPage = Command.StartOfPage,
EndOfPage = Command.EndOfPage,
PageLeft = Command.PageLeft,
PageRight = Command.PageRight,
LeftHome = Command.LeftHome,
LeftHomeExtend = Command.LeftHomeExtend,
RightEnd = Command.RightEnd,
RightEndExtend = Command.RightEndExtend,
Undo = Command.Undo,
Redo = Command.Redo,
Copy = Command.Copy,
Cut = Command.Cut,
Paste = Command.Paste,
QuitToplevel = Command.QuitToplevel,
Suspend = Command.Suspend,
NextView = Command.NextView,
PreviousView = Command.PreviousView,
NextViewOrTop = Command.NextViewOrTop,
PreviousViewOrTop = Command.PreviousViewOrTop,
Refresh = Command.Refresh,
ToggleExtend = Command.ToggleExtend,
NewLine = Command.NewLine,
Tab = Command.Tab,
BackTab = Command.BackTab,
ShootMainCannon,
ActivateThrusters,
}
from terminal.gui.
Thanks!
from terminal.gui.
Related Issues (20)
- v1 NetDriver throws System.InvalidOperationException when a key is pressed.
- NetDriver is consuming too much CPU resources in ReadConsoleKeyInfo. HOT 12
- DimAuto equality HOT 3
- Slider<> strange behaviour of Text property HOT 3
- v2: Button alignment in MessageBox is no longer centered HOT 3
- Allow to set Contents to dirty to just refresh the screen. HOT 2
- Terminal.Gui 1.17.0 Throws System Exception in Application.Run(dialog) HOT 17
- v1 CursesDriver isn't updating the buffer on the UpdateOffScreen method.
- How to "hide" views from layout? HOT 12
- Application.Run<T> is broken by not calling Init before initializing T.
- Add an example for CommunityToolkit.MVVM
- V2: TextField numeric input not working on non-Windows platforms HOT 5
- Checking if clipboard is available on windows HOT 6
- Disable borders on a MenuItem HOT 4
- v2 crashes on run with missing dependency JetBrains.Annotations HOT 9
- When adding more than one Window + TextView together with a MenuBar, the displayed text changes to the text of the first added Window HOT 4
- Mouse is broken in v2 HOT 2
- v2: Big performance impact on ListView/ComboBox's that contain a lot of items HOT 3
- Multiple views with the same `HotKey` should be supported
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from terminal.gui.