Ginxxx
Ginxxx

Reputation: 1648

Keypress Event on Xamarin Forms

I want to fire a keypress event like ENTER and trigger my method but I can't find any reference about doing a keypress event in xaml and call it on my view model

Could someone throw some reference. Please thanks.

Upvotes: 1

Views: 11421

Answers (1)

waletoye
waletoye

Reputation: 364

An input view (Entry or Editor) has to show the Keyboard first, therefore handle the TextChanged event of the input view.

First attach the event:

    public MainPage()
    {
        InitializeComponent();

        txtEntry.TextChanged += TxtEntry_TextChanged;
    }

Then handle the event

    void TxtEntry_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
        txtEntry.TextChanged -= TxtEntry_TextChanged;
        char key = e.NewTextValue?.Last() ?? ' ';

        if (key == 'A')
        {
            //do something 
        }


        txtEntry.TextChanged += TxtEntry_TextChanged;
    }

Upvotes: 1

Related Questions