Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

tk bindings and readonly?

by Elijah (Hermit)
on Dec 10, 2003 at 22:29 UTC ( [id://313903]=perlquestion: print w/replies, xml ) Need Help??

Elijah has asked for the wisdom of the Perl Monks concerning the following question:

I have been messing around with this for a while. I have noticed that in Tk::Text nthe author did not include a save function at all let alone a <Control-s> binding. So I am creating this binding myself in my program and I can get the save_vile sub to be called when I enter CTRL+s but it also enters a control character in the text field of my profram.

This will not do of course so I have looked into ways to get this character to not print to screen when entered. In Tk::Text they have a bindRdOnly sub that looks like it would do what I want it to do but every time I try and use it it cries about not having a package or an object fed to it. So naturally I try to give it one of my widget fields and this makes it happy (at least enough to run ) but then the control signal still appears and the file wont even save.

$t->bindRdOnly('<Control-s>', [\&save_file]);
That is my code I have tried. I have tried about 10 variations of this code and even have tried to re-create the recursive function of "sub ClassInit" in Tk::Text because this appears to be how the author of this mod used the bindRdOnly function. Of course that diod not work either.

Anyone have any ideas on how I can accomplish this?

Replies are listed 'Best First'.
Re: tk bindings and readonly?
by Roger (Parson) on Dec 10, 2003 at 22:55 UTC
    The bindRdOnly subroutine is not doing what you think it's doing. This subroutine is called during class initialization to setup default bindings, not to introduce new ones.

    You can try the following instead -
    $t->bind('Tk::Text', '<Control-s>', \&save_file);
      That worked great! thank you. It seems it is always the small things that are getting me these days.
      Just a note: converters method of doing this binding is specific to just that widget, while Rogers method can be used if one wants to affect that binding on ALL Tk::Text widgets. I think it is an important difference, ie, if you had 2 Text Widgets and only wanted that key binding on a specific widget in your main window... you should use converters. ++ to both of you though
Re: tk bindings and readonly?
by converter (Priest) on Dec 10, 2003 at 22:47 UTC

    Without seeing your code it's difficult to guess what you're doing wrong, but in a nutshell, you should bind a callback to the parent MainWindow, for example:

    use Tk; $mw = tkinit; $mw->Text->pack; $mw->bind('<Control-s>', sub { print "save something...\n" }); MainLoop;

    The parent widget gets the keyboard input and responds to it instead of passing the event to the Text widget. If this doesn't fix the problem or doesn't address the right problem, post a snippet of code that shows what you're trying to do.

      I made a mistake here. When the Text widget has focus, the C-s character is still inserted. After re-reading the docs, it looks like the best approach for an instance binding would be something like:

      use Tk; $mw = tkinit; $t1 = $mw->Text->pack; $t1->bind('<Control-s>', sub { print "foo\n\n"; $_[0]->break }); # the order of bindtags is: # class name (Tk::Text), window name, ancestral toplevel, "all" # this modifies the tag list so that the instance binding, # which includes a call to break(), has higher priority # than the class binding $t1->bindtags([($t1->bindtags)[1,0,2,3]]); MainLoop;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://313903]
Approved by Roger
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found