# Reading Keyboard state

**URL:** <https://www.ruby-forum.com/t/reading-keyboard-state/88139>\
**Category:** Ruby-Gnome 2\
**Created:** [March 8, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/reading-keyboard-state/88139 "2007-03-08T13:24:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [March 8, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/reading-keyboard-state/88139/1 "2007-03-08T13:24:51Z")

</div>

Hi,

I’m managing the selection of a list of widgets.

During a button-pressed-event on the container I want to check  
whether SHIFT and/or CTRL are pressed to adjust how the selection  
changes.

How can I read the keyboard state at a particular instant?

Thanks,  
Martin

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys-and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [March 8, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/reading-keyboard-state/88139/2 "2007-03-08T16:41:50Z")

</div>

Martin P. wrote:

> Hi,
> 
> I’m managing the selection of a list of widgets.
> 
> During a button-pressed-event on the container I want to check  
> whether SHIFT and/or CTRL are pressed to adjust how the selection  
> changes.
> 
> How can I read the keyboard state at a particular instant?
> 
> Thanks,  
> Martin

There may be a better way of doing this, but my first suggestion would  
be to connect to the key-press-event and key-release-event for the same  
container. Then you can set some boolean variable somewhere.

Something like

widget.signal\_connect(“key-press-event”) do |a, key|  
@shift\_down = true if Gdk::Keyval.to\_name(key.keyval) == “Shift\_L”  
@control\_down = true if Gdk::Keyval.to\_name(key.keyval) == “Control\_L”  
end

widget.signal\_connect(“key-release-event”) do |a, key|  
@shift\_down = false if Gdk::Keyval.to\_name(key.keyval) == “Shift\_L”  
@control\_down = false if Gdk::Keyval.to\_name(key.keyval) ==  
“Control\_L”  
end

Then you can check @shift\_down and @control\_down whenever you need to.

It does seem inefficient though. I’d love to know if it’s possible to  
query the status of the keyboard.

best,  
Dan

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [March 8, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/reading-keyboard-state/88139/3 "2007-03-08T17:42:31Z")

</div>

On Thu, Mar 08, 2007 at 12:11:18PM +0000, Martin P. wrote:

> Hi,
> 
> I’m managing the selection of a list of widgets.
> 
> During a button-pressed-event on the container I want to check  
> whether SHIFT and/or CTRL are pressed to adjust how the selection  
> changes.
> 
> How can I read the keyboard state at a particular instant?

When you get a button-press-event, the second argument, a  
Gdk::EventButton, includes a “state” property:

[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AEventButton#state](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AEventButton#state)

You can inspect that to see which modifiers are currently active during  
the button press.

-pete

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys-and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [March 9, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/reading-keyboard-state/88139/4 "2007-03-09T16:00:30Z")

</div>

Peter J. wrote:

> When you get a button-press-event, the second argument, a  
> Gdk::EventButton, includes a “state” property:
> 
> [http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AEventButton#state](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AEventButton#state)
> 
> You can inspect that to see which modifiers are currently active during  
> the button press.

Thank you. Just what I was looking for, but didn’t find as I was  
searching the apis, but missed this as i was using  
‘keyboard’ in the search. oops.

Martin.

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys-and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [March 8, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/reading-keyboard-state/88139/5 "2007-03-08T17:55:56Z")

</div>

Peter J. wrote:

> When you get a button-press-event, the second argument, a  
> Gdk::EventButton, includes a “state” property:
> 
> [http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AEventButton#state](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AEventButton#state)
> 
> You can inspect that to see which modifiers are currently active during  
> the button press.
> 
> -pete

Thanks you peter, I wanted to know the answer to that one as well.

Dan
