Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cause ButtonPress/Releas (i.e a click) on button from C

6 views
Skip to first unread message

Pattrick Hueper

unread,
May 11, 2002, 12:03:21 PM5/11/02
to
Hi,

i am writing a test tool for tk. Is there any way, that i can cause a
ButtonPress and ButtonRelease on a tk button, so i can cause a "click"
from c?

I tried this:

XEvent* pXButtonPressEvent = (XEvent*) Tcl_Alloc(sizeof(XEvent));
XEvent* pXButtonReleaseEvent = (XEvent*)
Tcl_Alloc(sizeof(XEvent));
pXButtonPressEvent->xbutton.type = ButtonPress;
pXButtonPressEvent->xbutton.serial =
Tk_Display(thisElement)->request;
pXButtonPressEvent->xbutton.send_event = False;
pXButtonPressEvent->xbutton.display = Tk_Display(thisElement);
pXButtonPressEvent->xbutton.window = Tk_WindowId(thisElement);
pXButtonPressEvent->xbutton.root =
Tk_WindowId(Tk_Parent(thisElement));
pXButtonPressEvent->xbutton.subwindow = 0;
pXButtonPressEvent->xbutton.time = time(NULL);
pXButtonPressEvent->xbutton.x=0;
pXButtonPressEvent->xbutton.y=0;
pXButtonPressEvent->xbutton.x_root=Tk_X(thisElement);
pXButtonPressEvent->xbutton.y_root=Tk_Y(thisElement);
pXButtonPressEvent->xbutton.state=0;
pXButtonPressEvent->xbutton.button=1;
pXButtonPressEvent->xbutton.same_screen=1;
Tk_QueueWindowEvent(pXButtonPressEvent, TCL_QUEUE_TAIL);
pXButtonReleaseEvent->xbutton.type = ButtonRelease;
pXButtonReleaseEvent->xbutton.serial =
Tk_Display(thisElement)->request;
pXButtonReleaseEvent->xbutton.send_event = False;
pXButtonReleaseEvent->xbutton.display = Tk_Display(thisElement);
pXButtonReleaseEvent->xbutton.window = Tk_WindowId(thisElement);
pXButtonReleaseEvent->xbutton.root =
Tk_WindowId(Tk_Parent(thisElement));
pXButtonReleaseEvent->xbutton.subwindow = 0;
pXButtonReleaseEvent->xbutton.time = time(NULL)+100;
pXButtonReleaseEvent->xbutton.x=0;
pXButtonReleaseEvent->xbutton.y=0;
pXButtonReleaseEvent->xbutton.x_root=Tk_X(thisElement);
pXButtonReleaseEvent->xbutton.y_root=Tk_Y(thisElement);
pXButtonReleaseEvent->xbutton.state=256;
pXButtonReleaseEvent->xbutton.button=1;
pXButtonReleaseEvent->xbutton.same_screen=1;
Tk_QueueWindowEvent(pXButtonReleaseEvent, TCL_QUEUE_TAIL);

and recorded events on the window with a eventHandler.

my eventHandler function is called and finds the two events, but
nothing happens in the tk gui, when i click the button "by hand" it
causes the right action!

has anybody any idea what i can do? am i doing something wrong?

Thanks for any help,

Pattrick

Petasis George

unread,
May 13, 2002, 1:43:45 AM5/13/02
to
Pattrick Hueper wrote:
>
> Hi,
>
> i am writing a test tool for tk. Is there any way, that i can cause a
> ButtonPress and ButtonRelease on a tk button, so i can cause a "click"
> from c?
>

Actually the answer is quite simple:

pack [button .b -text Text -command {puts %W}]
.b flash

The bad news is it is not working. Perhaps a bug?

If you insist going with X-events, why not use
event generate?


event generate .b <ButtonPress-1>
after 200 {event generate .b <ButtonRelease-1>}
update

You can even control the time the button starys pressed. Note that
if the delay is not present, nothing shows up. Perhaps this
is the reason button flash does not produce the desired
effect?

George

Pattrick Hueper

unread,
May 13, 2002, 7:39:45 AM5/13/02
to
Hi,

Petasis George <pet...@iit.demokritos.gr> wrote in message news:<3CDF5291...@iit.demokritos.gr>...


> event generate .b <ButtonPress-1>
> after 200 {event generate .b <ButtonRelease-1>}
> update
>

this works... kind of... i can see the Button go down and up, BUT...
the command associated with the button is still not executed, so it
gets me as far as my (admittedly far more complicated) c code!

Patty

Ken Jones

unread,
May 13, 2002, 11:19:26 AM5/13/02
to
"Pattrick Hueper" <pat...@gmx.net> wrote in message
news:d8f00cc1.02051...@posting.google.com...

A quick examination of the default Button bindings indicates that a button
needs to receive an <Enter>, <ButtonPress-1>, <ButtonRelease-1> sequence to
invoke its associated command. It looks as though it's also a good idea to
generate a <Leave> to clean things up afterwards (as that's the sequence of
events a Button would normally receive if a user were directly interacting
with the application).

In case you weren't aware of how to do so, you can examine the default
widget bindings in a couple of ways. One is by using the Tcl [bind] command
to introspect the bindings. Just give [bind] the widget class name as an
argument, and it returns a list of all events that have bindings for that
widget class. For example:

bind Button

To examine a binding action, execute [bind] with both the target (e.g., the
widget class) and the event specification. For example:

bind Button <Enter>

You'll notice that the default widget bindings call a variety of pre-defined
procedures. For example, in Tcl 8.3 the action for receiving an <Enter>
event on a Button is to execute tkButtonEnter, passing the name of the
button widget that received the event as an argument. You can use the [info
args] and [info body] commands to then introspect on these pre-defined
procedures.

Of course, an even easier way to examine all of the built-in bindings is to
browse through the library code that implements them all. These are simply
Tcl scripts that are automatically [source]d when a Tk application starts.
To find them, go to the directory where Tcl is installed on your system,
then go to the lib/tkX.X subdirectory (where X.X is the version of Tcl/Tk
you've got installed). The file tk.tcl gets [source]d automatically, and it
in turn [source]s most of the other files in that directory. Of note in this
situation is the button.tcl file, which implements the Button, Checkbutton,
and Radiobutton bindings and their actions.

- Ken Jones, President
Avia Training and Consulting
www.avia-training.com
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax


Bruce Hartweg

unread,
May 13, 2002, 12:50:37 PM5/13/02
to
Petasis George wrote:

> Pattrick Hueper wrote:
> >
> > Hi,
> >
> > i am writing a test tool for tk. Is there any way, that i can cause a
> > ButtonPress and ButtonRelease on a tk button, so i can cause a "click"
> > from c?
> >
>
> Actually the answer is quite simple:
>
> pack [button .b -text Text -command {puts %W}]
> .b flash
>
> The bad news is it is not working. Perhaps a bug?

not a bug, all flash does is flash the button visually, use:

.b invoke

to cause it to trigger the command callback

Bruce

Georgios Petasis

unread,
May 14, 2002, 2:25:25 AM5/14/02
to
> > Actually the answer is quite simple:
> >
> > pack [button .b -text Text -command {puts %W}]
> > .b flash
> >
> > The bad news is it is not working. Perhaps a bug?
>
> not a bug, all flash does is flash the button visually, use:
>
> .b invoke
>

Well, it is a bug, as .b flash does not visually
flashes the button for me (tk 8.4a5, solaris, windows).
I know that .b flash does not execute the button associated
command. Does .b flash gives visual feedback on your system?

George


Pattrick Hueper

unread,
May 14, 2002, 9:34:11 AM5/14/02
to
"Ken Jones" <k...@avia-training.com> wrote in message news:<2QQD8.1731$%64.88...@newssvr21.news.prodigy.com>...

Hi Ken,

thanks! that does it!

Patty

Bruce Hartweg

unread,
May 14, 2002, 11:16:08 AM5/14/02
to
Georgios Petasis wrote:

At first, no. Then careful reading of man page says flashing
is done be redisplaying widget with alternate normal/active
colors. On my windows box the default background AND
the default activebackground are the same (SystemButtonFace)
so the flashing did not *seem* to occur - by setting these values
such that they are different, the flashing can be observed.

Bruce

0 new messages