How to get IsKeyDown method to work in C#

后端 未结 2 412
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 22:42

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

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

The object browser says

相关标签:
2条回答
  • 2020-12-11 23:01

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

    0 讨论(0)
  • 2020-12-11 23:17

    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!");
    }
    
    0 讨论(0)
提交回复
热议问题