Making bright regions less bright with twapi and tk

Undazzler (windows)

Having problems with dazzling windows I decided to write a script to "tune" out this problem. Here we have 3 hotkeys : F1 F2 and crtl+q. F1 cover/uncover the window/region with a transparent square. F2 covers/uncover a black rectangle over the window/region. ctrl+q quits the script. It is a "proof of concept" script, -Pelle Otterholm

foreach package {twapi Tk} { package require $package }

proc create_hook {combo callback} { set ::hook($combo) [twapi::register_hotkey $combo $callback] }

proc destroy_hook {combo} {
    if {[info exists ::hook($combo)]} {
        twapi::unregister_hotkey $::hook($combo)
        unset ::hook($combo)
        return 1
    }
    return 0
}

proc destroy_all_hooks {} {
    foreach combo [array names ::hook] {
        destroy_hook $combo
    }
}

proc hwin_at_point {} {
    foreach {x y} [twapi::get_mouse_location] break
    return [twapi::get_window_at_location $x $y]
}

proc alpha {{value 65}} {
    if { $value >= 0 && $value <= 100 } {
        set ::alpha $value
        wm attributes . -alpha [expr {1.0 * $value / 100} ]
    }
}

proc get_geometry {} {
    foreach {x1 y1} [twapi::get_mouse_location] break
    set win [hwin_at_point]
      foreach {x1 y1 x2 y2} [twapi::get_window_coordinates $win] break
    set width [expr $x2 - $x1]
    set height [expr $y2 - $y1]
    set ::geometry ${width}x$height+$x1+$y1
}

proc black_out {} {
    if {$::blackout eq 0} {
        alpha 100
        set ::blackout 1
    } else {
        alpha
        set ::blackout 0
    }
}

# wm attributes . -alpha 0.70 -topmost 1
proc cover_window {} {
    if {$::visible eq 0} {
        get_geometry
        wm deiconify .
        alpha 65
        wm geometry . $::geometry
        set ::visible 1
    } else {
        wm withdraw .
        set ::visible 0
    }
}

proc initialize {} {
    set ::visible 0
    set ::blackout 0
    . configure -bg black
    wm overrideredirect . 1
    wm attributes . -topmost 1
    wm withdraw .
}

initialize
create_hook F1 { cover_window }
create_hook F2 { black_out }
create_hook Ctrl-q { destroy_all_hooks ; exit }
vwait forever