Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

winforms - How to get IsKeyDown method to work in C#

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...