Reading Keyboard state

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’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

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

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

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’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

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

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’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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs