WPF的keyDown响应时间精度 [英] WPF keyDown response time accuracy

查看:157
本文介绍了WPF的keyDown响应时间精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林发展哪些用户在应用中可以看到的东西,并有通过点击键盘上的按键反应。反应时间是至关重要的,在更准确的越好。

Im developing application in which user can see something and has to react by clicking a key on the keyboard. Reaction time is crucial and the more accurate the better it is.

我写了示例应用程序的inf WPF只有少数的code线测试默认设置:

I wrote sample app inf wpf only few lines of code to test default settings:

namespace Test
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    private Stopwatch sw; 
    public MainWindow()
    {
        InitializeComponent();
        sw = new Stopwatch();
        sw.Start();
        this.KeyDown += OnKeyDown;
    }

    private void OnKeyDown(object sender, KeyEventArgs keyEventArgs)
    {
        sw.Stop();

        lbl.Content = sw.ElapsedMilliseconds;
        sw.Restart();
    }
  }
}

LBL 只是简单的标签。

奇怪的是,当我preSS例如空间并保持在此范围内的LBL变化值:30-33

What is strange is that when I press for example space and hold it the value of the lbl changes in the range: 30-33.

所以我不能predict什么是响应精度?这是不可能的具有例如1毫秒精度?用户点击空间,并在同一时间(例如1毫秒精度),我可以处理它在事件处理程序?

So I cant predict what is the response accuracy ? It is impossible to have for example 1 ms accuracy ? User hits space and in the same time (for example 1 ms accuracy) I can handle it in event handler ?

的主要问题是:

可以说我有一个keydown事件处理程序:

lets say I have a keydown event handler:

Test_KeyDown(object sender, KeyEventArgs keyEventArgs)
{
   time = stopwatch.elapsed();
   stopwatch.Restart();
}

什么是时间都不可能发生的最小值。我可以肯定的是,时间值精确到1毫秒? 在这种方法我开始计时,但后来我不得不等待 - 多长时间 - 对于GUI刷新?

what is the minimum value of "time" that can possibly occur. Can I be sure that the time value is accurate to 1 ms ? In this method I start the stopwatch but then I have to wait - how long - for the GUI to refresh ?

推荐答案

您的测试肯定是无效的,它只是衡量正如其他一些贡献者的键盘重复率。但是,对于非预期的效益非常有用,你可以看到,你不会有键盘有问题。

Your test is certainly invalid, it just measures the keyboard repeat rate as pointed out by several other contributors. But it is useful for its unintended benefit, you can actually see that you won't have a problem with the keyboard.

大多数事件在Windows出现在那个年代由时钟中断率确定的一个比率。在默认情况下蜱每秒6​​4次,每15.625毫秒一次。这会唤醒内核,并去寻找,看看有什么需要做的,找工作要传递给处理器内核。最典型的没有什么可以做的,其核心是关机用HLT指令。直到下一次中断。

Most events in Windows occur at a rate that's determined the by the clock interrupt rate. Which by default ticks 64 times per second, once every 15.625 milliseconds. That wakes up the kernel and it goes looking to see if something needs to be done, looking for work to pass to a processor core. Most typically there's nothing to do and the core is shutdown with a HLT instruction. Until the next interrupt occurs.

因此​​,一个关心的是,你的测试可能不会比15.625毫秒更准确。而你的观察一致,意外,你所看到的是这个数字两倍。但是,这不是真正的情况,你可以使用你的程序一看就知道。使用控制面板+键盘和调整的重复率滑块。注意如何调整,并得到你的电话号码更改为不属于15.625的倍数值。

So a concern would be that your test could never be more accurate than 15.625 milliseconds. And your observation matches, by accident, what you see was twice that number. But that's not actually the case and you can use your program to see that. Use Control Panel + Keyboard and tweak the Repeat Rate slider. Note how you can adjust it and get your number to change to values that are not a multiple of 15.625.

这并不完全是一个意外,键盘控制器也产生一个中断,就像时钟一样。你有积极的证据,这中断本身已经足以让你的程序重新激活。你可以告诉大家,键盘控制器本身就足以快速扫描矩阵键盘。从键盘你的错误栏将不会超过+/- 2毫秒,你在显示的数字看到噪音。如果你有一个键盘,扫描速度慢,你可以用这个测试消灭它。

This is not entirely an accident, the keyboard controller also generates an interrupt, just like the clock does. You have positive proof that this interrupt itself is already good enough to get your program to re-activate. And you can tell that the keyboard controller itself is fast enough scanning the keyboard matrix. Your error bar from the keyboard won't be larger than +/- 2 msec, about the noise you see in the displayed number. In case you have a keyboard that scans slower, you can eliminate it with this test.

远更大的担忧,你拥有的是视频。视频适配器典型地以每秒60更新刷新LCD显示器。因此最坏的情况下,所述测试受试者将不能够在物理上的的图像为17毫秒。和液晶显示器本身并没有那么快或者,在便宜的有16毫秒或者更糟糕的响应时间。在晶体中的液体A的副作用不能够翻转速度不够快。

The far bigger concern you have is the video. The video adapter typically refreshes an LCD monitor at 60 updates per second. So worst case, the test subject would not be able to physically see the image for 17 msec. And LCD monitors themselves are not that fast either, the cheap ones have a response time of 16 msec or worse. A side-effect of the Crystal in the Liquid not being able to flip fast enough.

消除刷新率错误要求您的程序与垂直消隐同步。有些东西你可以与DirectX做。你可以发现,有大约4毫秒的响应时间,高档液晶显示器,深受游戏玩家。

Eliminating the refresh rate error requires that you program synchronizes with the vertical blanking interval. Something you can do with DirectX. You can find upscale LCD monitors that have response times of about 4 msec, popular with gamers.

这篇关于WPF的keyDown响应时间精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆