picoe / Eto

Cross platform GUI framework for desktop and mobile applications in .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MacOs : Cannot trigger an action when Enter is pressed

borjafdezgauna opened this issue · comments

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

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!

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.