Git Product home page Git Product logo

Comments (3)

tznind avatar tznind commented on June 21, 2024 1

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
image
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.

tznind avatar tznind commented on June 21, 2024 1

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.

luizfernandonb avatar luizfernandonb commented on June 21, 2024

Thanks!

from terminal.gui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.