osvein
osvein

Reputation: 645

Extract KeyCode from KeyData

How can I extract the KeyCode flag (a System.Windows.Forms.Keys value without modifiers) itself from a System.Windows.Forms.Keys value?

Let's say the Keys has the flags Keys.Control, Keys.Shift and Keys.A. I want to extract the Keys.A flag, but the Keys´s flags (including modifiers) are variable.

Upvotes: 1

Views: 1636

Answers (2)

Hans Passant
Hans Passant

Reputation: 941465

The Keys enum already has a mask for that, its name will not surprise you:

  Keys code = keyData & Keys.KeyCode;

Its underlying value is 0xffff, effectively masking off the modifier state bits. A similar mask value is available to isolate the modifiers bits, its is Keys.Modifiers (0xffff0000).

Upvotes: 8

King King
King King

Reputation: 63317

I think this is what you want:

Keys excludeModifier = yourKey & ~Keys.Control & ~Keys.Shift & ~Keys.Alt;

Upvotes: 2

Related Questions