Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need help with detecting when a key is pressed in WPF?

wpf c# keypressed detect key error

Best Answer BlackRabbit, 07 March 2015 - 01:12 PM

The equivalent is KeyDown.

You need to use something like this:

 

this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F2)
   {
   }
}
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 04 March 2015 - 01:03 PM

Hello,

 

I have been trying to check when a key is pressed in my program such as "F2" and then handle that key press in another method.

 

I have already used: 

this.keyPressed += new System.Windows.Forms.KeyEventHandler(this.keyPressed);

in the MainWindow() method in WPF C#. Then I used this method to handle the key press: 

private void keyPressed(object sender, System.Windows.Forms.KeyEventArgs e)
{
            
}

however, it all seems to be going when until "this.keyPressed" in the first code example above turns red, the error I get is "Cannot assign to 'keyPressed' because it is a 'method group". - Key pressed is the method I used above in the second code example...

 

Any ideas why this error has come up?

 

Thanks for any help in advanced!



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 07 March 2015 - 01:12 PM   Best Answer

The equivalent is KeyDown.

You need to use something like this:

 

this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F2)
   {
   }
}


#3 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 09 March 2015 - 09:47 AM

 

The equivalent is KeyDown.

You need to use something like this:

this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F2)
   {
   }
}

 

Thank you so much for your help! It works!



#4 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 09 March 2015 - 10:14 AM

Just a quick one, is there a way to set it so the key down press works when your not using the program, as when I have the program minimized the key down does not work, it only works when I am using the program? 






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download