1 (изменено: teadrinker, 2009-02-28 16:50:30)

Тема: AHK: Отслеживание времени бездействия мыши

Скрипт выполняет нужную функцию по прошествии определённого времени неактивности мыши.

    CoordMode, Mouse, Screen
    SetTimer, MouseWatch, 1000
    return

MouseWatch:
    MouseGetPos, x, y
    if (x = x_old) && (y = y_old)
         counter++
    else counter =
    if counter = 5
         MyFunc(), counter := 0
    x_old := x, y_old := y
    return
    
~WheelDown::
~WheelUp::
~RButton::
~LButton::
    counter =
    return
    
MyFunc()
{
    MsgBox, 5 секунд бездействия мыши!
}

Esc:: ExitApp

Авторы скрипта teadrinker и YMP

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder

2

Re: AHK: Отслеживание времени бездействия мыши

Вариант с использованием хука на события мыши:

   PrevTime := A_TickCount
   OnExit, Exit
   hHookKeybd := DllCall("SetWindowsHookEx"
      , Int, WH_MOUSE_LL := 14
      , Int, RegisterCallback("LowLevelMouseProc", "Fast")
      , UInt, DllCall("GetModuleHandle", UInt, 0)
      , UInt, 0)
   Return
 
Exit:
   DllCall("UnhookWindowsHookEx", Uint, hHookKeybd)
   ExitApp
 
LowLevelMouseProc(nCode, wParam, lParam)
{
   global PrevTime
   Time := NumGet(lParam+0, 16)
   InactivityTime := Time - PrevTime
   if InactivityTime > 300
   {
      ToolTip % "Время бездействия мыши " InactivityTime " ms"
      PrevTime := Time
   }
   Return DllCall("CallNextHookEx", UInt, 0, Int, nCode, UInt, wParam, UInt, lParam)
}
 
Esc::ExitApp
Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder

3

Re: AHK: Отслеживание времени бездействия мыши

Вариант, поддерживающий AHK_L x64.

   PrevTime := A_TickCount
   OnExit, Exit
   hHookMouse := DllCall("SetWindowsHookEx" . (A_IsUnicode ? "W" : "A")
      , Int, WH_MOUSE_LL := 14
      , Int, RegisterCallback("LowLevelMouseProc", "Fast")
      , Ptr, DllCall("GetModuleHandle", UInt, 0, Ptr)
      , UInt, 0, Ptr)
   Return
 
Exit:
   DllCall("UnhookWindowsHookEx", UInt, hHookMouse)
   ExitApp
 
LowLevelMouseProc(nCode, wParam, lParam)
{
   global PrevTime
   Time := NumGet(lParam+0, 16, "UInt")
   InactivityTime := Time - PrevTime
   if InactivityTime > 300
   {
      ToolTip % "Время бездействия мыши " InactivityTime " ms"
      PrevTime := Time
   }
   Return DllCall("CallNextHookEx", Ptr, 0, Int, nCode, UInt, wParam, UInt, lParam)
}
 
Esc::ExitApp
Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder