Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the KeyCode.Unknown #3055

Closed
tig opened this issue Dec 16, 2023 · 5 comments
Closed

Remove the KeyCode.Unknown #3055

tig opened this issue Dec 16, 2023 · 5 comments
Labels
design Issues regarding Terminal.Gui design (bugs, guidelines, debates, etc...)

Comments

@tig
Copy link
Collaborator

tig commented Dec 16, 2023

I propose to remove the KeyCode.Unknown which aren't used by the drivers now, since they return KeyCode.Null if KeyChar is '\0'. Thus avoids using the prior to check if a key is valid or not and only compare if it's KeyCode.Null.

I agree.

Note that Key already doesn't have Unknown.

I think it should also been removed from the KeyCode enum.

Originally posted by @BDisp in #2927 (comment)

@tig tig added the design Issues regarding Terminal.Gui design (bugs, guidelines, debates, etc...) label Dec 16, 2023
@BDisp
Copy link
Collaborator

BDisp commented Dec 17, 2023

@tig I also have another suggestion that could be done in this issue or open a new issue if you want. Key.IsValid is actually relying => KeyCode is not (KeyCode.Null or KeyCode.Unknown); After removing the KeyCode.Unknown which I enterally agree, it will only relying on => KeyCode is not KeyCode.Null; which could be only be a KeyCode.ShiftMask, KeyCode.AltMask or KeyCode.CtrlMask. These modifiers keys alone or combined without a non modifier key shouldn't be used because I think don't make no sense, but I could be wrong. So my suggestion is change to IsValid => KeyCode != KeyCode.Null && new Key (KeyCode & ~KeyCode.AltMask & KeyCode & ~KeyCode.CtrlMask & KeyCode & ~KeyCode.ShiftMask) != KeyCode.Null;
What do you think?

@tig
Copy link
Collaborator Author

tig commented Dec 17, 2023

@tig I also have another suggestion that could be done in this issue or open a new issue if you want. Key.IsValid is actually relying => KeyCode is not (KeyCode.Null or KeyCode.Unknown); After removing the KeyCode.Unknown which I enterally agree, it will only relying on => KeyCode is not KeyCode.Null; which could be only be a KeyCode.ShiftMask, KeyCode.AltMask or KeyCode.CtrlMask. These modifiers keys alone or combined without a non modifier key shouldn't be used because I think don't make no sense, but I could be wrong. So my suggestion is change to IsValid => KeyCode != KeyCode.Null && new Key (KeyCode & ~KeyCode.AltMask & KeyCode & ~KeyCode.CtrlMask & KeyCode & ~KeyCode.ShiftMask) != KeyCode.Null; What do you think?

On Windows (and maybe other platforms in the future) it is possible to detect a "Shift Key" (Shift, Ctrl, or Alt) press. We should not arbitrarily prevent devs from doing so.

So, as long as the driver doesn't include the CtrlShift mask with the "Ctrl" key event, I agree.

Here I

  • Pressed and released "Ctrl"
  • Pressed and released "Ctrl"
  • Pressed and released "Enter"
  • Pressed "Ctrl", Pressed "U", Released both

image

Also, in the new world, this is better syntax for what you suggest:

	/// <summary>
	/// Indicates whether the <see cref="Key"/> is valid or not. Invalid keys are <see cref="Key.Empty"/>,
	/// and keys with only shift modifiers.
	/// </summary>
	public bool IsValid => this != Empty && (NoAlt.NoShift.NoCtrl != Empty);

Unit tests:

	// IsValid
	[Theory]
	[InlineData (KeyCode.A, true)]
	[InlineData (KeyCode.B, true)]
	[InlineData (KeyCode.F1 | KeyCode.ShiftMask, true)]
	[InlineData (KeyCode.Null, false)]
	[InlineData (KeyCode.ShiftMask, false)]
	[InlineData (KeyCode.CtrlMask, false)]
	[InlineData (KeyCode.AltMask, false)]
	[InlineData (KeyCode.ShiftMask | KeyCode.AltMask, false)]
	public void IsValid (Key key, bool expected) => Assert.Equal (expected, key.IsValid);

@tig
Copy link
Collaborator Author

tig commented Dec 17, 2023

I can have a PR ready for this in a few min. Unless you already have one ready.

@BDisp
Copy link
Collaborator

BDisp commented Dec 17, 2023

I can have a PR ready for this in a few min. Unless you already have one ready.

I'm trying to fix the VkeyPacketSimulator and if you have your ready please submit it, thanks. May help me fix this better.

@tig
Copy link
Collaborator Author

tig commented Dec 17, 2023

Almost ready.

tig added a commit that referenced this issue Dec 17, 2023
* Simplifies Key.IsValid

* Updated unit tests

* Fixed menu
@tig tig closed this as completed Dec 17, 2023
BDisp pushed a commit to BDisp/Terminal.Gui that referenced this issue Dec 29, 2023
* Simplifies Key.IsValid

* Updated unit tests

* Fixed menu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Issues regarding Terminal.Gui design (bugs, guidelines, debates, etc...)
Projects
No open projects
Status: ✅ Done
Development

No branches or pull requests

2 participants