home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / tcl / 1205 < prev    next >
Encoding:
Internet Message Format  |  1992-08-17  |  2.8 KB

  1. Path: sparky!uunet!sun-barr!ames!agate!sprite.Berkeley.EDU!ouster
  2. From: ouster@sprite.Berkeley.EDU (John Ousterhout)
  3. Newsgroups: comp.lang.tcl
  4. Subject: Re: Question on binding
  5. Date: 17 Aug 1992 15:32:12 GMT
  6. Organization: U.C. Berkeley Sprite Project
  7. Lines: 49
  8. Distribution: world
  9. Message-ID: <16ogpsINNll1@agate.berkeley.edu>
  10. References: <1992Aug14.201600.26716@bnr.ca>
  11. NNTP-Posting-Host: tyranny.berkeley.edu
  12. Keywords: binding
  13.  
  14. Subject: Re: Question on binding
  15. Keywords: binding
  16.  
  17. In article <1992Aug14.201600.26716@bnr.ca>, tewei@bnr.ca (Te-Wei Sun) writes:
  18. |> 
  19. |> Hi,
  20. |> 
  21. |> I wrote a short script in tk trying to bind several mouse events for
  22. |> canvas. The goal I am trying to achieve here is as follows:
  23. |> 
  24. |> If I click button 1 once on the canvas, I would like to see the message
  25. |> "b1 pressed once".
  26. |> 
  27. |> If I double click button 1 on the canvas, I would like to see ONLY the
  28. |> message "b1 pressed twice", not "b1 pressed once" and "b1 pressed twice",
  29. |> The same applied for triple click.
  30. |> 
  31. |> If I click button 1 on the rectangle widget, I would like to see ONLY the
  32. |> message "b1 pressed once on rectangle", not "b1 pressed once" and "b1 pressed once on rectangle".
  33. |> 
  34. |> If I double click button 1 on the rectangle widget, I would like to see ONLY
  35. |> the message "b1 pressed twice on rectangle" and so on for triple click.
  36. |> 
  37. |> 
  38. |> Is there any way I can achieve the way I would like to have my bindings?
  39. |> 
  40.  
  41. There are a couple of things to realize here.  First bindings on widgets
  42. (e.g. "bind .c <1> ...") are independent of bindings on individual items or
  43. tags within canvases (e.g. ".c bind 2 <1> ...").  If you set up a binding on
  44. a rectangle within a canvas and also a separate binding on the canvas, both
  45. will trigger when you click with the mouse in the rectangle.  It sounds like
  46. you want a way to distinguish between a click over an item and a click when
  47. the mouse isn't over any item.  You can distinguish these by checking to see
  48. if there is a "current" item.  If the command ".c find withtag current"
  49. returns an empty string then the mouse isn't over an item.  You can use this
  50. in the binding for the whole canvas to do nothing when there is a current
  51. item.
  52.  
  53. Second, you will always get a single-click event before a double-click event.
  54. The reason for this is that at the time of the single-click event there's no
  55. way to know whether or not a second click is going to follow, so Tk always
  56. issues the single-click event.  If you don't want to process the single-click
  57. in cases where there is (eventually) a double click, then use the "after"
  58. command in your single-click event handler to delay the real work you want
  59. to do for a while.  If a double-click event follows in the interim, then
  60. you can cancel the real work.  This has the disadvantage of introducing a
  61. delay in processing single clicks, but I don't see any alternative, given
  62. your desires.
  63.