26

In my application i detect when a key is pressed and see if the modifier is the shift key but the Keys enumerator has Shift and ShiftKey.

It seems the event is always sending Keys.Shift, but is there a case where the Keys.ShiftKey will be used?

(and the same question applies to Keys.Control and Keys.ControlKey)

Thanks for any input.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Brian Tacker
  • 1,091
  • 2
  • 18
  • 37

2 Answers2

25

Keys.Shift is a modifier key (used for key combinations) while Keys.ShiftKey is a regular key code just like most others in the Keys enumeration.

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Does this mean that Keys.Shift could be triggered from a special mouse or some other peripheral, while Keys.ShiftKey cannot? – Ryan Gross Jun 22 '11 at 21:54
  • That makes alot of sense. Thanks for your help. p.s. is there a reason they could have the same enumerator value for both? – Brian Tacker Jun 22 '11 at 21:58
  • 4
    @Brian @Ryan If you look at [`KeyEventArgs`](http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx), you'll use `Keys.Shift` to check against `KeyEventArgs.Modifiers` while you would use `Keys.ShiftKey` to check against `KeyEventArgd.KeyCode`. Check out the examples in MSDN. – Bala R Jun 23 '11 at 12:52
  • You'd also use `Keys.Shift` when checking against `Control.ModifierKeys` – test Dec 13 '21 at 17:00
9

Keys.ShiftKey refers to the actual shift key while Keys.Shift refers to the shift modification itself. Keys.ShiftKey can be used like the other key codes to check for presses, but you cannot check to see if the Keys.Shift was pressed because it represents a state rather than an object. I hope this makes sense.

See here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

bradenb
  • 793
  • 4
  • 11