Skip to content

Add global key interceptor to TerminaApplication #82

@Aaronontheweb

Description

@Aaronontheweb

Parent: #77

Summary

Add a mechanism for registering global key handlers that run BEFORE focused component input routing. This fixes the issue where Ctrl+Q can't quit the application because the focused TextInputNode consumes it.

Files to Modify

  • src/Termina/TerminaApplication.cs

Current Problem

Input event received
    ↓
FocusManager.RouteInput() → TextInputNode.HandleInput() returns true
    ↓
Event consumed - ViewModel never sees Ctrl+Q

Solution

// New API
public IDisposable RegisterGlobalKeyHandler(
    ConsoleKey key,
    ConsoleModifiers modifiers,
    Action handler);

// Usage in demo apps
app.RegisterGlobalKeyHandler(ConsoleKey.Q, ConsoleModifiers.Control, () => app.Shutdown());

Implementation

private readonly List<Func<ConsoleKeyInfo, bool>> _globalInterceptors = new();

private void ProcessEvent(object evt)
{
    if (evt is KeyPressed keyPressed)
    {
        // Global interceptors FIRST
        foreach (var interceptor in _globalInterceptors)
        {
            if (interceptor(keyPressed.KeyInfo))
                return; // consumed
        }

        // Then focused component
        if (_focusManager.RouteInput(keyPressed.KeyInfo))
            return;

        // Then ViewModel observable
        _inputSubject.OnNext(evt);
    }
}

Acceptance Criteria

  • Global key handlers registered before app run
  • Handlers checked before focus routing
  • Returns IDisposable for cleanup
  • Ctrl+Q works to quit even when TextInputNode is focused
  • Multiple handlers can be registered
  • Handler can mark key as consumed to prevent further processing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions