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

MacOs : Cannot trigger an action when Enter is pressed #2566

Open
borjafdezgauna opened this issue Oct 20, 2023 · 2 comments
Open

MacOs : Cannot trigger an action when Enter is pressed #2566

borjafdezgauna opened this issue Oct 20, 2023 · 2 comments
Labels
Milestone

Comments

@borjafdezgauna
Copy link

Hello,
I am using the PasswordBox control and it works fine, but on MacOs, KeyDown event is not triggered when an Enter is pressed. I have been playing with a customized version of the PasswordBoxHandler (which seems to trigger the event only when the text is changed, not when special keys such as Enter is pressed), but haven't been able to do it.

Any pointers as to what I should do to solve this minor issue would be appreciated. Thanks!!

Expected Behavior

I would expect KeyDown to be triggered when Enter is pressed, as it is on Windows/Linux

Actual Behavior

It is not triggered.

Steps to Reproduce the Problem

Create a PasswordBox, attach a KeyDown event listener, and do something when Enter is pressed

Code that Demonstrates the Problem

PasswordBox passwordBox = new PasswordBox();
passwordBox.KeyDown += OnKeyPressed;
...

protected void OnKeyPressed(object sender, KeyEventArgs e)
{
    if (e.Key == Keys.Enter)
    {
       //This condition is never met on MacOS
    }
}

Specifications

  • Version: 2.8
  • Platform(s): Mac64
  • Operating System(s): MacOS
@borjafdezgauna
Copy link
Author

I found a way to detect the Enter key. In case someone has the same issue...

I created a custom PasswordBoxHandler for Mac64 (I copied the original implementation).

I edited AttachEvent():

public override void AttachEvent(string id)
{
	switch (id)
	{
		case TextControl.TextChangedEvent:
			Control.Changed += HandleChanged;
			break;
		case TextControl.KeyDownEvent:
			Control.DoCommandBySelector += KeyDownEvent;
			break;
		default:
			base.AttachEvent(id);
			break;
	}
}

And I created a new KeyDownEvent method to handle commands:

private bool KeyDownEvent(NSControl control, NSTextView textView, Selector commandSelector)
{
	var handler = this;
    if (handler != null && commandSelector.Name == "insertNewline:")
        handler.Callback.OnKeyDown(handler.Widget, new KeyEventArgs(Eto.Forms.Keys.Enter, KeyEventType.KeyDown));
	return false;
}

Then, I added my custom handler in the Mac64 version of the program:

platform.Add(typeof(Eto.Forms.PasswordBox), () => new [MY_NAMESPACE].PasswordBoxHandler());

Cheers!

@cwensley
Copy link
Member

Hey @borjafdezgauna,

Thanks for the issue report and the workaround. The PasswordBox on macOS uses a custom field editor so there's no way for us to trap the keydown in the usual way. Handling the enter key as you have done sounds like a reasonable thing to add, and it looks like we might be able to add support for arrow keys and escape.

@cwensley cwensley added the bug label Oct 29, 2023
@cwensley cwensley added this to the 2.8.x milestone Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants