How to get IsKeyDown method to work in C#

十年热恋 提交于 2019-12-17 22:28:32

问题


I can’t figure out how get this method to work:

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

The object browser says the following:

public static bool IsKeyDown(System.Windows.Input.Key key)
Member of System.Windows.Input.Keyboard
Summary:
Determines whether the specified key is pressed.
Parameters:
key: The specified key.
Return Values:
true if key is in the down state; otherwise, false.

Okay, so it’s a member of Keyboard, right? I used the following code: Keyboard test = new Keyboard();

But when I type test and then the dot, IsKeyDown is not an option. The only options are from the Windows.Forms members. What am I missing here? Thanks.


回答1:


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!");
}



回答2:


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();



来源:https://stackoverflow.com/questions/12984522/how-to-get-iskeydown-method-to-work-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!