mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Adding a more realistic key down/up event with null key.
This commit is contained in:
@@ -388,19 +388,23 @@ namespace Terminal.Gui {
|
||||
void ProcessInput (ConsoleKeyInfo consoleKey)
|
||||
{
|
||||
keyModifiers = new KeyModifiers ();
|
||||
var map = MapKey (consoleKey);
|
||||
if (map == (Key)0xffffffff)
|
||||
return;
|
||||
|
||||
if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) {
|
||||
keyModifiers.Alt = true;
|
||||
}
|
||||
if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Shift)) {
|
||||
keyModifiers.Shift = true;
|
||||
}
|
||||
if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) {
|
||||
keyModifiers.Alt = true;
|
||||
}
|
||||
if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Control)) {
|
||||
keyModifiers.Ctrl = true;
|
||||
}
|
||||
var map = MapKey (consoleKey);
|
||||
if (map == (Key)0xffffffff) {
|
||||
if ((consoleKey.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
|
||||
keyDownHandler (new KeyEvent (map, keyModifiers));
|
||||
keyUpHandler (new KeyEvent (map, keyModifiers));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
keyDownHandler (new KeyEvent (map, keyModifiers));
|
||||
keyHandler (new KeyEvent (map, keyModifiers));
|
||||
|
||||
@@ -3914,40 +3914,50 @@ This is a tes
|
||||
[Fact, AutoInitShutdown]
|
||||
public void KeyDown_And_KeyUp_Events_Must_Called_Before_OnKeyDown_And_OnKeyUp ()
|
||||
{
|
||||
var keyDown = false;
|
||||
var keyPress = false;
|
||||
var keyUp = false;
|
||||
|
||||
var view = new DerivedView ();
|
||||
view.KeyDown += (e) => {
|
||||
Assert.Equal (Key.a, e.KeyEvent.Key);
|
||||
Assert.False (keyDown);
|
||||
Assert.False (view.IsKeyDown);
|
||||
e.Handled = true;
|
||||
keyDown = true;
|
||||
};
|
||||
view.KeyPress += (e) => {
|
||||
Assert.Equal (Key.a, e.KeyEvent.Key);
|
||||
Assert.False (keyPress);
|
||||
Assert.False (view.IsKeyPress);
|
||||
e.Handled = true;
|
||||
keyPress = true;
|
||||
};
|
||||
view.KeyUp += (e) => {
|
||||
Assert.Equal (Key.a, e.KeyEvent.Key);
|
||||
Assert.False (keyUp);
|
||||
Assert.False (view.IsKeyUp);
|
||||
e.Handled = true;
|
||||
};
|
||||
|
||||
var iterations = -1;
|
||||
|
||||
Application.Iteration += () => {
|
||||
iterations++;
|
||||
if (iterations == 0) {
|
||||
Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
|
||||
} else if (iterations == 1) {
|
||||
Application.RequestStop ();
|
||||
}
|
||||
keyUp = true;
|
||||
};
|
||||
|
||||
Application.Top.Add (view);
|
||||
|
||||
Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
|
||||
|
||||
Application.Iteration += () => Application.RequestStop ();
|
||||
|
||||
Assert.True (view.CanFocus);
|
||||
|
||||
Application.Run ();
|
||||
Application.Shutdown ();
|
||||
|
||||
Assert.True (keyDown);
|
||||
Assert.True (keyPress);
|
||||
Assert.True (keyUp);
|
||||
Assert.False (view.IsKeyDown);
|
||||
Assert.False (view.IsKeyPress);
|
||||
Assert.False (view.IsKeyUp);
|
||||
}
|
||||
|
||||
public class DerivedView : View {
|
||||
@@ -3963,20 +3973,72 @@ This is a tes
|
||||
public override bool OnKeyDown (KeyEvent keyEvent)
|
||||
{
|
||||
IsKeyDown = true;
|
||||
return base.OnKeyDown (keyEvent);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool ProcessKey (KeyEvent keyEvent)
|
||||
{
|
||||
IsKeyPress = true;
|
||||
return base.ProcessKey (keyEvent);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnKeyUp (KeyEvent keyEvent)
|
||||
{
|
||||
IsKeyUp = true;
|
||||
return base.OnKeyUp (keyEvent);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[Theory, AutoInitShutdown]
|
||||
[InlineData (true, false, false)]
|
||||
[InlineData (true, true, false)]
|
||||
[InlineData (true, true, true)]
|
||||
public void KeyDown_And_KeyUp_Events_With_Only_Key_Modifiers (bool shift, bool alt, bool control)
|
||||
{
|
||||
var keyDown = false;
|
||||
var keyPress = false;
|
||||
var keyUp = false;
|
||||
|
||||
var view = new DerivedView ();
|
||||
view.KeyDown += (e) => {
|
||||
Assert.Equal (-1, e.KeyEvent.KeyValue);
|
||||
Assert.Equal (shift, e.KeyEvent.IsShift);
|
||||
Assert.Equal (alt, e.KeyEvent.IsAlt);
|
||||
Assert.Equal (control, e.KeyEvent.IsCtrl);
|
||||
Assert.False (keyDown);
|
||||
Assert.False (view.IsKeyDown);
|
||||
keyDown = true;
|
||||
};
|
||||
view.KeyPress += (e) => {
|
||||
keyPress = true;
|
||||
};
|
||||
view.KeyUp += (e) => {
|
||||
Assert.Equal (-1, e.KeyEvent.KeyValue);
|
||||
Assert.Equal (shift, e.KeyEvent.IsShift);
|
||||
Assert.Equal (alt, e.KeyEvent.IsAlt);
|
||||
Assert.Equal (control, e.KeyEvent.IsCtrl);
|
||||
Assert.False (keyUp);
|
||||
Assert.False (view.IsKeyUp);
|
||||
keyUp = true;
|
||||
};
|
||||
|
||||
Application.Top.Add (view);
|
||||
|
||||
Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\0', (ConsoleKey)'\0', shift, alt, control));
|
||||
|
||||
Application.Iteration += () => Application.RequestStop ();
|
||||
|
||||
Assert.True (view.CanFocus);
|
||||
|
||||
Application.Run ();
|
||||
Application.Shutdown ();
|
||||
|
||||
Assert.True (keyDown);
|
||||
Assert.False (keyPress);
|
||||
Assert.True (keyUp);
|
||||
Assert.True (view.IsKeyDown);
|
||||
Assert.False (view.IsKeyPress);
|
||||
Assert.True (view.IsKeyUp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user