为什么任务延迟在window_keyup和window_keydown方法中不起作用?
我有三个按钮。当我按下3个可选按钮中的任何一个时,计时器应该启动。例如。当我按蓝色黑头topbtn按钮时,计时器开始。如果我在1秒内按一个或两个按钮中的一个或两个,则为标签提供值。在这种情况下,如果按下按钮2次,则不应重视标签。 加载窗口时,单击按钮时它可以工作,但是当我按键盘时它不起作用。
这些按钮工作正常,但是 window_keyup 和 window_keydown 方法中的代码正在出现故障。
private async void blueHeadTopBtn_Click(object sender, RoutedEventArgs e)
{
BlueHead();
WinBluePoint();
blueHeadTopBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadTopBtn.IsEnabled = true;
}
private async void blueHeadLeftBtn_Click(object sender, RoutedEventArgs e)
{
BlueHead();
WinBluePoint();
blueHeadLeftBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadLeftBtn.IsEnabled = true;
}
private async void blueHeadRightBtn_Click(object sender, RoutedEventArgs e)
{
BlueHead();
WinBluePoint();
blueHeadRightBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadRightBtn.IsEnabled = true;
}
这些代码工作不正确。
private async void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Q)
{
BlueHead();
WinBluePoint();
blueHeadTopBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadTopBtn.IsEnabled = true;
}
if (e.Key == Key.E)
{
BlueHead();
WinBluePoint();
blueHeadLeftBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadLeftBtn.IsEnabled = true;
}
if (e.Key == Key.R)
{
BlueHead();
WinBluePoint();
blueHeadRightBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadRightBtn.IsEnabled = true;
}
}
我该如何解决这个问题?我是否正确使用任务了?
I have three buttons. The timer should have started when I pressed any of the 3 optional buttons. For example. When I press the blueHeadTopBtn button, the timer starts. If I press one or both of the remaining 2 buttons in 1 second, it gives value to the label. in this case, if a button is pressed 2 times, the label should not be valued.
When the window is loaded, it works when I click the buttons, but it does not work when I press the keyboard.
These buttons are working correctly but the codes in Window_KeyUp and Window_KeyDown methods are malfunctioning.
private async void blueHeadTopBtn_Click(object sender, RoutedEventArgs e)
{
BlueHead();
WinBluePoint();
blueHeadTopBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadTopBtn.IsEnabled = true;
}
private async void blueHeadLeftBtn_Click(object sender, RoutedEventArgs e)
{
BlueHead();
WinBluePoint();
blueHeadLeftBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadLeftBtn.IsEnabled = true;
}
private async void blueHeadRightBtn_Click(object sender, RoutedEventArgs e)
{
BlueHead();
WinBluePoint();
blueHeadRightBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadRightBtn.IsEnabled = true;
}
These codes are working incorrectly.
private async void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Q)
{
BlueHead();
WinBluePoint();
blueHeadTopBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadTopBtn.IsEnabled = true;
}
if (e.Key == Key.E)
{
BlueHead();
WinBluePoint();
blueHeadLeftBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadLeftBtn.IsEnabled = true;
}
if (e.Key == Key.R)
{
BlueHead();
WinBluePoint();
blueHeadRightBtn.IsEnabled = false;
await Task.Delay(1000);
blueHeadRightBtn.IsEnabled = true;
}
}
How can I solve this issue? Did I use TASK correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您的代码是错误的。从查看您的神秘代码片段,您似乎想在按下按钮1秒钟。我假设您想通过键盘输入实现相同的操作:按下该键后的特定键。
在您的钥匙上处理程序中,您仅禁用按钮。当然,这对键盘输入没有影响。您必须禁用关键事件处理。
为此,要么在钥匙锁的持续时间内取消注册事件处理程序,要么使用标志。
该示例没有为应该参与锁定逻辑的每个密钥或按钮创建标志,而是使用
dictionary
,该示例允许实现无需额外的努力而扩展。这也可以通过消除您拥有的重复代码来改善您的代码。现在,我们可以为每个按钮使用共享事件处理程序,而专用处理程序用于键盘输入:mainwindow.xaml.cs
mainwindow.xaml
I guess your code is wrong. From reviewing your mysterious code snippets, it appears like you want to disable a button for 1s after it was pressed. I assume you want to achieve the same with the keyboard input: ignore a particular key for 1s after it was pressed.
In your key up handlers you only disable the buttons. This of course has no effect on the keyboard input. You must disable the key event handling.
To accomplish this, either unregister the event handler for the duration of the key lock or use a flag.
Instead of creating a flag for every key or button that should participate in the locking logic, this example uses a
Dictionary
which allows the implementation to scale without extra efforts. This also improves your code by eliminating the duplicate code that you had. Now, we can use a shared event handler for every button and a dedicated handler for keyboard input:MainWindow.xaml.cs
MainWindow.xaml