Detecting KeyDown in an entry? RRS feed

  • Question

  • User67482 posted


    I'd like to detect when the backspace keyboard button is pressed in an Entry even when there is no text in the entry. I've tried creating a custom renderer and overriding OnKeyDown but it doesn't seem to do anything. In my custom android renderer that extends Entry:

    public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
        Console.WriteLine("A key was pressed");
        if (keyCode == Keycode.Del)
            // do stuff here
        return base.OnKeyDown(keyCode, e);

    Unfortunately this doesn't seem to do anything. How can I achieve this?

    Friday, December 19, 2014 7:26 PM

All replies

  • User89791 posted

    According to the hover-over on OnKeyDown, keyboards usually don't fire the OnKeyDown and OnKeyUp (or any of them, for that matter) events. I tried to use OnKeyPreIme and was still not able to hit a breakpoint in that method.

    I also tried this and it didn't work:

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            if (e.OldElement == null)
                var nativeEditText = (global::Android.Widget.EditText)Control;
                nativeEditText.KeyPress += CustomEntryRenderer_KeyPress;
     void CustomEntryRenderer_KeyPress(object sender, global::Android.Views.View.KeyEventArgs e)
            if (e.KeyCode == Keycode.Del)
                var lol = 1;
    Friday, December 19, 2014 8:12 PM
  • User67482 posted

    I also tried using the OnKeyPreIme method and had no success. How is the Entry handling keyboard input events?

    Friday, December 19, 2014 8:55 PM
  • User67482 posted

    Anyone know how to do this? Spent hours trying every event handler and tinkering with custom renderers with no success.

    Monday, December 22, 2014 6:09 AM
  • User84987 posted

    Have you tried attaching an ITextWatcher (EditText.AddTextChangedListener) or TextView.IOnEditorActionListener (EditText.SetOnEditorActionListener) to your EditText in the custom renderer?

    Monday, December 22, 2014 6:16 AM
  • User230706 posted

    I know this is an old post, but since I needed the answer recently I thought others might want this:

    If you only need to detect a return on an Entry control, add the following code: XAML <Entry x:Name="entryLoginID" Completed="EntryLoginID_OnCompleted"/>

    CS FILE 'private void EntryLoginID_OnCompleted(object sender, EventArgs e)' { //Run This action on KeyDown Return MyAction(); }

    This worked perfect for my needs. I used MyAction to Perform the same action as a button on my page. In this case, validating the entry fields for login information and then loading the next page or displaying an error if invalid.

    Sunday, July 17, 2016 1:05 AM
  • User230706 posted

    This worked for me: XAML <Entry x:Name="entryLoginID" FontSize="40" Completed="EntryLoginID_OnCompleted"/> CS FILE private void EntryLoginID_OnCompleted(object sender, EventArgs e) { //Run This Action on Entry Return KeyDown MyAction(); }

    Sunday, July 17, 2016 1:12 AM
  • User242293 posted

    You should be using Commands in XAML not Events...XAML is a command driven, not event driven framework

    Sunday, July 17, 2016 2:36 AM
  • User1278 posted

    Did you ever find a solution to this? I need to look for arrow keys being pressed. So I cannot use completed and cannot use text changed. I have the same behavior as you, no events are fired for keypress or OnKeyDown overrride in my custom renderer.

    Wednesday, June 28, 2017 5:05 PM
  • User389955 posted

    i had the the problem about D-pad buttons and Media buttons. when i switched on googleAPIs in my emulator android, the trouble disappeared.

    Thursday, October 24, 2019 12:05 PM