From 156f587cfadd503505079d9518f89184ea1769dd Mon Sep 17 00:00:00 2001 From: Johan Polson <40406620+johanpolson@users.noreply.github.com> Date: Sun, 9 Mar 2025 11:46:09 +0100 Subject: [PATCH] Use "handled" for keybord input in Browser (#18349) Co-authored-by: Julien Lebosquain --- .../Avalonia.Browser/Interop/InputHelper.cs | 15 +++++++++++---- .../webapp/modules/avalonia/input.ts | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Browser/Avalonia.Browser/Interop/InputHelper.cs b/src/Browser/Avalonia.Browser/Interop/InputHelper.cs index 29ddb36e9e..088c1d4a9c 100644 --- a/src/Browser/Avalonia.Browser/Interop/InputHelper.cs +++ b/src/Browser/Avalonia.Browser/Interop/InputHelper.cs @@ -13,16 +13,23 @@ internal static partial class InputHelper return Task.CompletedTask; } + public static Task RedirectInputRetunAsync(int topLevelId, Func handler, T @default) + { + if (BrowserTopLevelImpl.TryGetTopLevel(topLevelId) is { } topLevelImpl) + return Task.FromResult(handler(topLevelImpl)); + return Task.FromResult(@default); + } + [JSImport("InputHelper.subscribeInputEvents", AvaloniaModule.MainModuleName)] public static partial void SubscribeInputEvents(JSObject htmlElement, int topLevelId); [JSExport] - public static Task OnKeyDown(int topLevelId, string code, string key, int modifier) => - RedirectInputAsync(topLevelId, t => t.InputHandler.OnKeyDown(code, key, modifier)); + public static Task OnKeyDown(int topLevelId, string code, string key, int modifier) => + RedirectInputRetunAsync(topLevelId, t => t.InputHandler.OnKeyDown(code, key, modifier), false); [JSExport] - public static Task OnKeyUp(int topLevelId, string code, string key, int modifier) => - RedirectInputAsync(topLevelId, t => t.InputHandler.OnKeyUp(code, key, modifier)); + public static Task OnKeyUp(int topLevelId, string code, string key, int modifier) => + RedirectInputRetunAsync(topLevelId, t => t.InputHandler.OnKeyUp(code, key, modifier), false); [JSExport] public static Task OnBeforeInput(int topLevelId, string inputType, int start, int end) => diff --git a/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts b/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts index 5d14597642..704028271d 100644 --- a/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts +++ b/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts @@ -95,16 +95,23 @@ export class InputHelper { public static subscribeKeyEvents(element: HTMLInputElement, topLevelId: number) { const keyDownHandler = (args: KeyboardEvent) => { - JsExports.InputHelper.OnKeyDown(topLevelId, args.code, args.key, this.getModifiers(args)); - if (this.clipboardState !== ClipboardState.Pending) { - args.preventDefault(); - } + JsExports.InputHelper.OnKeyDown(topLevelId, args.code, args.key, this.getModifiers(args)) + .then((handled: boolean) => { + if (!handled || this.clipboardState !== ClipboardState.Pending) { + args.preventDefault(); + } + }); }; element.addEventListener("keydown", keyDownHandler); const keyUpHandler = (args: KeyboardEvent) => { - JsExports.InputHelper.OnKeyUp(topLevelId, args.code, args.key, this.getModifiers(args)); - args.preventDefault(); + JsExports.InputHelper.OnKeyUp(topLevelId, args.code, args.key, this.getModifiers(args)) + .then((handled: boolean) => { + if (!handled) { + args.preventDefault(); + } + }); + if (this.rejectClipboard) { this.rejectClipboard(); }