# Overriding key bindings in tktext

**URL:** <https://www.ruby-forum.com/t/overriding-key-bindings-in-tktext/179956>\
**Category:** Ruby\
**Created:** [December 8, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/overriding-key-bindings-in-tktext/179956 "2009-12-08T18:59:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 8, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/overriding-key-bindings-in-tktext/179956/1 "2009-12-08T18:59:53Z")

</div>

Does tktext let me completely override bindings? For instance, if I  
wanted to disable the enter key, editor.bind(“Any-Key-Return”) {}  
doesn’t do anything - the newline gets inserted.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 9, 2009, 12:51am UTC](https://www.ruby-forum.com/t/overriding-key-bindings-in-tktext/179956/2 "2009-12-09T00:51:39Z")

</div>

From: Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: overriding key bindings in tktext  
Date: Wed, 9 Dec 2009 02:58:01 +0900  
Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Does tktext let me completely override bindings? For instance, if I  
> wanted to disable the enter key, editor.bind(“Any-Key-Return”) {}  
> doesn’t do anything - the newline gets inserted.

You have to break the evaluation of the widget’s bindtag list.  
By default, the bindtag list is [_the\_widget_, TkText,  
_toplevel\_window\_of\_the\_widget_, “all”].  
You define a binding to _the\_widget_. But after the callback,  
Tk evaluates bindings of TkText (depends on the order of bindtags list).

You can break the evaluation of bindtags by  
editor.bind(“Any-Key-Return”){…; break; …}  
or editor.bind(“Any-Key-Return”){…; Tk.callback\_break; …}.  
If the former has no effect (depends on Ruby or Ruby/Tk version),  
please use the latter.  
The latter should work on any version of Ruby/Tk.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 9, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/overriding-key-bindings-in-tktext/179956/3 "2009-12-09T22:32:11Z")

</div>

On Wed, Dec 9, 2009 at 5:17 AM, Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> please use the latter.  
> The latter should work on any version of Ruby/Tk.

Thanks, that works beautifully!

martin
