Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Tk::Canvas' response to keystrokes

by TomKane (Beadle)
on Feb 08, 2009 at 12:18 UTC ( #742251=perlquestion: print w/replies, xml ) Need Help??
TomKane has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create an equation editor. Doing this is something that won't work with a text widget as it requires me to have multiple baselines to accommodate sub- and superscripts, as well as having multiple fonts.

I have a small example just to test how to use canvas directly as a text editor. I can handle taking in keystrokes and adding them to the text on the canvas in a text item. But I'm having a problem handling the keyboard arrow keys. Whenever I press the left arrow, the keystroke binding works correctly, but the entire canvas shifts to the right.

I'm hopeful that there will be some way to lock the canvas into place so it doesn't move, and I've looked at a number of ways at doing that, but nothing has worked for me so far. Note that it makes no difference if I bind the left arrow separately, or even if I bind the left arrow to the main window directly.

Are there any suggestions?

use strict; use warnings; use Tk; my $mw = MainWindow->new ; my $canvas ; my $text ; my $text_var = 'Canvas example' ; my $text_len = length( $text_var ) ; my $cur_pos = $text_len ; my $width = 200 ; my $height = 100 ; $mw->title( 'Canvas Example' ) ; $canvas = $mw->Canvas( -background => 'white', -width => $width, -height => $height, -scrollregion => [ 0, 0, 500, 500 ], ) ; $canvas->CanvasFocus() ; $text = $canvas->createText( 100, 90, -text => $text_var, -tags => 'title', ) ; $canvas->focus( $text ) ; $canvas->icursor( $text, $cur_pos ) ; $canvas->bind( 'title', '<KeyRelease>' => \&updater ) ; my $exit = $mw->Button( -text => 'Exit', -command => [$mw => 'destroy'], ) ; $canvas->pack; $exit->pack; ######### MainLoop; ######### #function to manipulate text in the field or to move cursor around sub updater { my ( $k ) = @_ ; my $e = $k->XEvent ; #get event object my $k_val = $e->N ; #get decimal equivalent my $k_str = $e->K ; #get ascii equivalent #print "updater $k_val\n" ; #should move the cursor to left in text if( $k_str eq 'Left' ) { $cur_pos = $cur_pos > 0 ? $cur_pos - 1 : 0 ; } elsif( $k_str eq 'Right' ) { $cur_pos = $cur_pos < length( $text_var ) ? $cur_pos + 1 : len +gth( $text_var ) ; } else{ #append new char to the text $text_var .= $k_str ; $text_len = length( $text_var ) ; $cur_pos++ ; #show updated text $canvas->itemconfigure( -text => $text_var, -tags => 'title', ) ; } $canvas->icursor( $text, $cur_pos ) ; }

Replies are listed 'Best First'.
Re: Tk::Canvas' response to keystrokes
by jethro (Monsignor) on Feb 08, 2009 at 14:13 UTC

    You made your canvas scrollable, i.e. the canvas is bigger as the viewable area displayed. I tried your script and all cursor keys moved the canvas, not only the left-arrow key. Not sure if you meant that or your operation system (which seems to be windows) or something else mucks about with the other cursor keys

    By far the easiest solution to your problem: Make the viewable area the same size as the canvas.

      Thanks. That did the trick. My addition of -scrollregion was actually a last minute stab and it had some effect in the left direction but not for moving the right arrow. I had really meant to leave it out before putting my example up on the forum.

      Anyway, when I changed the program line, thus:

      -scrollregion => [ 0, 0, $width, $height ],

      everything worked just as advertised. Thanks again. Tom

Re: Tk::Canvas' response to keystrokes
by zentara (Archbishop) on Feb 08, 2009 at 19:34 UTC
    Hi, just mentioning that you might like the Gtk2::TextView widget for math symbol equations. It supports superscript, subscript, scaled images( integral sign etc) , etc Here is a sample (with more tag assortments than you probably need)

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      I just got around to looking closely at your response (I had been so busy with the fix that the other answer gave me that I didn't think so deeply about it at the time), and want to thank you again. That ability to format math equations is definitely on my longer term horizon. I hope to use it before too long. Tom

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://742251]
Front-paged by Arunbear
NodeReaper writes the same droll phrase again

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2017-01-17 21:53 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (158 votes). Check out past polls.