Version 3 of hidden cursor

Updated 2003-07-29 13:57:52

Sometimes you need to hide the cursor. E.g., a presentation application in which the cursor only shows up when you move it to highlight something, then disappears. From reading the discussion on c.l.t, the following should work in Linux:

 proc show_cursor {} { .c conf -cursor "" }
 proc hide_cursor {} { .c conf -cursor { gumby blue red } }

On my Debian Box, this shows Gumby, so not a success. It is even less successful under Windows:

 Error: bad cursor spec " gumby blue red "

The documented "no" cursor doesn't work on Linux or Windows either.


You can instead use a custom cursor which is invisible.

In linux, save the following as none.cur:

 #define none_width 1
 #define none_height 1
 #define none_x_hot 0
 #define none_y_hot 0
 static unsigned char none_bits[] = {
    0x00};

Then use:

 proc show_cursor {} { .c conf -cursor "" }
 proc hide_cursor {} { .c conf -cursor "@path_to_cursor/none.cur black" }

For windows, you will have to find one for yourself on the net (e.g., a google search for invisible.cur), or use the tools suggested in custom cursors to build one. The cursor file must be on a system path, not in a VFS path.

 proc show_cursor {} { .c conf -cursor "" }
 proc hide_cursor {} { .c conf -cursor "@path_to_cursor/invisible.cur" }

On windows, user32.dll has the function ShowCursor(bool) which allows you to hide/show the cursor for all windows, but there is no easy way to call it unless you are already building a compiled application.


You can also simulate a hidden cursor with pointer warping, moving the cursor to the corner of the screen on hide, and restoring its position on show. This works reasonably well if the canvas covers the entire screen, as shown in the following example:

 # full screen canvas
 pack [canvas .c -width [winfo screenwidth .] -height [winfo screenheight .]]
 wm overrideredirect . 1

 set hidden {} ;# last cursor coordinates
 set hiding 0 ;# hiding, so don't try to unhide
 proc show_cursor {} {
     # suppress the motion event that occurs when warping
     if $::hiding return
     if [llength $::hidden] {
         foreach {x y} $::hidden break
         set ::hidden {}
         event generate {} <Motion> -warp 1 -x $x -y $y
         # grab release .c
     }
 }
 proc hide_cursor {} {
     if [llength $::hidden] return
     set ::hidden [winfo pointerxy .c]
     set ::hiding 1
     # almost to the corner --- if you go to the corner, 
     # you will miss half the motion events
     event generate {} <Motion> -warp 1 -x [expr {[winfo screenwidth .]-1}] -y 1
     update
     set ::hiding 0
     # grab set -global .c
     ## Global grab safety valve
     ## after cancel { grab release .c }
     ## after 5000 { grab release .c }
 }

 # escape by clicking mouse
 bind all <1> exit

 # make the cursor appear when the mouse moves
 bind .c <Motion> {
     show_cursor
     # reschedule hide
     after cancel hide_cursor
     after 1000 hide_cursor
 }

 # start with the cursor hidden
 after 1000 hide_cursor

For a canvas which does not cover the entire screen, uncomment the grab set -global and grab release lines.