# Mask of a Gdk::ScrollEvent

**URL:** <https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967>\
**Category:** Ruby-Gnome 2\
**Created:** [December 8, 2009, 10:04pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967 "2009-12-08T22:04:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 8, 2009, 10:04pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/1 "2009-12-08T22:04:50Z")

</div>

I try to handle scroll events on an Gtk::StatusIcon. I woulld like to do  
somehing like that

#!/usr/bin/env ruby  
###call this script with one argument (a path to an image).  
require ‘gtk2’  
si=Gtk::StatusIcon.new  
si.pixbuf=Gdk::Pixbuf.new(ARGV[0])  
si.signal\_connect(‘scroll-event’){|icon, event|  
p event.state  
p event.state.to\_i  
case event.state  
when 0#Empty mask  
p ‘scroll event’  
p event.direction  
when Gdk:🪟:CONTROL\_MASK  
p ‘control+scroll event’  
end  
}  
Gtk.main

On my latop, I did not work.  
If I use ctrl+scroll, event.state.inspect shows a control mask type but  
event.state.to\_i==8196 or Gdk:🪟:CONTROL\_MASK.to\_i==4. Apparently  
the 14th bit of event.state is equal to 1.  
[http://library.gnome.org/devel/gdk/unstable/gdk-Windows.html#GdkModifierType](http://library.gnome.org/devel/gdk/unstable/gdk-Windows.html#GdkModifierType)  
tells nothing about this bit.  
Why this bit is 1? Will it always be 1?

I could use event.state.inspect and strings in the case statement but it  
really seems ugly.

I do not know what to do.

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 8, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/2 "2009-12-08T23:30:21Z")

</div>

Vincent C. wrote:  
I reread gtk doc. It seems that bits 13 and 14 are used by xkb.  
For now, I will use a workaround to have bits 13 and 14 equal to 0.

b=event.state.to\_i.to\_s(2)  
b.reverse!;b[13]=0 if b[13];b[14]=0 if b[14];b.reverse!  
case b.to\_i(2)  
when 0  
…  
when Gdk::Window::CONTROL\_MASK  
…  
end

Not very clean but it does not rely on the output of inspect method.

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 9, 2009, 12:32pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/3 "2009-12-09T12:32:50Z")

</div>

2009/12/8 Vincent C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> …  
> end

I think you do not understand how masks should be used.

Here is a working example :  
#!/usr/bin/env ruby  
#call this script with one argument (a path to an image).  
require ‘gtk2’  
si = Gtk::StatusIcon.new  
si.pixbuf = Gdk::Pixbuf.new(ARGV[0])  
si.signal\_connect(‘scroll-event’) do |icon, event|  
p event.state  
p event.direction  
case  
when (event.state & Gdk:🪟:CONTROL\_MASK) != 0  
p ‘control + scroll event’  
when (event.state & Gdk:🪟:SHIFT\_MASK) != 0  
p ‘shift + scroll event’  
else  
p ‘scroll only’  
end  
end  
Gtk.main

It works for me. I always have #\<Gdk:🪟:ModifierType mod2-mask\>,  
which seems to be “numlock”.

If you do not want to manage control/shift/alt, you don’t really care  
about it, since you only need direction.

Hope it helps.

Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 10, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/4 "2009-12-10T18:24:15Z")

</div>

Hi Simon.  
Thanks for you reply.

I do want manage crontol/shift… since I want to call different actions  
depending on the modifier used.

I understand !(event.state & Gdk:🪟:CONTROL\_MASK) check if control  
is hit. But it do not check if control is the only modifier hit.  
Am I wrong?

I think I will have to used a solution close to yours. Cause I cannot be  
sure none other modifier (num lock, caps lock…) is pressed.  
event.state.control\_mask? should be equal to !(event.state &  
Gdk:🪟:CONTROL\_MASK) and it look nicer. 🙂

Thanks for your help.

Simon A. wrote:

> Here is a working example :  
> #!/usr/bin/env ruby  
> #call this script with one argument (a path to an image).  
> require ‘gtk2’  
> si = Gtk::StatusIcon.new  
> si.pixbuf = Gdk::Pixbuf.new(ARGV[0])  
> si.signal\_connect(‘scroll-event’) do |icon, event|  
> p event.state  
> p event.direction  
> case  
> when (event.state & Gdk:🪟:CONTROL\_MASK) != 0  
> p ‘control + scroll event’  
> when (event.state & Gdk:🪟:SHIFT\_MASK) != 0  
> p ‘shift + scroll event’  
> else  
> p ‘scroll only’  
> end  
> end  
> Gtk.main
> 
> It works for me. I always have #\<Gdk:🪟:ModifierType mod2-mask\>,  
> which seems to be “numlock”.
> 
> If you do not want to manage control/shift/alt, you don’t really care  
> about it, since you only need direction.
> 
> Hope it helps.
> 
> Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 11, 2009, 11:03am UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/5 "2009-12-11T11:03:23Z")

</div>

2009/12/10 Vincent C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I understand !(event.state & Gdk:🪟:CONTROL\_MASK) check if control  
> is hit. But it do not check if control is the only modifier hit.  
> Am I wrong?

You are right, my example was to show you how to use bitmask. It is  
not functional :).  
I don’t know of syntactic sugar for bitmasks in ruby, so you have to  
program almost like in C.

> I think I will have to used a solution close to yours. Cause I cannot be  
> sure none other modifier (num lock, caps lock…) is pressed.  
> event.state.control\_mask? should be equal to !(event.state &  
> Gdk:🪟:CONTROL\_MASK) and it look nicer. 🙂

I don’t think you need to know what other controls are on or off, you  
already know you are in a scroll event, just check the modifier you  
want to manage (ctrl, shift, alt, whatever).

If you want to manage ctrl, shift, and ctrl+shift, it would be  
something like this:  
if (event.state & Gdk:🪟:CONTROL\_MASK) then  
code  
elif (event.state & Gdk:🪟:SHIFT\_MASK) then  
code  
elif (event.state & (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK))  
then  
code  
else  
code  
end

you could use an empty case also.

Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 11, 2009, 11:46am UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/6 "2009-12-11T11:46:12Z")

</div>

I am currently tring this code :

mask=0; mod=event.state  
mask+=1 if mod.control\_mask?# 1==2**0  
mask+=2 if mod.shift\_mask?# 2==2**1  
mask+=4 if mod.mod1\_mask?#Alt 4=2\*\*2  
case mask  
when 0  
code  
when 1  
code  
when 2  
code  
when 3  
code  
…  
end

It look very similar to yours. Instead of checking the bit mask, I use a  
integer corresponding to each bit. After all integer are store binary!  
(Not sure I am very clear)  
If It does not work, I will try your code.

Thanks for your time.

Simon A. wrote:

> I don’t think you need to know what other controls are on or off, you  
> already know you are in a scroll event, just check the modifier you  
> want to manage (ctrl, shift, alt, whatever).
> 
> If you want to manage ctrl, shift, and ctrl+shift, it would be  
> something like this:  
> if (event.state & Gdk:🪟:CONTROL\_MASK) then  
> code  
> elif (event.state & Gdk:🪟:SHIFT\_MASK) then  
> code  
> elif (event.state & (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK))  
> then  
> code  
> else  
> code  
> end
> 
> you could use an empty case also.
> 
> Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 11, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/7 "2009-12-11T12:07:42Z")

</div>

I did not think about that.  
But of course if (event.state &  
(Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)) is true,  
(event.state & Gdk:🪟:CONTROL\_MASK) is necessarry true. So we  
should check against (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)  
first.  
Thanks Pascal T. for this fine remark.

Pascal T. wrote:

> Le vendredi 11 dï¿½cembre 2009 ï¿½ 11:02 +0100, Simon A. a ï¿½crit :
> 
> > if (event.state & Gdk:🪟:CONTROL\_MASK) then  
> > code  
> > elif (event.state & Gdk:🪟:SHIFT\_MASK) then  
> > code  
> > elif (event.state & (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)) then
> 
> This one should be first, else you will never enter it because  
> event.state & Gdk:🪟:CONTROL\_MASK will be true

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 11, 2009, 12:44pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/8 "2009-12-11T12:44:03Z")

</div>

2009/12/11 Vincent C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â code  
> when 2  
> Â code  
> when 3  
> Â code  
> …  
> end
> 
> It look very similar to yours. Instead of checking the bit mask, I use a  
> integer corresponding to each bit. After all integer are store binary!

I did not thought about that. But it’s much simpler than you think :

mask = event.state &  
(Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK|…)  
case mask  
when Gdk:🪟:CONTROL\_MASK  
code  
when Gdk:🪟:SHIFT\_MASK  
code  
when Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK  
code  
end

You could switch to integers, but may gdk changes its mask, very  
unlikely, but who knows, it would break.  
Taking out verbosity, the code is much understandable imo with the  
constants.

And thanks Pascal, my exmaple was terrible indeed 🙂

Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 11, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/9 "2009-12-11T13:11:17Z")

</div>

If gdk changes its mask, my code should not brake cause the integer is  
build with control\_mask? and co methods. But you are write your last  
code seems more understable. I think I will rewrite my method.

Once again thanks.

Simon A. wrote:

> mask = event.state &  
> (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK|…)  
> case mask  
> when Gdk:🪟:CONTROL\_MASK  
> code  
> when Gdk:🪟:SHIFT\_MASK  
> code  
> when Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK  
> code  
> end
> 
> You could switch to integers, but may gdk changes its mask, very  
> unlikely, but who knows, it would break.  
> Taking out verbosity, the code is much understandable imo with the  
> constants.
> 
> And thanks Pascal, my exmaple was terrible indeed 🙂
> 
> Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 11, 2009, 11:55am UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/10 "2009-12-11T11:55:29Z")

</div>

Le vendredi 11 décembre 2009 à 11:02 +0100, Simon A. a écrit :

> if (event.state & Gdk:🪟:CONTROL\_MASK) then  
> code  
> elif (event.state & Gdk:🪟:SHIFT\_MASK) then  
> code  
> elif (event.state & (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)) then

This one should be first, else you will never enter it because  
event.state & Gdk:🪟:CONTROL\_MASK will be true

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 12, 2009, 4:27am UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/11 "2009-12-12T04:27:44Z")

</div>

Hey, could you show your final version, if you don’t mind?

I’d like to collect interesting code (such as this) if it is working. 🙂

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 12, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/12 "2009-12-12T21:13:57Z")

</div>

Marc H. wrote:

> Hey, could you show your final version, if you don’t mind?

I will as soon as it is written.

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 13, 2009, 1:33am UTC](https://www.ruby-forum.com/t/mask-of-a-gdk-scrollevent/179967/13 "2009-12-13T01:33:29Z")

</div>

My “code with integer” works but I finally use Simon code.

#Check only for control, alt and shift.  
mask=event.state&(Gdk::Window::CONTROL\_MASK|Gdk::Window::MOD1\_MASK|Gdk::Window::SHIFT\_MASK)  
case mask  
when 0#no modifier key hit  
code  
when Gdk::Window::CONTROL\_MASK  
code  
when (Gdk::Window::CONTROL\_MASK|dk::Window::MOD1\_MASK)  
code  
…  
end

It works well on my laptop.

Vincent
