Version 7 of Emulating Third Mouse Button Using Two Button Mouse

Updated 2003-08-13 23:04:07

[ 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

While this is clearly useful, I disagree that the majority of mice are two-button. 80% of mice sold have a mouse wheel (which doubles as a middle button), and I'm sure a good deal of the others have a standard button for the third. -DW


GPS: This seems simpler:

 bind . <Button-1><Button-3> {puts 2}
 bind . <Button-3><Button-1> {puts 2}

In practice I would proc'ify it, but you probably get the idea. It would need a single variable to record the state, because if 1 and 3 are activated and then released, and 1 is pressed again the <Button-3><Button-1> binding will be invoked.