跟踪WPF应用程序上的按键和按键释放

时间:2015-01-25 17:10:46

标签: c# wpf

有没有办法跟踪WPF上的按键和释放操作?

这是我到目前为止所尝试过的,但我发现的是_upDownKeyIsPressed只有在按下另一个键时才设置为false - 而不是当向上或向下键是释放

    private void KeyUpPress(KeyEventArgs e)
    {
        if (e.Key == Key.Up || e.Key == Key.Down)
        {
            _upDownKeyIsPressed = true;
            Console.WriteLine(_upDownKeyIsPressed.ToString());
        }
    }

    private void KeyDownPress(KeyEventArgs e)
    {
        if (e.Key == Key.Up || e.Key == Key.Down)
        {
            _upDownKeyIsPressed = false;
            Console.WriteLine(_upDownKeyIsPressed.ToString());
        }
    }

    private void PlotListView_KeyUp(object sender, KeyEventArgs e)
    {
        KeyUpPress(e);
    }

    private void PlotListView_KeyDown(object sender, KeyEventArgs e)
    {
        KeyDownPress(e);
    }

1 个答案:

答案 0 :(得分:1)

如果你想尝试我在评论中说的话。我想到这样的事情:

    private void PlotListView_KeyUp(object sender, KeyEventArgs e)
    {
        TrackUpDownKeyPress(e, false);
    }

    private void PlotListView_KeyDown(object sender, KeyEventArgs e)
    {
        TrackUpDownKeyPress(e, true);
    }

    private void TrackUpDownKeyPress(KeyEventArgs e, bool isPressed)
    {
        if (e.Key == Key.Up || e.Key == Key.Down)
            _upDownKeyIsPressed = isPressed;
    }

如果您想了解有关keyUp和keyDown之间差异的更多信息,请查看此question