# Keystrokes detection for non-control, meta key

**URL:** <https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442 "2007-01-19T16:30:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![teeshift](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@teeshift](https://www.ruby-forum.com/u/teeshift)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442/1 "2007-01-19T16:30:27Z")

</div>

I have tried

@canvas.bind(‘k’, proc{|e| })  
@canvas.bind(‘KeyPress-k’, proc{|e| })  
@canvas.bind(‘Key-k’, proc{|e| })

well… none of them work.  
Any wise words?

Thank you.

Tee Shift

---

<div class="post-metadata">

**Author:** ![teeshift](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@teeshift](https://www.ruby-forum.com/u/teeshift)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442/2 "2007-01-19T16:30:53Z")

</div>

teeshift wrote:

> I have tried
> 
> @canvas.bind(‘k’, proc{|e| })  
> @canvas.bind(‘KeyPress-k’, proc{|e| })  
> @canvas.bind(‘Key-k’, proc{|e| })
> 
> well… none of them work.

Try  
root.bind(“Key-k”) {…}

IIRC, the reason is that key events are sent to the top level window  
unless you have focused on a subwindow.

---

<div class="post-metadata">

**Author:** ![teeshift](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@teeshift](https://www.ruby-forum.com/u/teeshift)\
**Post date:** [September 25, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442/3 "2007-09-25T22:34:16Z")

</div>

Thank you Joel.

I tried and it worked. Now, is there a way to find which key users have  
pressed without  
specifying it first?

@canvas.bind(‘Key’, proc{})

what should be inside the ‘proc’? I know e.x, e.y but have tried  
e.keys, e.key, e.value  
without success and did not find any valuable information on Event.

Thank you.

Tee

---

<div class="post-metadata">

**Author:** ![teeshift](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@teeshift](https://www.ruby-forum.com/u/teeshift)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442/4 "2007-09-25T23:04:15Z")

</div>

Ignore my previous message. The answer is at

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/f0cd7b379e8b195f/e9fded6c453a28bb?lnk=gst&q=get+pixel+canvas&rnum=1#e9fded6c453a28bb](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/f0cd7b379e8b195f/e9fded6c453a28bb?lnk=gst&q=get+pixel+canvas&rnum=1#e9fded6c453a28bb)

However, I would love to know about the Qt4 and Java. Does it work on  
their canvas?

---

<div class="post-metadata">

**Author:** ![teeshift](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@teeshift](https://www.ruby-forum.com/u/teeshift)\
**Post date:** [September 25, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442/5 "2007-09-25T22:47:20Z")

</div>

teeshift wrote:

> Thank you Joel.

You’re welcome!

> I tried and it worked. Now, is there a way to find which key users have  
> pressed without  
> specifying it first?
> 
> @canvas.bind(‘Key’, proc{})

I was curious about this too.

To find out, add something like this:

```
   root.bind "Key" do |stuff|
     p stuff, stuff.methods
   end

```

Then press a key in your window. It turns out that the “stuff” is an  
Event instance, and it has @keysym=“h”, and there are #keycode and  
#keysym methods.

If you add the line

```
     p stuff.keycode, stuff.keysym

```

then you can see that pressing the “h” key gives this output:

43  
“h”

So probably the #keysym method is what you want.

---

<div class="post-metadata">

**Author:** ![teeshift](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@teeshift](https://www.ruby-forum.com/u/teeshift)\
**Post date:** [September 25, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/keystrokes-detection-for-non-control-meta-key/82442/6 "2007-09-25T23:07:38Z")

</div>

Thanks. Joel.

Sorry for the late reply. Did not have much time doing doodling on  
Ruby.

The other question, I will post it on the group with a different  
title.  
Is there any method to get a pixel from the canvas? I can draw a pixel  
(painfully by using either  
line or rectangle, same storage anyway) but can I sample a pixel ?

I vaguely recall that Java seems to do that. Do not know if Qt 4 does  
it. ( A complete novice on Qt4  
and haven’t coded Java for years.)
