捕获.NET MAUI(Windows)中的Keydown或Keypress
我正在为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以获得应从 handler.platformview 。然后连接本地事件。
在这里,
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.
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.如果您使用的是
条目
您不需要读取所有密钥,则可以订阅完成
事件>Enter> Enter
是读入条目。因此,可以将许多条形码读取器配置为插入的最后一部分。
编辑7月8日2023年:
不幸的是,完成的事件似乎是不可预测的,这导致了误报。因此,我必须更改方法,并决定将听众添加到我的条目绑定源中,并在结果以\ r结束时进行分析。这解决了我所有的误报。
像这样的事情
也许是由毛伊岛虫引起的。 (毛伊岛是一个很好的工具
If you are using an
Entry
you don't need to read all keys, instead you can subscribe toCompleted
event witch is fired whenEnter
is readed into the Entry.So many barcode readers can be configured to insert
Enter
as last part of barcode readed.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
Maybe this is caused by maui bugs. (Maui is a pretty good tool but still so buggy)
键盘加速器在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上:
键盘加速器也可以在代码中指定:
Keyboard accelerators are in MAUI .NET 8.
https://learn.microsoft.com/en-us/dotnet/maui/user-interface/keyboard-accelerators?view=net-maui-8.0
A KeyboardAccelerator can be attached to a MenuFlyoutItem by adding it to its KeyboardAccelerators collection:
Keyboard accelerators can also be specified in code:
对于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.