Ruby::Tk on OS X event binding hint

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

On Jul 28, 2006, at 13:42, Morton G. 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.

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.

On Jul 28, 2006, at 8:50 AM, Matthew S. 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 :frowning:

Much appreciated.

You’re welcome.

Regards, Morton

From: Morton G. [email protected]
Subject: Ruby::Tk on OS X event binding hint
Date: Fri, 28 Jul 2006 21:42:49 +0900
Message-ID: [email protected]

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}

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)}

On Jul 28, 2006, at 8:50 AM, Matthew S. 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

From: Morton G. [email protected]
Subject: Re: Ruby::Tk on OS X event binding hint
Date: Sat, 29 Jul 2006 04:58:52 +0900
Message-ID: [email protected]

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. :wink:
If the part (and events for the callback) never change in the life
cycle of your software, it has no problem.

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:

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?

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 ([email protected])

Regards, Morton

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

I would think you never want to bind Command- and Command-Shift-
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

From: Morton G. [email protected]
Subject: Re: Ruby::Tk on OS X event binding hint
Date: Sat, 29 Jul 2006 22:26:29 +0900
Message-ID: [email protected]

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.

I’m very sorry. Probably, ‘Command-Shift-q’ doesn’t work.
It must be ‘Command-Q’.
If you have to distinguish between + and + ,
please check `state’ field (‘%s’) of the event.

Sometimes Tk.callback_break or Tk.callback_continue is useful

to control the flow of callback operations.

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

I would think you never want to bind Command- and Command-
Shift- 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