Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
[Corion]: "Unmatched ) in regex; marked by <-- HERE in m/(&#9583;&# 9633;)&#9583;&# 65077; &#9531;&#9473;&# 9531;) <-- HERE / at -e line 1." :)
[Corion]: Meh - my "flip the table" string doesn't render here ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-07-17 16:06 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (372 votes). Check out past polls.