[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby::Tk on OS X event binding hint

Morton Goldberg

7/28/2006 12:43:00 PM

On Macintosh OS X, one can use 'Command' and 'Option' as Tk event
type modifiers to get Command and Option key shortcuts. For example,

Tk.root.bind('Command-q') {exit}
Tk.root.bind('Command-Q') {exit}

will give the normal quit-key behavior. Also, if info_mnu is an
instance of TkMenu,

Tk.root.bind('Button-2') {|e| info_mnu.popup(e.root_x, e.root_y)}
Tk.root.bind('Control-Button-1') {|e| info_mnu.popup(e.root_x,
e.root_y)}

will make info_mnu into a contextual menu. The first binding will
serve users with a two- or three-button mouse and the second will
serve those with a one-button mouse.

'Command-' is an alias for 'M1-' and 'Option-' is an alias for 'M2-'.

All of the documentation on Tk event modifiers I've seen so far
doesn't mention this, so I thought I'd post it here. On a Google
search I found a blog reference to 'Command', but nothing on
'Option'. So I don't think this is widely known, but if I'm wrong, I
apologize for this posting.

Regards, Morton

10 Answers

Matthew Smillie

7/28/2006 12:51:00 PM

0

On Jul 28, 2006, at 13:42, Morton Goldberg wrote:

> On Macintosh OS X, one can use 'Command' and 'Option' as Tk event
> type modifiers to get Command and Option key shortcuts. For example,
>
> Tk.root.bind('Command-q') {exit}
> Tk.root.bind('Command-Q') {exit}
>
> will give the normal quit-key behavior. Also, if info_mnu is an
> instance of TkMenu,

Just as an interface technicality, command-shift-q is a global
shortcut for logging out, not for quitting an application.

> Tk.root.bind('Button-2') {|e| info_mnu.popup(e.root_x, e.root_y)}
> Tk.root.bind('Control-Button-1') {|e| info_mnu.popup(e.root_x,
> e.root_y)}
>
> will make info_mnu into a contextual menu. The first binding will
> serve users with a two- or three-button mouse and the second will
> serve those with a one-button mouse.
>
> 'Command-' is an alias for 'M1-' and 'Option-' is an alias for 'M2-'.
>
> All of the documentation on Tk event modifiers I've seen so far
> doesn't mention this, so I thought I'd post it here. On a Google
> search I found a blog reference to 'Command', but nothing on
> 'Option'. So I don't think this is widely known, but if I'm wrong,
> I apologize for this posting.

Much appreciated.

Morton Goldberg

7/28/2006 1:52:00 PM

0

On Jul 28, 2006, at 8:50 AM, Matthew Smillie wrote:

> Just as an interface technicality, command-shift-q is a global
> shortcut for logging out, not for quitting an application.

Thanks for pointing this out. If ever I was aware of that short-cut,
I had forgotten it. The binding to 'Command-Q' is bogus -- and I must
admit I added it without testing it first :(

> Much appreciated.

You're welcome.

Regards, Morton


Morton Goldberg

7/28/2006 2:43:00 PM

0

On Jul 28, 2006, at 8:50 AM, Matthew Smillie wrote:

> Just as an interface technicality, command-shift-q is a global
> shortcut for logging out, not for quitting an application.

I've done the testing that I should have done before my first post on
this thread, and just as another technicality, a ruby application,
not the system, gets the command-shift-q event when the app's window
is active, so inside a ruby app, Cmnd+Shift+q _will_ work as a quit
command with the binding

Tk.root.bind('Command-Q') {exit}

But that doesn't make it a good idea; it would be poor design
practice to use such a binding.

Regards, Morton



Hidetoshi NAGAI

7/28/2006 4:05:00 PM

0

Morton Goldberg

7/28/2006 7:59:00 PM

0

Thank you for your comments. However, I'm pretty new to Ruby Tk, and
I need further explanation to help me understand them.

On Jul 28, 2006, at 12:05 PM, Hidetoshi NAGAI wrote:

>> On Macintosh OS X, one can use 'Command' and 'Option' as Tk event
>> type modifiers to get Command and Option key shortcuts. For example,
>>
>> Tk.root.bind('Command-q') {exit}
>> Tk.root.bind('Command-Q') {exit}
>
> It will not work on other toplevel widgets.
> The following may be better.
> ----------------------------------------
> ev = TkVirtualEvent.new
> ev.add('Command-q')
> ev.add('Command-Shift-q') ### or ev.add('Command-q', 'Command-Shift-
> q')
> TkBindTag::ALL.bind(ev){exit}
> ----------------------------------------

Are you saying that, when my code runs, Command-q keystroke events
will trigger the callback only when the main window (Tk.root) is the
active window? Or is the problem that I've used two calls to bind
when I should have used just one? Or is it something else?

>> will give the normal quit-key behavior. Also, if info_mnu is an
>> instance of TkMenu,
>>
>> Tk.root.bind('Button-2') {|e| info_mnu.popup(e.root_x, e.root_y)}
>> Tk.root.bind('Control-Button-1') {|e| info_mnu.popup(e.root_x,
>> e.root_y)}
>>
>> will make info_mnu into a contextual menu. The first binding will
>> serve users with a two- or three-button mouse and the second will
>> serve those with a one-button mouse.
>
> ev = TkVirtualEvent.new
> ev.add('Button-2', 'Control-Button-1')
> Tk.root.bind(ev){|e| info_mnu.popup(e.root_x, e.root_y)}

Again I ask for further explanation: What problem arises with the
original code that you are fixing? (My code seems to be working with
no problems in my application, but that app has only one window.) In
this case it looks as if the two separate calls to bind are what
bother you.

> --
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Regards, Morton


Hidetoshi NAGAI

7/28/2006 10:04:00 PM

0

Morton Goldberg

7/29/2006 11:34:00 AM

0

Ah, now I understand. I did not consider the points you bring up. Now
I know better. I'm both a Ruby and a Tk newbie, so I'm glad to get
advice from an expert.

Thank you very much. I will heed your advice.

Regards, Morton

On Jul 28, 2006, at 6:03 PM, Hidetoshi NAGAI wrote:

> From: Morton Goldberg <m_goldberg@ameritech.net>
> Subject: Re: Ruby::Tk on OS X event binding hint
> Date: Sat, 29 Jul 2006 04:58:52 +0900
> Message-ID: <BF26EC2E-BDF2-4875-9C7E-D60FA9037A9D@ameritech.net>
>>> It will not work on other toplevel widgets.
>>> The following may be better.
>>> ----------------------------------------
>>> ev = TkVirtualEvent.new
>>> ev.add('Command-q')
>>> ev.add('Command-Shift-q') ### or ev.add('Command-q', 'Command-Shift-
>>> q')
>>> TkBindTag::ALL.bind(ev){exit}
>>> ----------------------------------------
>>
>> Are you saying that, when my code runs, Command-q keystroke events
>> will trigger the callback only when the main window (Tk.root) is the
>> active window? Or is the problem that I've used two calls to bind
>> when I should have used just one? Or is it something else?
>
> I'm sorry if I misunderstanded your post.
> When your application has two or more windows, your binding will work
> on the root widget (or widgets on the root widget) only.
> Of course, you are right if that is what you want.
> The bindtags list of the widget on the other toplevel widget doesn't
> include the root widget.
> Please see Tcl/Tk's manual of `bindtags'.
>
>>> ev = TkVirtualEvent.new
>>> ev.add('Button-2', 'Control-Button-1')
>>> Tk.root.bind(ev){|e| info_mnu.popup(e.root_x, e.root_y)}
>>
>> Again I ask for further explanation: What problem arises with the
>> original code that you are fixing? (My code seems to be working with
>> no problems in my application, but that app has only one window.) In
>> this case it looks as if the two separate calls to bind are what
>> bother you.
>
> "Don't Repeat Yourself" to prevent bugs. ;-)
> If the part (and events for the callback) never change in the life
> cycle of your software, it has no problem.
> --
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
>


William Smargiassi

7/29/2006 12:39:00 PM

0

I would think you never want to bind Command-<key> and Command-Shift-
<key> to the same function. The two key shortcuts should do something
different (but probably related) like Command-S is save, but Command-
Shift-S is save-as.

bill

On Jul 28, 2006, at 10:42 , Morton Goldberg wrote:

> On Jul 28, 2006, at 8:50 AM, Matthew Smillie wrote:
>
>> Just as an interface technicality, command-shift-q is a global
>> shortcut for logging out, not for quitting an application.
>
> I've done the testing that I should have done before my first post
> on this thread, and just as another technicality, a ruby
> application, not the system, gets the command-shift-q event when
> the app's window is active, so inside a ruby app, Cmnd+Shift+q
> _will_ work as a quit command with the binding
>
> Tk.root.bind('Command-Q') {exit}
>
> But that doesn't make it a good idea; it would be poor design
> practice to use such a binding.
>
> Regards, Morton
>
>
>
>


Morton Goldberg

7/29/2006 1:26:00 PM

0

On Jul 29, 2006, at 8:39 AM, William Smargiassi wrote:

> I would think you never want to bind Command-<key> and Command-
> Shift-<key> to the same function. The two key shortcuts should do
> something different (but probably related) like Command-S is save,
> but Command-Shift-S is save-as.

I fully agree. That's why I wrote:

>> But that doesn't make it a good idea; it would be poor design
>> practice to use such a binding.

Regards, Morton



Hidetoshi NAGAI

7/29/2006 3:22:00 PM

0