Namespace KeyRoutedEventArgs could not be found even when using using System.Windows.Input in net maui?

huangapple go评论70阅读模式
英文:

Namespace KeyRoutedEventArgs could not be found even when using using System.Windows.Input in net maui?

问题

我正在查看.NET MAUI的文档,但找不到如何检测键盘按键,包括普通按键、组合键和功能键。我查看了一个帖子 https://stackoverflow.com/questions/72358210/how-to-detect-keyboard-inputs 并尝试使用以下代码:

#if WINDOWS
public void MyKeyDownEventHandler(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == 27)
    {
        //在这里编写代码
    }
}
#endif

但即使在加入以下代码之后:

#if WINDOWS
using System.Windows.Input;
#endif

仍然出现错误,提示找不到命名空间KeyRoutedEventArgs。

我期望能够在C#代码中至少检测普通按键和功能键。我也想检测键的组合,但不是必需的。

英文:

I was checking docs of net maui, but could not found how to detect key press including normal, combos and function keys. I checked at one post https://stackoverflow.com/questions/72358210/how-to-detect-keyboard-inputs and tried using

#if WINDOWS
public void MyKeyDownEventHandler(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == 27)
        {
            //code here
        }
}
#endif

but even after

#if WINDOWS
using System.Windows.Input;
#endif

it gives error namespace KeyRoutedEventArgs could not be found.

I expect to be able to detect at least normal and function keys in C# code. I also want to detect Combo of keys but not necessary.

答案1

得分: 2

总之,应该有一种标准方法来检测物理键盘是否被按下或检查键盘状态,如下所示。如果可以的话,可以使用其功能。

var isNumLockToggled = Keyboard.IsKeyToggled(Key.NumLock);
var isCapsLockToggled = Keyboard.IsKeyToggled(Key.CapsLock);
var isScrollLockToggled = Keyboard.IsKeyToggled(Key.Scroll);

然而,目前尚未支持此功能。您可以查看以下链接:

https://github.com/dotnet/maui/issues/3858

https://github.com/dotnet/maui/issues/9764

https://github.com/dotnet/maui/issues/3739

英文:

In conclusion, there should be a standard way to detect whether a physical keyboard is pressed or check key status like below.And if so, to use its functionality.

var isNumLockToggled = Keyboard.IsKeyToggled(Key.NumLock);
var isCapsLockToggled = Keyboard.IsKeyToggled(Key.CapsLock);
var isScrollLockToggled = Keyboard.IsKeyToggled(Key.Scroll);

However, this hasn't been supported. You can follow up the links below.

https://github.com/dotnet/maui/issues/3858

https://github.com/dotnet/maui/issues/9764

https://github.com/dotnet/maui/issues/3739

huangapple
  • 本文由 发表于 2023年2月23日 21:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定