Ruby::Tk on OS X event binding hint

M

Morton Goldberg

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
 
M

Matthew Smillie

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.
 
M

Morton Goldberg

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
 
M

Morton Goldberg

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
 
H

Hidetoshi NAGAI

From: Morton Goldberg <[email protected]>
Subject: Ruby::Tk on OS X event binding hint
Date: Fri, 28 Jul 2006 21:42:49 +0900
Message-ID: said:
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)}
 
M

Morton Goldberg

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

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?
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.

Regards, Morton
 
H

Hidetoshi NAGAI

From: Morton Goldberg <[email protected]>
Subject: Re: Ruby::Tk on OS X event binding hint
Date: Sat, 29 Jul 2006 04:58:52 +0900
Message-ID: said:
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'.
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.
 
M

Morton Goldberg

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
 
W

William Smargiassi

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
 
M

Morton Goldberg

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.

Regards, Morton
 
H

Hidetoshi NAGAI

From: Morton Goldberg <[email protected]>
Subject: Re: Ruby::Tk on OS X event binding hint
Date: Sat, 29 Jul 2006 22:26:29 +0900

I'm very sorry. Probably, 'Command-Shift-q' doesn't work.
It must be 'Command-Q'.
If you have to distinguish between <Shift> + <q> and <CapsLock> + <q>,
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top