Autohotkey: Ctrl + 1 mapping - autohotkey

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

Related

How to activate button in a loop

I want to activate the button called EditBUtton2, but I'm finding it difficult. I've only been able to successfully identify it in a loop, just not activate it.
^+f7::
WinGet, OutputVar, ID
MsgBox, "%OutputVar%"
WinGet, ActiveControlList, ControlList, A
Loop, Parse, ActiveControlList, `n
{
if(A_LoopField = "EditButton2")
MsgBox, "found button successfully."
; I want to activate this button, but this doesn't work:
Send, {Space}
;controlsend,, {Space}, %A_Loopfield%
}
return

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!

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