Newbie: Mouse events 
Author Message
 Newbie: Mouse events

Hi there,

I'm new to using TK so i would appreciate any help i can get. Please excuse
my "dumb" questions.

Anyways, I'm creating a simple drawing tool. Any tips on how i could
implement this:
When a person clicks the mouse in the drawing frame, the coordinates of that
point, in that frame, are stored as x1 and y1.

I'm not to sure on how to implement the mouse click and how to "read in" the
coordinates...Any help would be appreciated..

Thanks,
Pritesh



Thu, 05 Apr 2001 03:00:00 GMT  
 Newbie: Mouse events

Quote:

> Anyways, I'm creating a simple drawing tool. Any tips on how i could
> implement this:

> When a person clicks the mouse in the drawing frame, the coordinates
> of that point, in that frame, are stored as x1 and y1.

When the mouse is clicked, an XEvent is created that contains, among
other things, the x and y coordinates of the mouse click.  Here's the
best way I know to get them:

The Canvas widget has several methods which will help you get these
coordinates.  The following code will put the canvas coordinates
(different from the screen coordinates!) into $x1 and $y1.  $pad is
the Canvas widget, $frame is a Frame containing $pad, and $top is a
MainWindow.

  $x1 = $pad->pointerx - $top->x - $frame->x
  $y1 = $pad->pointery - $top->y - $frame->y

pointerx returns the screen coordinates of the last mouse click.
$top->x is the screen coordinate of the MainWindow, and $frame->x is
the screen coordinate of the Frame containing the Canvas.  So what
this code does is it takes the screen coordinates of the mouse click,
and subtracts away the screen coordinates of the top-left corner of
the Canvas.  This should give you the canvas coordinates.

Assuming you want to do some other stuff as well, you'll need to bind
a function call to the mouse click.  So create a function called
onClick or something, and put this right after you declare the Canvas:

  $canvas->bind("<1>", \&onClick);

The <1> means 'whenever someone clicks button 1, execute the function
onClick.' There are many other events you can bind to, including any
button with any combination of modifiers (i.e. "<Shift-Control-2>"),
and just about any keypress (like "<Return>" or "<Control-q>") too.

Hope this helps.

- ben

--

Hardware Design Engineer, Compaq Computer Corporation



Fri, 06 Apr 2001 03:00:00 GMT  
 Newbie: Mouse events
Oh wow! Thanks Ben!

I'll try implementing it your way and see how it goes!

thanks again,
Pritesh

Quote:


>> Anyways, I'm creating a simple drawing tool. Any tips on how i could
>> implement this:

>> When a person clicks the mouse in the drawing frame, the coordinates
>> of that point, in that frame, are stored as x1 and y1.

>When the mouse is clicked, an XEvent is created that contains, among
>other things, the x and y coordinates of the mouse click.  Here's the
>best way I know to get them:

>The Canvas widget has several methods which will help you get these
>coordinates.  The following code will put the canvas coordinates
>(different from the screen coordinates!) into $x1 and $y1.  $pad is
>the Canvas widget, $frame is a Frame containing $pad, and $top is a
>MainWindow.

>  $x1 = $pad->pointerx - $top->x - $frame->x
>  $y1 = $pad->pointery - $top->y - $frame->y

>pointerx returns the screen coordinates of the last mouse click.
>$top->x is the screen coordinate of the MainWindow, and $frame->x is
>the screen coordinate of the Frame containing the Canvas.  So what
>this code does is it takes the screen coordinates of the mouse click,
>and subtracts away the screen coordinates of the top-left corner of
>the Canvas.  This should give you the canvas coordinates.

>Assuming you want to do some other stuff as well, you'll need to bind
>a function call to the mouse click.  So create a function called
>onClick or something, and put this right after you declare the Canvas:

>  $canvas->bind("<1>", \&onClick);

>The <1> means 'whenever someone clicks button 1, execute the function
>onClick.' There are many other events you can bind to, including any
>button with any combination of modifiers (i.e. "<Shift-Control-2>"),
>and just about any keypress (like "<Return>" or "<Control-q>") too.

>Hope this helps.

>- ben

>--

>Hardware Design Engineer, Compaq Computer Corporation



Sat, 07 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Binding mouse event to a canvas

2. Problem with mouse event to Tree

3. Mouse events : release button

4. Responding to mouse/keyboard events?

5. detecting mouse events with canvas widget

6. help binding mouse events to canvas

7. Newbie: Paste selection in Hlist using mouse

8. newbie : how 2 get mouse coords ?

9. Pigheadedness (was Re: Newbie; Perl/Tk Event models)

10. Newbie question on creating a delay event

11. Events - newbie

12. Events .. (newbie)

 

 
Powered by phpBB® Forum Software