Jump to content

Repeat an action as long as mouse hovering over tray icon


 Share

Recommended Posts

I want to do an action every couple of seconds as long as the mouse is hovering over the tray icon. I'm not sure how to do that, because the standard tray event (as I understand it) only detects the mouse moving over the icon. It doesn't fire continually or set a checkable flag for as long as the mouse is there.

The actual aim of the code is for a tray tool that displays status information - the idea is to update/rewrite the tray tooltip every few seconds while the icon is hovered, so that if the mouse is left over the icon for a long-ish time, the tooltip data gets updated every few seconds and the tooltip doesn't vanish until the mouse actually stops hovering.

Any help welcomed!

Edited by Stilez
Link to comment
Share on other sites

Look at TraySetOnEvent ( $TRAY_EVENT_MOUSEOVER, "function" )

I did, but I figured as an event, it fires once on the mouse moving over. There's no sign of a corresponding $TRAY_EVENT_MOUSEOFF to detect the end of mousing over.
Link to comment
Share on other sites

I did, but I figured as an event, it fires once on the mouse moving over. There's no sign of a corresponding $TRAY_EVENT_MOUSEOFF to detect the end of mousing over.

An other way

#include <Constants.au3>

Opt ( "TrayMenuMode", 1 )   
TraySetState()
Global $_MousePosOld[2]

While 1
    $_MousePos = MouseGetPos ( )
    If $_MousePos[0] <> $_MousePosOld[0] Or $_MousePos[1] <> $_MousePosOld[1] Then
        $_Msg = TrayGetMsg ( )
        Switch $_Msg
            Case $TRAY_EVENT_MOUSEOVER
                ConsoleWrite ( "+->-- MOUSE OVER the tray" & @Crlf )
            Case Else
                ConsoleWrite ( "!->-- MOUSE Not OVER the tray" & @Crlf )
        EndSwitch
        $_MousePosOld[0] = $_MousePos[0]
        $_MousePosOld[1] = $_MousePos[1]
    EndIf
    Sleep ( 10 )
WEnd

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

An other way

Thanks :mellow:

Unfortunately that didn't work quite as I'd hoped. The (TrayTool|Tool)tip itself is the problem.

It looks like Tray Tooltip doesn't work like Traytip - rewriting it doesn't update the displayed text or reset the timeout. It still displays the same value as when first shown and vanishes after a few seconds. So TraySetToolTip() is out unless there's better control available via DLL calls.

Tooltip() looks more promising, but I'm having a different problem with it - it's somehow preventing detection of the "move off" state. (Also the traytooltip is still displayed and I can't work out how to stop it, and the tooltip no longer positions next to its tray icon).

You can see the problem for yourself by running the code below, with $USE_TOOLTIP set to 0 or 1.

Setting $USE_TOOLTIP = 0 posts the current "hover" status to the console as your code did. Setting $USE_TOOLTIP = 1 posts the status to console and also writes (or updates) a tooltip using a random number instead of "updated data". With $USE_TOOLTIP = 0 everything's fine. But when $USE_TOOLTIP = 1, the act of setting the tooltip somehow seems to cause the "mouse move away" detection to fail, so it never changes $over_icon back to 0 and therefore never turns off the tooltip.

Any ideas why?

#include <Constants.au3>

Global $_MousePosOld[2]

$over_icon = False
$USE_TOOLTIP = 1


While 1
    $_MousePos = MouseGetPos ( )
    If $_MousePos[0] <> $_MousePosOld[0] Or $_MousePos[1] <> $_MousePosOld[1] Then
        $_Msg = TrayGetMsg ( )
        Switch $_Msg
            Case $TRAY_EVENT_MOUSEOVER
                $over_icon = True
            Case Else
                $over_icon = False
        EndSwitch
        $_MousePosOld[0] = $_MousePos[0]
        $_MousePosOld[1] = $_MousePos[1]
    EndIf
    
    If $USE_TOOLTIP Then
        If $over_icon then 
            ToolTip("some info" & @CRLF & random(1,1000,1),5000,5000,"TITLE",0,4)
        Else
            Tooltip("")
        EndIf
    EndIf
    
    ConsoleWrite("$over_tray = " & $over_icon & @CRLF)
    
    Sleep ( 10 )

WEnd
Edited by Stilez
Link to comment
Share on other sites

And with TrayTip instead of ToolTip ?

#include <Constants.au3>

Global $_MousePosOld[2]
$over_icon = False
$USE_TOOLTIP = 1
Global $_TrayTip=0

While 1
    $_MousePos = MouseGetPos ( )
    If $_MousePos[0] <> $_MousePosOld[0] Or $_MousePos[1] <> $_MousePosOld[1] Then
        $_Msg = TrayGetMsg ( )
        Switch $_Msg
            Case $TRAY_EVENT_MOUSEOVER
                $over_icon = True
            Case Else
                $over_icon = False
        EndSwitch
        $_MousePosOld[0] = $_MousePos[0]
        $_MousePosOld[1] = $_MousePos[1]
    EndIf

    If $USE_TOOLTIP Then
        If $over_icon And $_TrayTip = 0 then
            $_TrayTip = 1
            TrayTip ( "some info", random(1,1000,1) & @CRLF & "has been copied to the clipboard", 5, 1 )
            AdlibRegister ( '_TrayTipClose', 3000 )
        EndIf
    EndIf
    ConsoleWrite("$over_tray = " & $over_icon & @CRLF)
    Sleep ( 10 )
WEnd

Func _TrayTipClose ( )
    TrayTip ( '', '', 0 )
    AdlibUnRegister ( '_TrayTipClose' )
    $_TrayTip=0
EndFunc ;==> _TrayTipClose ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Try this

#include <Constants.au3>

Global $_MousePosOld[2]
$over_icon = False
$USE_TOOLTIP = 1
Global $_ToolTip=0

While 1
    $_MousePos = MouseGetPos ( )
    If $_MousePos[0] <> $_MousePosOld[0] Or $_MousePos[1] <> $_MousePosOld[1] Then
        $_Msg = TrayGetMsg ( )
        Switch $_Msg
            Case $TRAY_EVENT_MOUSEOVER
                $over_icon = True
            Case Else
                $over_icon = False
        EndSwitch
        $_MousePosOld[0] = $_MousePos[0]
        $_MousePosOld[1] = $_MousePos[1]
    EndIf

    If $USE_TOOLTIP Then
        If $over_icon And $_ToolTip = 0 then
            $_ToolTip = 1
            ToolTip("some info" & @CRLF & random(1,1000,1),5000,5000,"TITLE",0,4)
            AdlibRegister ( '_ToolTipClose', 3000 )
        EndIf
    EndIf
    ConsoleWrite("$over_tray = " & $over_icon & @CRLF)
    Sleep ( 10 )
WEnd

Func _ToolTipClose ( )
    ToolTip ( "" )
    AdlibUnRegister ( '_ToolTipClose' )
    $_ToolTip=0
EndFunc ;==> _ToolTipClose ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...