Un1
Un1

Reputation: 4132

Autohotkey sends default button action if the cursor is over an inactive window

I wrote the following code in order to remap the 4-th and 5-th mouse buttons to scroll up & scroll down:

Code:

XButton2::
While GetKeyState("XButton2","P")
{
Send {WheelUp 1}
Sleep 120
}
return


XButton1::
While GetKeyState("XButton1","P")
{
Send {WheelDown 1}
Sleep 120
}
return

The problem:

If the cursor is over an active window, everything works.

But, when I click the XButton1 (4-th mouse button) or XButton2 (5-th mouse button) with the cursor over inactive window, sometimes it sends the default action into that window.

For example, if the cursor is over Chrome (inactive) and the active window is Task manager, it will send the default "page back" action instead of specified in the script "scroll down", but when I click on Chrome and make it active, everything works

Question:

Is there a way to just disable the default action? So that if you're not holding the button (While GetKeyState("XButton1","P")) it just does nothing?

Upvotes: 0

Views: 705

Answers (1)

Roman
Roman

Reputation: 221

Try this. It makes it so hotkeys work only if certain window is active and return nothing if they are not. I don't have extra mouse buttons so I tested it with regular keyboard keys and it worked.

loop
 {
 sleep,50
 #if WinActive("ahk_exe chrome.exe") or WinActive("ahk_exe explorer.exe") ;;hotkeys work only if chrome.exe or explorer.exe is active
  {
  XButton2::
  While GetKeyState("XButton2","P")
   {
   Send {WheelUp 1}
   Sleep 120
   }
  XButton1::
  While GetKeyState("XButton1","P")
   {
   Send {WheelDown 1}
   Sleep 120
   }
  }
 #if ! WinActive("ahk_exe chrome.exe") ;;if chrome isn't active XButton1 and XButton2 return nothing
  {
  XButton1::return
  XButton2::return
  }
 }

Upvotes: 1

Related Questions