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

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?

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
}
}

Related

AHK v2 mouse clicking loop

global toggleVar := false
>^j::
{
global toggleVar :=! toggleVar
}
if (WinActive("Warframe") and toggleVar = true)
{
while (GetKeyState("LButton"))
{
Click
Sleep 250
}
}
I am absolute noob in autohotkey, installed it today, also got scared by a bunch of viruses. Anyways, I wanted to have a toggle variable and when I'm in a game I want to hold down LMB and it will continue clicking for me. But it does nothing and I have no idea why.
#HotIf WinActive("Warframe")
>^j::
{
static toggle := 0
HotIf 'WinActive("Warframe")' ; change the hotkey context for Hotkey function
Hotkey "LButton", SpamClick, (toggle := !toggle) ? "On" : "Off" ; enable or disable lbutton hotkey
SpamClick(ThisHotkey) {
while GetKeyState("LButton", "P") {
Click
Sleep 250
}
}
}
#HotIf
Apparently this is how it's supposed to be done, it worked for me. Thanks to "plankoe" from reddit.

Automatically detect if a certain window becomes active?

I mean detect it without pressing a hotkey that will call the lines of code that will check it if said window is active
Below example works fine but pressing 4 is required.
4::
if WinActive("*Untitled - Notepad ahk_exe notepad.exe")
{
MsgBox, Found Notepad
}
else
{
MsgBox, Did Not Find Notepad
}
Return
I have also tried this, but it just spams me with MsgBoxes.
Loop, 50
{
if WinActive("ahk_class #32770")
{
MsgBox, Found Notepad
}
else
{
MsgBox, Did Not Find Notepad
}
}
Thanks.

how can i create an equivalent "$" effect on mouseclick events in AHK?

I'm trying to create an AHK script where the right mouse click is intercepted, similar to a $ call would work with a keyboard press. This is the code I've tried but the right click still is sent to the program.
$capslock::
send {u down}
var = 0
return
$capslock up::
send {u up}
var = 1
return
$rbutton::
if(var) {
;
} else
sendinput, {s}
return
Any help would be appreciated!

Autohotkey: Ctrl + 1 mapping

I want to remap mouse click to a button depend on a state of Numlock. If Numlock is 'ON' then I want the normal behavior else map to mouse click. The script I created below works fine but I have trouble remapping CTRL+1.
SHIFT+1 will result in '!' so it is simple to do but I don't know what's the behavior for CTRL+1 is. The current script has the dummy "Send, {^1}" but that is not the real behavior.
I appreciate any suggestion.
*The Keywait is for simulating holding the mouse button and the script is enable/disabled base on Numlock state.
$1::
if GetKeyState("NumLock","T")
Send, {1}
else
{
Click down,
KeyWait 1
Click up
}
Return
$+1::
if GetKeyState("NumLock","T")
Send, {!}
else
{
Click down,
KeyWait 1
Click up
}
Return
$^1::
if GetKeyState("NumLock","T")
Send, {^1}
else
{
Click down,
KeyWait 1
Click up
}
Return
The following should do what you've described, with some cleanup to the code itself. I also recommend reading up on the various Send modes, as SendInput is superior in just about every way possible.
$1::
if GetKeyState("NumLock","T")
SendInput, 1
else
{
Click down
KeyWait 1
Click up
}
Return
$+1::
if GetKeyState("NumLock","T")
SendInput, {!}
else
{
Click down
KeyWait 1
Click up
}
Return
$^1::
if GetKeyState("NumLock","T")
SendInput, ^1
else
{
Click down
KeyWait 1
Click up
}
Return

Autohotkey: Open windows explorer and wait till active window before proceeding

Really struggling with this.
I need to simply open a windows explorer window at a specified domain, wait until it is active then proceed. This is what I have so far:
#::
{
WinGet, old_active, ID, A
Run, explore C:\Users\Nathan\Documents\Test FDA
loop{
WinGet, new_active, ID, A
if(ahk_id %new_active% != ahk_id %old_active%)
{
WinMaximize, A
break
}
}
return
}
EDIT SOLVED ?>>>
DIDNT KNOW WINDOW SPY EXISTED CAME WITH IT :(((
Long time wasted, this simply works.
[::
{
Run explore C:\Users\Nathan\Documents\Test FDA
WinWaitActive Test FDA
WinMaximize A
return
}
how about this?:
"
F12::
WINDOWEXPLORER:
WinWaitActive, Windows Explorer,, 0.01
if ErrorLevel
{
Goto WINDOWEXPLORER
}
else
{
; SoundBeep 4500, 30
Return
}
"
I think you are looking for the WinWaitActive function. From the documentation:
Waits until the specified window is active
Put it after the line with Run...:
WinWaitActive, WinTitle
You will need to replace WinTitle with the title of the window that comes up after the Run command. There are other options available, such as duration to wait, titles to exclude, etc.
See the documentation for more details.