Version 2 of Moving the mouse

Updated 2004-01-29 13:28:43

ulis, 2004-01-29. No, Tcl nor Tk can move your mouse or your hand. But Tk (with the help of Tcl) can move the mouse cursor on your screen. The trick is to use the -warp option of the event generate command. (This trick comes from Kroc).

This is covered in warp

A little script to illustrate this:

  pack [button .b -text Ok -width 4 -command exit]
  bind . <Key> { set ::stop 1 }
  update
  set stop 0
  set x -20; set y -10; set incrx 1; set incry 0.5
  proc move {} \
  {
    if {$::stop} { return }
    set ::x [expr {$::x + $::incrx}]
    set ::y [expr {$::y + $::incry}]
    if {$::x < 20} \
    { 
      event generate .b <Motion> -warp 1 -x $::x -y $::y
      after 100 move 
    } \
    else \
    { 
      event generate .b <ButtonPress-1>
      after 100 event generate .b <ButtonRelease-1>
    }
  }
  focus -f .
  raise .
  move

Category ?