System.Windows.Forms.Keys.HasFlag behaving strange

I have the following code designed to prevent the user from writing new lines in a note text editor:

private void m_commentMemoEdit_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData.HasFlag(Keys.Enter))
    {
        e.SuppressKeyPress = true;
    }
}

      

This does prevent Enter from being typed, but oddly enough, it does not allow other keys to be inserted. So far we have found that the keys: "O", "M", "/" and "-" are also "caught".

Update: The following code does what I need:

private void m_commentMemoEdit_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyValue == (int)Keys.Return)
  {
    e.SuppressKeyPress = true;
  }
}

      

But I still don't understand that the previous code is not working and this is happening.

I looked through the enum System.Windows.Forms.Keys

but didn't find any clues (although I have to say this is one oddly constructed enum). Can anyone explain why this is happening?

0


source to share


3 answers


HasFlags () inherits from Enum.HasFlags (). This is useful for enumerations declared with the [Flags] attribute. It uses the operator and to perform a test for bit values. The problem is that Keys.Enter is not a flag value. Its value is 0x0d, 3 bits are set. Thus, any key that has a value with bits 0, 2, or 3 will return true. Like Keys.O, it is 0x4f. 0x4f and 0x0d = 0x0d, so HasFlags () returns true.

You should only use it with Keys values, which actually represent the flag values. These are Keys.Alt, Keys.Control and Keys.Shift. Note that these are modifier keys. This way you can use HasFlags to see the difference between, say, F and Ctrl + F.

To detect Keys.Enter you have to do a simple comparison. How did you find out. Note that your if () statement is also correct for Alt + Enter etc., this may not be what you want. Use instead



if (e.KeyData == Keys.Return) e.SuppressKeyPress = true;

      

Which suppresses the Enter key only if none of the modifier keys are pressed.

+3


source


I suspect this is because the underlying enumeration values System.Windows.Forms.Keys

map directly to keyboard codes, which are not always mutually exclusive. The documentation says you shouldn't use any bitwise operation on them and gives an example why not ( Beware of FlagsAttribute on enum! ).

MSDN also says that the function HasFlag

returns the result of this:, thisInstance And flag = flag

so you could set a breakpoint and look at the actual binaries entering the system and make sure this operation will give you the true value for the set of keys you listed.



At the end of the day, your updated code is the right way to do what you want to do.

0


source


HasFlag for flags - this means the bit is set or not

the key value is an ordinal value, so completely different from flags use the comparison operator and you should be fine.

some enum fields are defined as flags, for example.

enum SomeFlag
{
   BitOne = 1, 
   BitTwo = 2, 
   Bitthree = 4
};

      

it makes sense to use "HasFlag" here

0


source







All Articles