Version 3 of widget:repeaterButton

Updated 2002-05-09 23:27:14

GPS - Sun Mar 10, 2002: I looked at some code to a button that repeated an action and found it to be overly complex, so that inspired me to whip up this concise little tool.


  #!/bin/wish8.3
  #updated Thu May 9, 2002
  #eval button $win $args was changed to
  #eval button [concat $win $args]

  proc bind:copyClass {class newClass} {
    set bindingList [bind $class]
    #puts $bindingList

    foreach binding $bindingList {
      bind $newClass $binding [bind $class $binding]
    }
  }

  proc widget:repeaterButton:ButtonPress-1 {win} {
    eval [$win cget -command]
    if {$::buttonPressed == 1} {
      $win configure -relief sunken
      after 20 "widget:repeaterButton:ButtonPress-1 $win"
    }
  }

  proc widget:repeaterButton:ButtonRelease-1 {win} {
    $win configure -relief raised
  }

  proc widget:repeaterButton {win args} {
    bind:copyClass Button RepeaterButton

    bind RepeaterButton <ButtonPress-1> "set ::buttonPressed 1; \
      widget:repeaterButton:ButtonPress-1 %W"
    bind RepeaterButton <ButtonRelease-1> "set ::buttonPressed 0; \
      widget:repeaterButton:ButtonRelease-1 %W"

    eval button [concat $win $args]

    bindtags $win "RepeaterButton all"
    return $win
  }


  #test code
  catch {console show}
  pack [widget:repeaterButton .rb -text "Say Hello" -command "puts HELLO"]

It's nice, but it repeats too fast, I think. Is there a way to slow it down?

AK - What happens when you increase the timeout of the after command ?

GPS: I'm glad you like it. Increasing the after timeout as AK suggested will fix the issue. Also, note the update to it above which uses concat to fix an issue with passing some arguments.