Skip to content

Add KeyDown and KeyUp events to Entry with iOS empty field support#20

Draft
Copilot wants to merge 10 commits intomainfrom
copilot/fix-19
Draft

Add KeyDown and KeyUp events to Entry with iOS empty field support#20
Copilot wants to merge 10 commits intomainfrom
copilot/fix-19

Conversation

Copy link

Copilot AI commented Jul 24, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Summary

This PR adds KeyDown and KeyUp events to the Entry control with special handling for iOS/macOS platforms to ensure events trigger even when the entry field is empty and backspace is pressed.

Problem

On iOS and macOS platforms, when an Entry field is empty and the user presses the backspace key, no events were being triggered. This was because the native UITextField doesn't call the ShouldChangeCharacters delegate when there's no text to delete.

Solution

Core Changes

  1. Added KeyEventArgs class - Provides key information to event handlers
  2. Extended Entry with key events - Added KeyDown and KeyUp events with corresponding SendKeyDown/SendKeyUp methods
  3. Enhanced IEntry interface - Added KeyDown and KeyUp methods for platform handlers
  4. iOS-specific implementation - Override DeleteBackward in MauiTextField to capture backspace on empty fields

Key Features

  • Backspace detection on empty Entry: Works on iOS/macOS even when text field is empty
  • Cross-platform interface: Consistent API across all platforms
  • Non-breaking: Existing Entry functionality remains unchanged
  • Complete event coverage: Both KeyDown and KeyUp events are supported

Usage Example

var entry = new Entry();
entry.KeyDown += (sender, e) => {
    Debug.WriteLine($"Key pressed: {e.Key}");
};
entry.KeyUp += (sender, e) => {
    Debug.WriteLine($"Key released: {e.Key}");
};

Technical Implementation

iOS Platform (MauiTextField):

public override void DeleteBackward()
{
    // Trigger KeyDown event for backspace, even if text is empty
    KeyDown?.Invoke(this, "Backspace");
    
    base.DeleteBackward();
    
    // Trigger KeyUp event for backspace
    KeyUp?.Invoke(this, "Backspace");
}

Entry Handler Connection:

platformView.KeyDown += OnKeyDown;
platformView.KeyUp += OnKeyUp;

void OnKeyDown(object? sender, string key) => VirtualView?.KeyDown(key);
void OnKeyUp(object? sender, string key) => VirtualView?.KeyUp(key);

Testing

  • Added comprehensive UI tests in KeyEventsOnEmptyEntry.xaml and corresponding test cases
  • Tests verify both empty field backspace scenario and regular typing scenarios
  • Tests ensure events are triggered with correct key information

Platform Support

  • iOS/macOS: ✅ Full implementation with empty field backspace support
  • Other platforms: 🔧 Interface ready for future implementation (events will be triggered when platform handlers are implemented)

Fixes #19.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 4zjvsblobprodcus390.vsblob.vsassets.io
    • Triggering command: dotnet build src/Controls/tests/TestCases.HostApp/ --configuration Debug --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits July 24, 2025 10:46
Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Copilot AI changed the title [WIP] Events and Methods Not Triggering for Empty Entry on Mac and iOS Add KeyDown and KeyUp events to Entry with iOS empty field support Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Events and Methods Not Triggering for Empty Entry on Mac and iOS

2 participants