3

我不知道如何让这种方法起作用:

System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key)

对象浏览器显示以下内容:

public static bool IsKeyDown(System.Windows.Input.Key key)
System.Windows.Input.Keyboard 的成员
摘要:
确定是否按下了指定的键。
参数:
key:指定的键。
返回值:
如果键处于关闭状态,则为 true;否则为假。

好的,所以它是键盘的成员,对吧?我使用了以下代码:Keyboard test = new Keyboard();

但是当我输入 test 然后输入点时, IsKeyDown 不是一个选项。唯一的选项来自 Windows.Forms 成员。我在这里想念什么?谢谢。

4

2 回答 2

10

Add PresentationCore.dll assembly as a reference.

Add WindowsBase.dll assembly as a reference.

Test code:

private void buttonMisc_Click(object sender, EventArgs e)
{
    if (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift) == true)
        MessageBox.Show("Got it!");
}
于 2012-10-20T01:47:34.547 回答
4

IsKeyDown is static, so you need to use it like

Keyboard.IsKeyDown()

Not with an instantiated object.

You also need to make sure you have the correct using statement at the top:

using System.Windows.Input;

EDIT

On further inspection, Keyboard is a static class... So you can't Keyboard test = new Keyboard();

于 2012-10-20T01:47:53.713 回答