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

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!

Related

combining hotkeys

I'm trying to make an autoclicker in autohotkey but can't get it to work.
I have some knowledge about autohotkey but it is not very much:
<^LButton::
Loop
{
SetMouseDelay 0.001
Click
If(GetKeyState("LButton","P")=0)
Break
}
It works with <^LButton as hotkey, but not <^nLButton.
Therefore I need help with hotkey-combinations.
I get the errorcode:
Line Text: <^nLButtonSuspend
Error: This line does not contain a recognized action.
If you want to combinate Three keys as a Hotkey.
Click the Keys : [Ctrl] + [n] + [Lbutton] = Do a Action.
You can Try this:
example1.ahk
;#notrayicon
#SingleInstance force
^n::
GetKeyState, state, Lbutton
if state = D
{
Loop
{
send a
;SetMouseDelay 0.001
;Click
If(GetKeyState("LButton","P")=0)
Break
}
} else {
send b ;this codeline is only so that you can test it out in notepad. - you can remove this
}
Return
esc::exitapp
note : It is not perfect but the answer is close to your Question.

Autohotkey Return to regular function of Spacebar after mapping

i am trying to be able to toggle between different functions mapped to the spacebar
mapping spacebar isnt difficult, but how the hell do i get the "regular" function of spacebar back ?
Space::Space isnt working XD
BackSpace::tog()
tog()
{
static togstate = 0
if (togstate = 1)
{
ToolTip, Spacebar Normal
SetTimer, RemoveToolTip, 1500
Space::
togstate = 0
}
else
{
ToolTip, Spacebar Alternativ
SetTimer, RemoveToolTip, 1500
Space::
{
SetKeyDelay,5, 10
Send, 2345
}
return
togstate = 1
}
}
Ordinary hotkey bindings are more like declarations than commands or functions. So, they need to be on the global level.
You just need to use the Hotkey command, either inside or outside of functions.
Example:
Hotkey Space, funcA
Hotkey Space, funcB
Hotkey Space, Off
funcA() {
...
}
funcB() {
...
}

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

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

Why I can't get msgbox to show up in a while loop?

MButton::
Send {LWIN down}{TAB down}{LWIN up}{TAB up}
while GetKeyState("MButton")
{
Msgbox 1
}
return
I am new to Ahk, hope someone could enlighten me of my error.
Use "p" parameter to get the actual physical state of the button:
GetKeyState("MButton","p")