1

Can an event be fired when the shift key is pressed, and when the shift key is released? Can someone please provide sample code for how to do this?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Larsen187
  • 21
  • 2
  • 3
    Do you have any code that you've tried? If so paste that and we'll be happy to help you figure out why it's not working. – taylonr Mar 12 '11 at 03:41

3 Answers3

3
private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (!e.IsRepeat && (e.Key == Key.LeftShift || e.Key == Key.RightShift))
    {
        Trace.WriteLine("Down Shift " + DateTime.Now);
    }
}

private void Window_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
    {
        Trace.WriteLine("Up Shift " + DateTime.Now);
    }
}
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
0

To do this you have to first decide about on which control is activated and on that control you have to generate the Key_press event.

On key_press event you have to check about which key is pressed i.e. you need shift. So you need to code about if shift key is pressed then do functionality you want to do.

So this is the way how you can accomplish.

Hope this can help you.

http://www.java2s.com/Code/CSharp/Event/Shiftkeypressed.htm

Bhavik Goyal
  • 2,786
  • 6
  • 23
  • 42
0

John, take a look at my solution for the Enter Key. You could certainly apply it to the shift key as well if there is a particular control you want to do this for. Otherwise, please provide more information a solution can be narrowed down.

Community
  • 1
  • 1
scottrudy
  • 1,633
  • 1
  • 14
  • 24
  • 3
    your link goes to this page. Are you trying to cause a recursion stack overflow *on* StackOverflow?? :) – jb. Mar 12 '11 at 07:48