0

I have a Custom Entry Control (ExtendedEntryCell) in Xamarin Forms and want to detect if the user pressed "Return" on the keyboard. The custom control looks like this.

public class ExtendedEntryCell : Entry
{
    public event EventHandler<EventArgs> KeyPressed;

    public void SendKeyPressed(object sender, EventArgs e)
    {
        KeyPressed?.Invoke(sender, e);
    }
}

For Windows Phone I created a Custom Renderer based on this (External Link) example.

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);

    if (Control == null)
        return;

    Control.KeyDown -= Control_KeyDown;
    Control.KeyDown += Control_KeyDown;
}

private void Control_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
    {
        (Element as ExtendedEntryCell).SendKeyPressed(Element, new EventArgs());
    }
}

Is there a similar solution for iOS in Xamarin Forms?

Torben
  • 438
  • 1
  • 7
  • 22
  • 1
    The Xamarin.Forms Entry control has a [Completed](https://developer.xamarin.com/api/event/Xamarin.Forms.Entry.Completed/) event. Is that what you're looking for? – j.f. Jan 22 '18 at 14:32
  • Yes and No :D You are right, it will solve the problem in my case, but anyways: Is there a way to figure out, which key got pressed? – Torben Jan 22 '18 at 16:27
  • Not as easily, but the UITextFieldDelegate provides a [`shouldChangeCharactersIn`](https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-textfield) method or you can subscribe to the [`UIControlEventEditingChanged`](https://developer.apple.com/documentation/uikit/uicontrolevents/uicontroleventeditingchanged) event. [Here](https://stackoverflow.com/q/4923950/3711928) lists some differences. One major note is that these more or less tell you what the text is changing/has changed to, not exactly what key was pressed. – j.f. Jan 22 '18 at 16:45

1 Answers1

1

Subscribe the Completed event, it will fire when user clicks return:

MyEntry.Completed += MyEntry_Completed;
private void MyEntry_Completed(object sender, EventArgs e)
{
}

Is there a way to figure out, which key got pressed?

Subscribe the TextChanged event, through detecting the OldTextValue and NewTextValue we can get which key being tapped like:

private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)
{
    Debug.WriteLine(string.Format("old value:{0}", e.OldTextValue));
    Debug.WriteLine(string.Format("new value:{0}", e.NewTextValue));
}

But in forms we can only get this two values, if the user try to insert text from middle position, we may calculate the wrong key string. because we can't get the range where user begin to insert. To achieve this, we should create a rederer for entry, and subscribe the delegate:

public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
{
    return true;
}

the replacementString means which character being tapped. if it is "", then user clicks the backspace.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
  • Thanks for your detailed explanation. I will mark this as result, if there is realy no other way of doing it. – Torben Jan 23 '18 at 14:05