key down/up support

This commit is contained in:
Charlie Kindel
2020-04-14 20:33:02 -06:00
parent d772cf4962
commit e37ae64356
5 changed files with 99 additions and 10 deletions

View File

@@ -119,8 +119,29 @@ namespace Terminal.Gui {
public virtual bool ProcessColdKey (KeyEvent keyEvent)
{
return false;
}
}
/// <summary>
/// Method invoked when a key is pressed.
/// </summary>
/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
/// <returns>true if the event was handled</returns>
public virtual bool KeyDown (KeyEvent keyEvent)
{
return false;
}
/// <summary>
/// Method invoked when a key is released.
/// </summary>
/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
/// <returns>true if the event was handled</returns>
public virtual bool KeyUp (KeyEvent keyEvent)
{
return false;
}
/// <summary>
/// Method invoked when a mouse event is generated
/// </summary>
@@ -1009,8 +1030,31 @@ namespace Terminal.Gui {
if (view.ProcessColdKey (keyEvent))
return true;
return false;
}
}
/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
public override bool KeyDown (KeyEvent keyEvent)
{
if (subviews == null || subviews.Count == 0)
return false;
foreach (var view in subviews)
if (view.KeyDown (keyEvent))
return true;
return false;
}
/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
public override bool KeyUp (KeyEvent keyEvent)
{
if (subviews == null || subviews.Count == 0)
return false;
foreach (var view in subviews)
if (view.KeyUp (keyEvent))
return true;
return false;
}
/// <summary>
/// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
/// </summary>
@@ -1943,6 +1987,7 @@ namespace Terminal.Gui {
static void ProcessKeyEvent (KeyEvent ke)
{
var chain = toplevels.ToList();
foreach (var topLevel in chain) {
if (topLevel.ProcessHotKey (ke))
@@ -1965,6 +2010,29 @@ namespace Terminal.Gui {
if (topLevel.Modal)
break;
}
}
static void ProcessKeyDownEvent (KeyEvent ke)
{
var chain = toplevels.ToList ();
foreach (var topLevel in chain) {
if (topLevel.KeyDown (ke))
return;
if (topLevel.Modal)
break;
}
}
static void ProcessKeyUpEvent (KeyEvent ke)
{
var chain = toplevels.ToList ();
foreach (var topLevel in chain) {
if (topLevel.KeyUp (ke))
return;
if (topLevel.Modal)
break;
}
}
static View FindDeepestView (View start, int x, int y, out int resx, out int resy)
@@ -2092,7 +2160,7 @@ namespace Terminal.Gui {
}
toplevels.Push (toplevel);
Current = toplevel;
Driver.PrepareToRun (MainLoop, ProcessKeyEvent, ProcessMouseEvent);
Driver.PrepareToRun (MainLoop, ProcessKeyEvent, ProcessKeyDownEvent, ProcessKeyUpEvent, ProcessMouseEvent);
if (toplevel.LayoutStyle == LayoutStyle.Computed)
toplevel.RelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
toplevel.LayoutSubviews ();

View File

@@ -469,7 +469,7 @@ namespace Terminal.Gui {
/// <param name="mainLoop"></param>
/// <param name="keyHandler"></param>
/// <param name="mouseHandler"></param>
public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler);
public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
/// <summary>
/// Updates the screen to reflect all the changes that have been done to the display buffer

View File

@@ -203,8 +203,9 @@ namespace Terminal.Gui {
keyHandler (new KeyEvent ((Key)wch));
}
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
{
// Note: Curses doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
Curses.timeout (-1);
(mainLoop.Driver as Mono.Terminal.UnixMainLoop).AddWatch (0, Mono.Terminal.UnixMainLoop.Condition.PollIn, x => {

View File

@@ -320,8 +320,9 @@ namespace Terminal.Gui {
return (Key)(0xffffffff);
}
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
{
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
{
// Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
(mainLoop.Driver as NetMainLoop).WindowsKeyPressed = delegate (ConsoleKeyInfo consoleKey) {
var map = MapKey (consoleKey);
if (map == (Key)0xffffffff)

View File

@@ -26,6 +26,7 @@
// SOFTWARE.
//
using System;
using System.CodeDom;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
@@ -537,11 +538,15 @@ namespace Terminal.Gui {
}
Action<KeyEvent> keyHandler;
Action<KeyEvent> keyDownHandler;
Action<KeyEvent> keyUpHandler;
Action<MouseEvent> mouseHandler;
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
{
this.keyHandler = keyHandler;
this.keyDownHandler = keyDownHandler;
this.keyUpHandler = keyUpHandler;
this.mouseHandler = mouseHandler;
}
@@ -554,10 +559,14 @@ namespace Terminal.Gui {
var inputEvent = result [0];
switch (inputEvent.EventType) {
case WindowsConsole.EventType.Key:
// See how this relies on key up?
if (inputEvent.KeyEvent.bKeyDown == false)
return;
var map = MapKey (ToConsoleKeyInfoEx (inputEvent.KeyEvent));
if (inputEvent.KeyEvent.UnicodeChar == 0 && map == (Key)0xffffffff)
if (inputEvent.KeyEvent.UnicodeChar == 0)
keyDownHandler (new KeyEvent (map));
if (map == (Key)0xffffffff)
return;
keyHandler (new KeyEvent (map));
break;
@@ -837,6 +846,16 @@ namespace Terminal.Gui {
return (Key)((int)Key.F1 + delta);
}
//if (keyInfo.KeyChar == 0) {
// if (keyInfo.Modifiers == ConsoleModifiers.Control)
// return (Key)(uint)Key.CharMask;
// if (keyInfo.Modifiers == ConsoleModifiers.Alt)
// return (Key)(uint)Key.AltMask;
// if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
// return (Key)(uint)(Key.AltMask | Key.CharMask);
// }
//}
return (Key)(0xffffffff);
}