2

I'm including a scrolled text box widget in my main Tk window which I have set up to intercept any key presses as follows:

use Tk;
use Tk::ROText; # read-only version of Text widget

$mw = new Tk::MainWindow;
$textBox = $mw->Scrolled('ROText', -scrollbars =>'e')->grid();
$mw->bind('<Any-KeyPress>' => \&key_handler);

#Attempts to disable key bindings of text widget
#$textBox->bind('<Any-KeyPress>' => sub {});
#$textBox->bindtags(undef);

Tk::MainLoop;

sub key_handler {
    $count++;
    $textBox->insert('end', "key press #$count\n");
}

The Text widget has a bunch of default key bindings, among them is moving the cursor with the arrow keys. I want to disable this behavior, since I wish to execute some other actions based on the arrow keys.

I tried $textBox->bind('Any-KeyPress' => sub {}); and $textBox->bindtags(undef); based on suggested solutions I found online, but neither would disable the cursor movement inside the text box. I still get the undesired cursor movement in addition to the desired key_handler calls.

Are there any other suggestions as to how I can disable Text widget keyboard bindings?

Automaton
  • 143
  • 8
  • 1
    A [mcve] would be nice. – melpomene Jun 26 '18 at 04:45
  • Sorry, trying to figure out how to format stuff here, trying again.. – Automaton Jun 26 '18 at 16:51
  • @melpomene I've added a minimal example, thanks. Clicking somewhere in the text box will activate the cursor at that location, then arrow keys will move it around. – Automaton Jun 26 '18 at 17:05
  • 1
    Related: [Remove tkinter text default binding](https://stackoverflow.com/q/31326766/2173773) – Håkon Hægland Jun 26 '18 at 17:19
  • 1
    I found a workaround in calling `$textBox->SetCursor('end');` after the insert command. That way, the cursor stays at the end of the buffer and the scroll widget tracks new entries. – Automaton Jun 26 '18 at 17:32
  • Would like to add that I saw related Q&A for Python tkinter and TCL/Tk, but it wasn't obvious what the equivalent for Perl would be. – Automaton Jun 26 '18 at 19:42

0 Answers0