Re: compare GdkModifierType



Robert Schulze wrote:

I read the state from the device with gdk_device_get_state and safe it
at state And now, I won't compare the state.
I read at the Gdk Reference:
      GDK_Button1_MASK = 1 << 8;
      GDK_Button2_MASK = 1 << 9;
        ...
If I print the state as a int the Button1 is 1040. Why not 8?

Note the "<<" operator which is not the "=" operator. It's a binary
shift to the left. "1 << 8" is the same as "2*2*2*2*2*2*2*2". This is
256 in decimal, 0x100 in hexadecimal or 1 0000 0000 in binary.

If I won't compare it with state == GDK_BUTTON1_MASK  it doesn't work.
Why? ==== CODE === 
 GdkModifierType      state;
 insigned int         count = 0;
 ...
 gdk_device_get_state(dev, win, NULL, &state);
 if(state == GDK_BUTTON1_MASK) ++count;

You must not use a simple "==" comparison for multi-flag variables. In
those variables you are not interested in the value of the variable as a
whole but rather in the state of single (sometimes multiple) bits. You
need to address (or mask) the bits you want to query. In your case a
simple change of the operator would do:

if (state & GDK_BUTTON1_MASK) ++count;

Understanding this is essential, not only for GTK+ programming. Thus
it's actually not a GTK-specific issue. Thorough explanation of
statements / operators like those is a matter of your C tutorial.

And by the way, don't mistake the "&&" for the "&" operator. The first
one is a binary operator while the second one is a logical operator. To
test arbitrary bits you'll always want to use the binary operator. Print
out expressions like "255 & 8" and "255 && 8" and vary the numbers to
get a better understanding of the difference between logical and binary
operators. The same is true for "|" and "||".



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]