Optimize autohotkey code / send keyboard code - autohotkey

I'm impressed with what AutoHotkey can do. How can I optimize that code? What do I need to know?
SetTitleMatchMode RegEx ;
::/act1::
Send {LControl down}
Send {LShift down}
Send {m}
Send {LControl up}
Send {LShift up}
Send {Left 3}
Send {LShift down}
Send {Home}
Send {LShift up}
Send {LControl down}
Send {c}
Send {LControl up}
WinActivate WidnowA
Send {LControl down}
Send {Home}
Send {LControl up}
Send {Down 1}
Send {Right 12}
Send {LControl down}+{v}
Send {LControl up}
Send {,}
Send {Space}
Send {LControl down}
Send {s}
Send {LControl up}
CoordMode, Mouse, Screen
x := 150
y := 1420
Click %x% %Y%
Send {Right 3}
return
I think that no need to describe the sections, but.. can I write it another (easiest) way?
Thanks

Along with the possible typo David found, you can condense a lot of your send commands into one line. Also, does it need to be left control and shift or will either do? I've written it below without specifying.
SetTitleMatchMode , RegEx
CoordMode, Mouse, Screen
::/act1::
Send , ^+m{left 3}+{home}^c
WinActivate , WindowA
Send , ^{home}{down}{right 12}^v{,}{space}^s
Click , 150 , 1420
Send , {right 3}
Return
As far as writing it an easier way, it will help to know what you're trying to accomplish. It's likely that what you're trying to do can be done more reliablely by manipulating controls directly instead of sending keystrokes.

I don't see a lot of room to optimize here as there isn't much repetition going on. If you had the same series of keystrokes being used, you could reduce repetition with a function. However, it does appear that you have a typo at "WinActivate WidnowA"
One possible improvement is changing your use of "Send" to "SendInput" which is "generally faster and more reliable".

Related

Send Keypress to specific window/programm only

I have a Script that toggle a source in SLOBS (OBS), how can I make it that the keypress get only send to OBS, mostly my games also react to the Hotkeys, maybe it's possible to get the keypress only send to OBS so that the game is not affected by it, is this possible my actual script:
#Persistent
SetTimer, PressTheKey, 1800000
Return
PressTheKey:
Send, {F24 down}
Sleep, 50
Send, {F24 up}
Sleep, 10000
Send, {F24 down}
Sleep, 50
Send, {F24 up}
return
You need to use ControlSendRaw.

AutoHotkey - capture a hotkey combination in one application only - do not prevent other apps from using same combination

I have created a hotkey that I want to use only in MS Teams (workaround for the lack of 'Reply to message' function).
I assigned it to Ctrl+R, however it seems that it prevents other applications that use the same combination from noticing the hotkey.
The code is below:
^r::
if WinActive("ahk_exe Teams.exe")
{
SoundBeep 200,500
Send, ^c
Send, {Tab}
Send, >
Sleep, 500
Send, ^v
Send, {Enter}
Send, {Enter}
}
return
Is there a way to tell AHK to let the key combination bubble up when the active app is NOT teams?
I tried adding an 'else' clause in which I would Send, ^r but that didn't work.
Actually, I got it. Posting the working solution.
It seems I needed to wrap the hotkey declaration within the #ifwinactive directive.
#IfWinActive("ahk_exe Teams.exe")
^r::
SoundBeep 200,500
Send, ^c
Send, {Tab}
Send, >
Sleep, 500
Send, ^v
Send, {Enter}
Send, {Enter}
return
#IfWinActive
An even better, comprehensive solution and more powerful approach by #ThierryDalon - https://github.com/tdalon/ahk/blob/master/Lib/Teams.ahk
https://tdalon.blogspot.com/2020/11/teams-shortcuts-smart-reply.html

Can AutoHotKey intercept a mouse click?

aI use AutoHotKey to remap a few keys, and that has been extremely useful. Example:
!t::
Send, ^t
return
That would intercept Alt-t key and send Ctrl-t instead.
I was wondering if there's a way to intercept an Alt-LeftMouseClick and send Ctrl-LeftMouseClick instead?
This maps ALT+LeftMouseClick to Ctrl+LeftMouseClick
!lbutton::send ^{click}
Turns out, I could do it with:
!LButton::
Send, {Control down}
MouseClick
Send, {Control up}
return

Solution to AHK sending too many events when holding down a mouse button?

When using three different methods of holding down the left mouse button:
Mouseclick, left, 0, 0, 1, , D, R
or
Send {LButton down}
or
Click down
the game I'm making a macro for logs me out complaining that I'm sending too many actions. I tested this by itself as a script, like:
F3::
Click down
return
So there's no chance other code is causing it.
I was wondering if there are any settings I can use (by settings I mean like CoordMode, Mouse, Screen) or any other solution to perhaps prevent whatever rapid event sending AHK is using to simulate a mouse button being held down. Is there any possible fix I can try? I'm open to testing any ideas.
Well, you could introduce a Sleep like so:
LButton::
while (GetKeyState("LButton", "P"))
{
MouseClick, left
Sleep, 100
}
return
Though I personally dislike binding the mouse click to behaviours via the same mouse click, as it can lead to results that are difficult to get out of.
Here's an example with a toggleable hotkey.
Insert::
SendInput, {LButton Down}
loop
{
Sleep, 100
if getkeystate("Insert", "p") ; Hold for a second as the Sleep will delay recognition.
{
SendInput, {LButton Up}
break
}
}
return
Edited per the comments:
Insert::
SendInput, {LButton Down}
KeyWait, Insert, D
SendInput, {LButton Up}
return

Mapping 2 different results to the same key

I'm kinda new at this. I have a mouse with only 3 keys that I'd like to write a script for to allow me to use the right mouse button like a "browser back" key if clicked, while still retaining the original function if held for a longer period of time.
RButton::
sleep 400
GetKeyState, state, RButton
if state = U
send {Browser_Back}
else
send {RButton}
keywait, RButton
return
Currently, all my script above does now is activates the "browser back" function, regardless of time held down. I think there's a problem with the key being repeated at the send {RButton} line, but adding a $ to RButton:: didn't seem to help (if it was supposed to, idk.) If I replace the 3 "RButton" instances (not including the one on the send line) with a key on the keyboard, it works perfectly though. Help would be appreciated. Thanks.
Adjusted the code from BlackHolyMan's response to fix it. In case anybody was curious or wanted it, here it is:
RButton::
KeyWait, RButton, U T0.5
If !ErrorLevel
{
send {Browser_Back}
return
}
else
{
send {RButton Down}
KeyWait, RButton
send {RButton up}
}
return
Hi this may be just about what you need
RButton::
KeyWait, RButton, U T0.5
If !ErrorLevel
send {Browser_Back}
else
{
send {RButton Down}
KeyWait, RButton
send {RButton up}
}
return
Still some things you may need to fix as i did not test it for long...