英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论