Version 0 of Emulating Third Mouse Button Using Two Button Mouse

Updated 2003-08-13 13:17:03

adavis I am developing an application which requires a third mouse button; However, the two button mouse seems by far to be the most common variety. I've put together something which emulates a third mouse button by pressing both mouse buttons together. I've searched but haven't found an existing solution, though I guess there must be one, so I'm posting my version. It seems to work OK but I presume there is a neater solution...

I've decided to call the system button13 which generates the following virtual events:-

  • <<Button-1>> - Button one pressed.
  • <<Button-3>> - Button three pressed.
  • <<Button-13>> - Buttons one and three pressed together.

Buttons one and three must be pressed within 200ms of each other (This value can be set as a parameter).

 proc button13 {widget {ms 200}} {
    bind $widget <Button-1> "button13proc1 $widget 1 $ms"
    bind $widget <Button-3> "button13proc1 $widget 3 $ms"
 }

 proc button13proc1 {widget button ms} {
    global button13

    if {[info exists button13($widget)] && ! [string equal $button13($widget) $button]} {
       event generate $widget <<Button-13>>
       catch "unset button13($widget)"
       return
    }

    set button13($widget) $button

    after $ms "button13proc2 $widget $button"
 }

 proc button13proc2 {widget button} {
    global button13

    if {! [info exists button13($widget)]} {
       return
    }

    if {[info exists button13($widget)] && ! [string equal $button13($widget) $button]} {
       event generate $widget <<Button-13>>
    } else {
       event generate $widget <<Button-$button>>
    }

    catch "unset button13($widget)"
 }

 #===================#
 # Example of usage. #
 #===================#

 label .lab1 -text "Click on this label"

 button13 .lab1

 bind .lab1 <<Button-1>>  "puts Button-1"
 bind .lab1 <<Button-3>>  "puts Button-3"
 bind .lab1 <<Button-13>> "puts Button-13"

 pack .lab1