// // Evemts.cs: Events, Key mappings // // Authors: // Miguel de Icaza (miguel@gnome.org) // using System; namespace Terminal.Gui { /// /// Identifies the state of the "shift"-keys within a event. /// public class KeyModifiers { /// /// Check if the Shift key was pressed or not. /// public bool Shift; /// /// Check if the Alt key was pressed or not. /// public bool Alt; /// /// Check if the Ctrl key was pressed or not. /// public bool Ctrl; /// /// Check if the Caps lock key was pressed or not. /// public bool Capslock; /// /// Check if the Num lock key was pressed or not. /// public bool Numlock; /// /// Check if the Scroll lock key was pressed or not. /// public bool Scrolllock; } /// /// The enumeration contains special encoding for some keys, but can also /// encode all the unicode values that can be passed. /// /// /// /// If the is set, then the value is that of the special mask, /// otherwise, the value is the one of the lower bits (as extracted by ) /// /// Numerics keys are the values between 48 and 57 corresponding to 0 to 9 /// /// /// /// Upper alpha keys are the values between 65 and 90 corresponding to A to Z /// /// /// Unicode runes are also stored here, the letter 'A" for example is encoded as a value 65 (not surfaced in the enum). /// /// [Flags] public enum Key : uint { /// /// Mask that indicates that this is a character value, values outside this range /// indicate special characters like Alt-key combinations or special keys on the /// keyboard like function keys, arrows keys and so on. /// CharMask = 0xfffff, /// /// If the is set, then the value is that of the special mask, /// otherwise, the value is the one of the lower bits (as extracted by ). /// SpecialMask = 0xfff00000, /// /// The key code representing null or empty /// Null = '\0', /// /// Backspace key. /// Backspace = 8, /// /// The key code for the user pressing the tab key (forwards tab key). /// Tab = 9, /// /// The key code for the user pressing the return key. /// Enter = '\n', /// /// The key code for the user pressing the clear key. /// Clear = 12, /// /// The key code for the user pressing the escape key /// Esc = 27, /// /// The key code for the user pressing the space bar /// Space = 32, /// /// Digit 0. /// D0 = 48, /// /// Digit 1. /// D1, /// /// Digit 2. /// D2, /// /// Digit 3. /// D3, /// /// Digit 4. /// D4, /// /// Digit 5. /// D5, /// /// Digit 6. /// D6, /// /// Digit 7. /// D7, /// /// Digit 8. /// D8, /// /// Digit 9. /// D9, /// /// The key code for the user pressing Shift-A /// A = 65, /// /// The key code for the user pressing Shift-B /// B, /// /// The key code for the user pressing Shift-C /// C, /// /// The key code for the user pressing Shift-D /// D, /// /// The key code for the user pressing Shift-E /// E, /// /// The key code for the user pressing Shift-F /// F, /// /// The key code for the user pressing Shift-G /// G, /// /// The key code for the user pressing Shift-H /// H, /// /// The key code for the user pressing Shift-I /// I, /// /// The key code for the user pressing Shift-J /// J, /// /// The key code for the user pressing Shift-K /// K, /// /// The key code for the user pressing Shift-L /// L, /// /// The key code for the user pressing Shift-M /// M, /// /// The key code for the user pressing Shift-N /// N, /// /// The key code for the user pressing Shift-O /// O, /// /// The key code for the user pressing Shift-P /// P, /// /// The key code for the user pressing Shift-Q /// Q, /// /// The key code for the user pressing Shift-R /// R, /// /// The key code for the user pressing Shift-S /// S, /// /// The key code for the user pressing Shift-T /// T, /// /// The key code for the user pressing Shift-U /// U, /// /// The key code for the user pressing Shift-V /// V, /// /// The key code for the user pressing Shift-W /// W, /// /// The key code for the user pressing Shift-X /// X, /// /// The key code for the user pressing Shift-Y /// Y, /// /// The key code for the user pressing Shift-Z /// Z, /// /// The key code for the user pressing A /// a = 97, /// /// The key code for the user pressing B /// b, /// /// The key code for the user pressing C /// c, /// /// The key code for the user pressing D /// d, /// /// The key code for the user pressing E /// e, /// /// The key code for the user pressing F /// f, /// /// The key code for the user pressing G /// g, /// /// The key code for the user pressing H /// h, /// /// The key code for the user pressing I /// i, /// /// The key code for the user pressing J /// j, /// /// The key code for the user pressing K /// k, /// /// The key code for the user pressing L /// l, /// /// The key code for the user pressing M /// m, /// /// The key code for the user pressing N /// n, /// /// The key code for the user pressing O /// o, /// /// The key code for the user pressing P /// p, /// /// The key code for the user pressing Q /// q, /// /// The key code for the user pressing R /// r, /// /// The key code for the user pressing S /// s, /// /// The key code for the user pressing T /// t, /// /// The key code for the user pressing U /// u, /// /// The key code for the user pressing V /// v, /// /// The key code for the user pressing W /// w, /// /// The key code for the user pressing X /// x, /// /// The key code for the user pressing Y /// y, /// /// The key code for the user pressing Z /// z, /// /// The key code for the user pressing the delete key. /// Delete = 127, /// /// When this value is set, the Key encodes the sequence Shift-KeyValue. /// ShiftMask = 0x10000000, /// /// When this value is set, the Key encodes the sequence Alt-KeyValue. /// And the actual value must be extracted by removing the AltMask. /// AltMask = 0x80000000, /// /// When this value is set, the Key encodes the sequence Ctrl-KeyValue. /// And the actual value must be extracted by removing the CtrlMask. /// CtrlMask = 0x40000000, /// /// Cursor up key /// CursorUp = 0x100000, /// /// Cursor down key. /// CursorDown, /// /// Cursor left key. /// CursorLeft, /// /// Cursor right key. /// CursorRight, /// /// Page Up key. /// PageUp, /// /// Page Down key. /// PageDown, /// /// Home key. /// Home, /// /// End key. /// End, /// /// Insert character key. /// InsertChar, /// /// Delete character key. /// DeleteChar, /// /// Shift-tab key (backwards tab key). /// BackTab, /// /// Print screen character key. /// PrintScreen, /// /// F1 key. /// F1, /// /// F2 key. /// F2, /// /// F3 key. /// F3, /// /// F4 key. /// F4, /// /// F5 key. /// F5, /// /// F6 key. /// F6, /// /// F7 key. /// F7, /// /// F8 key. /// F8, /// /// F9 key. /// F9, /// /// F10 key. /// F10, /// /// F11 key. /// F11, /// /// F12 key. /// F12, /// /// F13 key. /// F13, /// /// F14 key. /// F14, /// /// F15 key. /// F15, /// /// F16 key. /// F16, /// /// F17 key. /// F17, /// /// F18 key. /// F18, /// /// F19 key. /// F19, /// /// F20 key. /// F20, /// /// F21 key. /// F21, /// /// F22 key. /// F22, /// /// F23 key. /// F23, /// /// F24 key. /// F24, /// /// A key with an unknown mapping was raised. /// Unknown } /// /// Describes a keyboard event. /// public class KeyEvent { KeyModifiers keyModifiers; /// /// Symbolic definition for the key. /// public Key Key; /// /// The key value cast to an integer, you will typical use this for /// extracting the Unicode rune value out of a key, when none of the /// symbolic options are in use. /// public int KeyValue => (int)Key; /// /// Gets a value indicating whether the Shift key was pressed. /// /// true if is shift; otherwise, false. public bool IsShift => keyModifiers.Shift || Key == Key.BackTab; /// /// Gets a value indicating whether the Alt key was pressed (real or synthesized) /// /// true if is alternate; otherwise, false. public bool IsAlt => keyModifiers.Alt; /// /// Determines whether the value is a control key (and NOT just the ctrl key) /// /// true if is ctrl; otherwise, false. //public bool IsCtrl => ((uint)Key >= 1) && ((uint)Key <= 26); public bool IsCtrl => keyModifiers.Ctrl; /// /// Gets a value indicating whether the Caps lock key was pressed (real or synthesized) /// /// true if is alternate; otherwise, false. public bool IsCapslock => keyModifiers.Capslock; /// /// Gets a value indicating whether the Num lock key was pressed (real or synthesized) /// /// true if is alternate; otherwise, false. public bool IsNumlock => keyModifiers.Numlock; /// /// Gets a value indicating whether the Scroll lock key was pressed (real or synthesized) /// /// true if is alternate; otherwise, false. public bool IsScrolllock => keyModifiers.Scrolllock; /// /// Constructs a new /// public KeyEvent () { Key = Key.Unknown; keyModifiers = new KeyModifiers (); } /// /// Constructs a new from the provided Key value - can be a rune cast into a Key value /// public KeyEvent (Key k, KeyModifiers km) { Key = k; keyModifiers = km; } /// /// Pretty prints the KeyEvent /// /// public override string ToString () { string msg = ""; var key = this.Key; if (keyModifiers.Shift) { msg += "Shift-"; } if (keyModifiers.Alt) { msg += "Alt-"; } if (keyModifiers.Ctrl) { msg += "Ctrl-"; } if (keyModifiers.Capslock) { msg += "Capslock-"; } if (keyModifiers.Numlock) { msg += "Numlock-"; } if (keyModifiers.Scrolllock) { msg += "Scrolllock-"; } msg += $"{((Key)KeyValue != Key.Unknown && ((uint)this.KeyValue & (uint)Key.CharMask) > 27 ? $"{(char)this.KeyValue}" : $"{key}")}"; return msg; } } /// /// Mouse flags reported in . /// /// /// They just happen to map to the ncurses ones. /// [Flags] public enum MouseFlags { /// /// The first mouse button was pressed. /// Button1Pressed = unchecked((int)0x2), /// /// The first mouse button was released. /// Button1Released = unchecked((int)0x1), /// /// The first mouse button was clicked (press+release). /// Button1Clicked = unchecked((int)0x4), /// /// The first mouse button was double-clicked. /// Button1DoubleClicked = unchecked((int)0x8), /// /// The first mouse button was triple-clicked. /// Button1TripleClicked = unchecked((int)0x10), /// /// The second mouse button was pressed. /// Button2Pressed = unchecked((int)0x80), /// /// The second mouse button was released. /// Button2Released = unchecked((int)0x40), /// /// The second mouse button was clicked (press+release). /// Button2Clicked = unchecked((int)0x100), /// /// The second mouse button was double-clicked. /// Button2DoubleClicked = unchecked((int)0x200), /// /// The second mouse button was triple-clicked. /// Button2TripleClicked = unchecked((int)0x400), /// /// The third mouse button was pressed. /// Button3Pressed = unchecked((int)0x2000), /// /// The third mouse button was released. /// Button3Released = unchecked((int)0x1000), /// /// The third mouse button was clicked (press+release). /// Button3Clicked = unchecked((int)0x4000), /// /// The third mouse button was double-clicked. /// Button3DoubleClicked = unchecked((int)0x8000), /// /// The third mouse button was triple-clicked. /// Button3TripleClicked = unchecked((int)0x10000), /// /// The fourth mouse button was pressed. /// Button4Pressed = unchecked((int)0x80000), /// /// The fourth mouse button was released. /// Button4Released = unchecked((int)0x40000), /// /// The fourth button was clicked (press+release). /// Button4Clicked = unchecked((int)0x100000), /// /// The fourth button was double-clicked. /// Button4DoubleClicked = unchecked((int)0x200000), /// /// The fourth button was triple-clicked. /// Button4TripleClicked = unchecked((int)0x400000), /// /// Flag: the shift key was pressed when the mouse button took place. /// ButtonShift = unchecked((int)0x2000000), /// /// Flag: the ctrl key was pressed when the mouse button took place. /// ButtonCtrl = unchecked((int)0x1000000), /// /// Flag: the alt key was pressed when the mouse button took place. /// ButtonAlt = unchecked((int)0x4000000), /// /// The mouse position is being reported in this event. /// ReportMousePosition = unchecked((int)0x8000000), /// /// Vertical button wheeled up. /// WheeledUp = unchecked((int)0x10000000), /// /// Vertical button wheeled down. /// WheeledDown = unchecked((int)0x20000000), /// /// Vertical button wheeled up while pressing ButtonShift. /// WheeledLeft = ButtonShift | WheeledUp, /// /// Vertical button wheeled down while pressing ButtonShift. /// WheeledRight = ButtonShift | WheeledDown, /// /// Mask that captures all the events. /// AllEvents = unchecked((int)0x7ffffff), } /// /// Low-level construct that conveys the details of mouse events, such /// as coordinates and button state, from ConsoleDrivers up to and /// Views. /// /// The class includes the /// Action which takes a MouseEvent argument. public class MouseEvent { /// /// The X (column) location for the mouse event. /// public int X { get; set; } /// /// The Y (column) location for the mouse event. /// public int Y { get; set; } /// /// Flags indicating the kind of mouse event that is being posted. /// public MouseFlags Flags { get; set; } /// /// The offset X (column) location for the mouse event. /// public int OfX { get; set; } /// /// The offset Y (column) location for the mouse event. /// public int OfY { get; set; } /// /// The current view at the location for the mouse event. /// public View View { get; set; } /// /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method. /// public bool Handled { get; set; } /// /// Returns a that represents the current . /// /// A that represents the current . public override string ToString () { return $"({X},{Y}:{Flags}"; } } }