Ali
Ali

Reputation: 51

.net maui custom entry keypress event handling

Is there a way to capture keypress event for a custom entry control in .Net Maui? for example if i have the class:

public class MyEntry : Entry
{

}

And I want to perform an action everytime a user for example presses the tab key on his keyword while he is typing/focusing the entry.

Upvotes: 5

Views: 6100

Answers (1)

Riccardo Minato
Riccardo Minato

Reputation: 1807

If you can use a character which is recognized by the Entry, this could work for you.

public class MyEntry : Entry
{
    public MyEntry()
    {
        TextChanged += MyEntry_TextChanged;
    }

    private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (e.OldTextValue != null
            && e.NewTextValue.Length <= e.OldTextValue.Length)
        {
            return;
        }

        if (e.NewTextValue.Last() == /*Insert your character here*/)
        {
            // Do your things
        }
    }
}

This won't work if the key you press on your keyboard has some other meaning which is not recognized as a character to write inside the Entry.

Upvotes: 2

Related Questions