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

I have the following situation. I have made some buttons that do actions but now I also want to make some keybindings. The problem is, that in the keybindings I'll have the same code from the buttons,and to avoid duplicating it I'd like to know how to emulate button pushes ,if possible(so that I can emulate the button pushes in the keybindings I want to make). I'm pretty sure I was able to do that on win32api. I remember I would do enumwindow and then findwindow and sendmessage WM_COMMAND or something similar(only difference was,that there I was trying to emulate press button on programs that were not made by me,here I have this perl script that I should be able to control much better). Can I do that here too , but in a simpler way , because I already have the object(the button).

More Clearly: How can one send events to a certain object in Perl/Tk ?

I'd also like to ask if possible about getting to a certain object if one only has access to the mainwindow of Tk.

If one only has access to a Tk main window how would it be possible to access objects on the Tk main window ? Would it be a good ideea to make a global hash in wich one puts references to all objects created ?

Replies are listed 'Best First'.
Re: emulate Tk button push
by zentara (Cardinal) on Sep 19, 2007 at 09:52 UTC
    What you are looking for is $button->invoke
    $mw->bind('<Control-A>', [$buttonA => 'invoke']);

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      thanks , that is exactly what I needed :)

Re: emulate Tk button push
by mmmmtmmmm (Monk) on Sep 19, 2007 at 09:14 UTC
    You can use the -underline option to make the button have an underline under the character that you want for the keybinding. For instance if $button's -text => 'Yes' then you could say -underline=> 0 to underline the Y (the 0th character...)

    Then you could just map the keypress event to the same sub that is executed when the button is clicked by using:

    $mw->bind('<KeyPress-Y>' => \&button_sub);

    To make Y execute the same sub as the button...

    ---mmmmtmmmm

      I thought of that method but the problem is,I have a big table,and in certain columns there are buttons. Ammounting to at least 80 buttons,so if I would make keybindings like that , I would need 80 key-combinations. So I want to key bind some sub that would find the row I'm on and then send an similar-WM_COMMND(push event) to the specific button on that row.

        find the row I'm on

        how do you define "the row I'm on"? Are there other widgets in the row which might have focus? What else are you not telling us?