# Catching TAB presses in a TextView

**URL:** <https://www.ruby-forum.com/t/catching-tab-presses-in-a-textview/83292>\
**Category:** Ruby-Gnome 2\
**Created:** [January 24, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/catching-tab-presses-in-a-textview/83292 "2007-01-24T22:53:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stoffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@stoffe](https://www.ruby-forum.com/u/stoffe)\
**Post date:** [January 24, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/catching-tab-presses-in-a-textview/83292/1 "2007-01-24T22:53:51Z")

</div>

Hello,

first, hope it’s ok to ask questions on this list. I couldn’t any  
other appropriate place to ask, but if it isn’t ok just say so and I’m  
gone. 🙂

I would like to catch TAB presses when editing text in a Gtk::TextView  
under certain circumstances, but I’m not sure how to go about it. What  
I want to do is implement a rudimentary tab completion, nothing more  
than inserting a whole word from a lookup table if there is some  
matching text to the left of the cursor. I’ve experimented with some  
different signals in the simple-editor sample, and I can detect TABs  
but not stop them.

I suppose it might be possible to detect the TAB press, delete it from  
the buffer and then insert the new stuff, but that seems a bit  
backwards and may look strange(?)

So, just wondering, is there any clean way to catch the TAB being  
pressed before the buffer is updated and cancel the insertion?

Many thanks,

– Kristoffer

## – Kristoffer LundÃ©n âœ‰ removed\_email\_address@domain.invalid âœ‰ removed\_email\_address@domain.invalid [http://www.gamemaker.nu/](http://www.gamemaker.nu/) â˜Ž 0704 48 98 77

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys - and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![stoffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@stoffe](https://www.ruby-forum.com/u/stoffe)\
**Post date:** [January 24, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/catching-tab-presses-in-a-textview/83292/2 "2007-01-24T23:08:53Z")

</div>

On Wed, Jan 24, 2007 at 10:53:04PM +0100, Kristoffer Lund??n wrote:

> I would like to catch TAB presses when editing text in a Gtk::TextView  
> under certain circumstances, but I’m not sure how to go about it. What  
> I want to do is implement a rudimentary tab completion, nothing more  
> than inserting a whole word from a lookup table if there is some  
> matching text to the left of the cursor. I’ve experimented with some  
> different signals in the simple-editor sample, and I can detect TABs  
> but not stop them.

The following shows how to stop tabs, by connecting to the  
key-press-event, and returning “TRUE”, which stops the event from  
propogating further (meaning getting to Gtk::TextView at all):

require 'gtk2'

tv = Gtk::TextView.new

accept\_tab = false  
tv.signal\_connect(‘key-press-event’) {  
accept\_tab = !accept\_tab  
!accept\_tab  
}

sw = Gtk::ScrolledWindow.new  
sw.add(tv)

w = Gtk::Window.new(‘test’)  
w.add(sw)  
w.show\_all  
w.signal\_connect(‘destroy’) { Gtk.main\_quit }

Gtk.main

In particular, the ‘!accept\_tab’ at the end of the one closure makes  
only every other tab propogate into Gtk::TextView land.

HTH,

-pete

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys - and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![stoffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@stoffe](https://www.ruby-forum.com/u/stoffe)\
**Post date:** [January 24, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/catching-tab-presses-in-a-textview/83292/3 "2007-01-24T23:27:56Z")

</div>

On 1/24/07, Peter J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The following shows how to stop tabs, by connecting to the  
> key-press-event, and returning “TRUE”, which stops the event from  
> propogating further (meaning getting to Gtk::TextView at all):

Thanks a lot, that’s a really simple and nice solution!

> In particular, the ‘!accept\_tab’ at the end of the one closure makes  
> only every other tab propogate into Gtk::TextView land.

Actually, it prevents every other keystroke, but comparing the events  
keyval to Gdk::Keyval::GDK\_Tab works just right.

Again, thanks!

– Kristoffer

## – Kristoffer LundÃ©n âœ‰ removed\_email\_address@domain.invalid âœ‰ removed\_email\_address@domain.invalid [http://www.gamemaker.nu/](http://www.gamemaker.nu/) â˜Ž 0704 48 98 77

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys - and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
