捕获.NET MAUI(Windows)中的Keydown或Keypress

发布于 2025-01-30 16:03:01 字数 501 浏览 3 评论 0原文

我正在为Android和Windows的条形码读取器应用程序工作。在Android上,我会收到每个条形码扫描的系统广播,但是在Windows上,典型配置是从条形码扫描仪发送的键盘条目。因此,我要做的是捕获键盘/按键事件,以便我可以将所有接收到的字符添加到临时字符串中,然后在收到“ Enter”后立即以“条形码读取事件”为“条形码读取事件”。

但是,我无法在任何控件中找到键盘/关键事件。这根本可能吗?如果是这样,我应该在哪里看?最接近的(我认为)我已经了解了如何使用应用程序生命周期事件的描述: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/app-lifecycle

谢谢,最好的问候,

Joerg。

I am working on a barcode reader app for Android and Windows. On Android, I receive a system broadcast for every barcode scanned, but on Windows the typical configuration is a keyboard entry sent from the barcode scanner. So what I am trying to do is capture the KeyDown/KeyPress event so that I can add all characters received into a temporary string and then submit to my app as a "barcode read event" as soon as "Enter" is received.

However, I am unable to find KeyDown/KeyPressed events in any of the controls. Is that possible at all? If so, where do I look? The closest (I think) I have gotten is this description of how to use the App lifecycle events: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/app-lifecycle

Thanks and best regards,

Joerg.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2025-02-06 16:03:01

您可以获得应从 handler.platformview 。然后连接本地事件。

var handler = mauiView.Handler;
UIElement? nativeView = handler?.PlatformView as UIElement;
if (nativeView != null)
{
    nativeView.KeyDown += this.PlatformView_KeyDown;
    nativeView.KeyUp += this.PlatformView_KeyUp;
    nativeView.PreviewKeyDown += this.PlatformView_PreviewKeyDown;
}

在这里,mauiview是应检测到的按键事件的视图。请注意,该视图应集中在要检测到的关键按下事件。

You can obtain the UIElement on which the key pressed events should be detected from Handler.PlatformView. And then hook up the native events.

var handler = mauiView.Handler;
UIElement? nativeView = handler?.PlatformView as UIElement;
if (nativeView != null)
{
    nativeView.KeyDown += this.PlatformView_KeyDown;
    nativeView.KeyUp += this.PlatformView_KeyUp;
    nativeView.PreviewKeyDown += this.PlatformView_PreviewKeyDown;
}

Here, mauiView is the view on which the key pressed events should be detected. Please note that this view should be focused for the key pressed events to be detected.

↙温凉少女 2025-02-06 16:03:01

如果您使用的是条目您不需要读取所有密钥,则可以订阅完成事件> Enter> Enter是读入条目。

因此,可以将许多条形码读取器配置为插入的最后一部分。

MyBarcodeReader()
{ 
    MyBarCodeEntry.Completed += MyBarCodeEntry_Completed;
}

private void MyBarCodeEntry_Completed(object sender, EventArgs e)
{
   /// Do your JOB
}

编辑7月8日2023年:

不幸的是,完成的事件似乎是不可预测的,这导致了误报。因此,我必须更改方法,并决定将听众添加到我的条目绑定源中,并在结果以\ r结束时进行分析。这解决了我所有的误报。

像这样的事情

public static readonly BindableProperty FilterProperty =
BindableProperty.Create(nameof(Filter), typeof(string), typeof(QuickSearch), null, propertyChanged: OnFilterChanged);
public string Filter
{
    get { return (string)GetValue(FilterProperty); }
    set { SetValue(FilterProperty, value); }
}
private static async void OnFilterChanged(BindableObject bindable, object oldValue, object newValue)
{
    if (bindable is QuickSearch q)
        await q.ProccessFilter();
}

private void ProccessFilter()
{
    CapturedCode = Filter?.Replace("\r", "") ?? string.Empty;
    Regex regex = new(SearchParms.RegexTemplate);
    if (Filter == null || regex.IsMatch((string)CapturedCode))
    {
        if (Filter != null && Filter.EndsWith("\r"))
            await ProccessCodeCompleted();
        return;
    }
} 

也许是由毛伊岛虫引起的。 (毛伊岛是一个很好的工具

If you are using an Entry you don't need to read all keys, instead you can subscribe to Completed event witch is fired when Enter is readed into the Entry.

So many barcode readers can be configured to insert Enter as last part of barcode readed.

MyBarcodeReader()
{ 
    MyBarCodeEntry.Completed += MyBarCodeEntry_Completed;
}

private void MyBarCodeEntry_Completed(object sender, EventArgs e)
{
   /// Do your JOB
}

EDIT JUL 8 2023:

Unfortunately the Completed event appears to be unpredictable and this was causing false positive reading. So I had to change my approach and decided to add a listener to my Entry binding source and analize if the results ends with an \r. This solve all my false positives.

Something like this

public static readonly BindableProperty FilterProperty =
BindableProperty.Create(nameof(Filter), typeof(string), typeof(QuickSearch), null, propertyChanged: OnFilterChanged);
public string Filter
{
    get { return (string)GetValue(FilterProperty); }
    set { SetValue(FilterProperty, value); }
}
private static async void OnFilterChanged(BindableObject bindable, object oldValue, object newValue)
{
    if (bindable is QuickSearch q)
        await q.ProccessFilter();
}

private void ProccessFilter()
{
    CapturedCode = Filter?.Replace("\r", "") ?? string.Empty;
    Regex regex = new(SearchParms.RegexTemplate);
    if (Filter == null || regex.IsMatch((string)CapturedCode))
    {
        if (Filter != null && Filter.EndsWith("\r"))
            await ProccessCodeCompleted();
        return;
    }
} 

Maybe this is caused by maui bugs. (Maui is a pretty good tool but still so buggy)

你的背包 2025-02-06 16:03:01

键盘加速器在MAUI .NET 8

。 “ nofollow noreferrer”> https://learn.microsoft.com/en-us/dotnet/maui/maui/user-interface/keyboard-accelerators?view=net-maui-8.0

”

可以通过将其添加到其键盘celerators Collection中:键盘加速器可以将其添加到MenuflyOutItem上:

<MenuFlyoutItem Text="Cut"
                Clicked="OnCutMenuFlyoutItemClicked">
    <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Ctrl"
                             Key="X" />
    </MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>

键盘加速器也可以在代码中指定:

cutMenuFlyoutItem.KeyboardAccelerators.Add(new KeyboardAccelerator
{
    Modifiers = KeyboardAcceleratorModifiers.Ctrl,
    Key = "X"
});

Keyboard accelerators are in MAUI .NET 8.

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/keyboard-accelerators?view=net-maui-8.0

menubar.png

A KeyboardAccelerator can be attached to a MenuFlyoutItem by adding it to its KeyboardAccelerators collection:

<MenuFlyoutItem Text="Cut"
                Clicked="OnCutMenuFlyoutItemClicked">
    <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Ctrl"
                             Key="X" />
    </MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>

Keyboard accelerators can also be specified in code:

cutMenuFlyoutItem.KeyboardAccelerators.Add(new KeyboardAccelerator
{
    Modifiers = KeyboardAcceleratorModifiers.Ctrl,
    Key = "X"
});
油饼 2025-02-06 16:03:01

对于Windows,最好将其连接到本机键盘系统中,从而确保您始终接收输入。

使用条目时,您永远不确定条目具有焦点并且能够接收输入。

一个很好的工具是 sharphook

For windows it is better to hook into the native keyboard system, which makes sure you always receive input.

When using an Entry you're never sure that an entry has focus and is capable of receiving input.

A great tool for this is SharpHook.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文