mattdwen
mattdwen

Reputation: 5428

How to detect key press on Xamarin.Mac NSTexField?

I am trying to detect when a user presses enter while editing a single line NSTextField, so as to submit the data they've entered.

There doesn't appear to be any events on NSTextField which would be useful,

Upvotes: 1

Views: 1343

Answers (1)

TheNextman
TheNextman

Reputation: 12566

You can subclass NSTextField and override the keyUp method:

[Register("CustomTextField")]
private class CustomTextField : MonoMac.AppKit.NSTextField
{
    public CustomTextField(IntPtr handle) : base(handle)
    {

    }

    public override void KeyUp (NSEvent theEvent)
    {
        if (theEvent.KeyCode == 36) {
            Console.WriteLine ("You pressed enter!");
        }
    }
}

Upvotes: 5

Related Questions