Autohotkey. How to make work both `a hotkey` and `a hotkey+key`? - autohotkey

I want to have two hotkeys in my script. Namely LWin Up and LWin+LAlt Up. I've tried to do it like that:
LAlt & LWin Up:: ;I've also tried commenting out the first
LWin & LAlt Up:: ;or the second line
LWin Up::
msgbox, % A_ThisHotkey
return
But the output depends on the order in which keys were pressed and released. The time between releasing the first and the second key also affects the result. Sometimes I get two MessageBoxes, sometimes just one and sometimes even none at all (the first line is commented out, press alt, press win, release win, release alt). How do I make it work? To be clear: I want to get only one MessageBox.
In the answer it would be great to see a script that provides full information about the hotkey pressed and the order in which the keys it consist of were pressed and released. *Only one hotkey should be triggered after releasing a hotkey+key combo.

You can not put them altogether.
you should use something like this:
altPressed := false
LAlt & LWin Up::
msgbox, % A_ThisHotkey
return
LWin & LAlt Up::
msgbox, % A_ThisHotkey
altPressed := true
return
LWin Up::
if !altPressed {
msgbox, % A_ThisHotkey
}
altPressed := false
return
If what you want to do instead of msgbox is too spreaded in my code you can use the below one:
SetTimer, toDo, 10
toDo:
if doWhatEver {
;; HERE DESCRIBE WHAT TO DO
doWhatEver := false
}
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
altPressed := false
doWhatEver := false
LAlt & LWin Up::
doWhatEver := true
return
LWin & LAlt Up::
doWhatEver := true
altPressed := true
return
LWin Up::
if !altPressed {
doWhatEver := true
}
altPressed := false
return

TechJS's answer wasn't working exactly as I wish so here is my script. It detects the order in which the keys were pressed and released, and doesn't depend on time between keys pressed/released.
global firstPressed := ""
LAlt::
altDown := true
if (winDown)
return
firstPressed := "!"
return
LWin::
winDown := true
if (altDown)
return
firstPressed := "#"
return
LAlt Up::
altDown := false
if (!winDown) {
if (comboJustUp) {
comboJustUp := false
return
}
msgbox !
}
if (winDown) {
comboJustUp := true
if (firstPressed = "#")
msgbox #!!.
if (firstPressed = "!")
msgbox !#!.
}
return
LWin Up::
winDown := false
if (!altDown) {
if (comboJustUp) {
comboJustUp := false
return
}
msgbox #
}
if (altDown) {
comboJustUp := true
if (firstPressed = "!") ; \here is one bug though. If you switch theese
msgbox !##. ; /two lines
if (firstPressed = "#") ; \with theese
msgbox #!#. ; /two. It won't work correctly for some reason
}
return

Related

AutoHotKey - Multiply variables not working

I have problems with an AutoHotKey script. When I press F1, the left mouse button is held but A is also pressed. Does anyone know how I can fix this?
#MaxThreadsPerHotkey, 2
Toggle := 0
Toggle2 := 0
F1::
Toggle := !Toggle
If (Toggle){
Click, Down
} else {
Click, Up
}
F2::
Toggle2 := !Toggle2
If (Toggle2){
send {a down}
} else {
send {a up}
}
You need to tell autohotkey that you are done writing the code that should be executed when a hotkey is pressed by putting a Return after the last part you want to execute.
From the docs:
Returns from a subroutine to which execution had previously jumped via
function-call, Gosub, Hotkey activation, GroupActivate, or other
means.
So for your script:
#MaxThreadsPerHotkey, 2
Toggle := 0
Toggle2 := 0
F1::
Toggle := !Toggle
If (Toggle){
Click, Down
} else {
Click, Up
}
return
F2::
Toggle2 := !Toggle2
If (Toggle2){
send {a down}
} else {
send {a up}
}
return

Press in random time range?

I am currently having this code which will press Z every 1.5 seconds after activated by pressing B
toggle := 0
return
b::
toggle := !toggle
if (toggle = 1)
SetTimer, Pressz, 1500
else
SetTimer, Pressz, Off
return
Pressz:
SendInput, z
v::SetTimer, Pressz, 1500
But then I am not sure how to change the SetTimer into random time between 0 to 1500
Please help thanks.
Utilizing the 'Random' function that 0x464e mentioned, and single fire SetTimer routines, I created this
toggle := 0
return
b::
toggle := !toggle
;MsgBox %toggle%
if (toggle){
gosub Routine
}
return
Routine:
if(toggle){
Random, var, -1500, 0
gosub Pressz
SetTimer, Routine, %var%
}
return
Pressz:
SendInput, z
return
v::
toggle=1
gosub Routine
return

AutoHotKey dumb syntax issue (Two functions in the same script)

I have two functions below. They work by themselves but i can't get them to work in the same script. The one at the bottom won't work. Can someone help me to fix. For example, below "LShift & RShift" or "RShift & LShift" won't work. I'm not sure what i'm doing wrong.
endKeys := "{BS}{Enter}{Insert}{Home}{Pgup}{PdDwn}End}{Delete}"
. "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
. "{LShift}{RShift}{Tab}{Esc}{CAPSLOCK}{Ctrl}{PrintScreen}{NumLock}"
. "{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}"
. "{Numpad7}{Numpad8}{Numpad9}{NumpadDel}{Up}{Down}{Left}{Right}"
. "{LAlt}{RAlt}{.}{,}{/}"
~Alt Up::
Input, key, V L1 t0.5 E, % endKeys
If (Errorlevel ~= "Alt") {
Double_ALT := true
Sleep 2000
Double_ALT := false
}
return
; Press a key within two seconds after double tapping the Alt key, to activate an action:
#If (Double_ALT)
a:: MsgBox, Test
b:: MsgBox, Test
c:: MsgBox, Test
d::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
Double_ALT :=false
return
s:: MsgBox, Test
f:: MsgBox, Test
return
LShift & RShift::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
return
RShift & LShift::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
return
endKeys := "{BS}{Enter}{Insert}{Home}{Pgup}{PdDwn}End}{Delete}"
. "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
. "{LShift}{RShift}{Tab}{Esc}{CAPSLOCK}{Ctrl}{PrintScreen}{NumLock}"
. "{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}"
. "{Numpad7}{Numpad8}{Numpad9}{NumpadDel}{Up}{Down}{Left}{Right}"
. "{LAlt}{RAlt}{.}{,}{/}"
~Alt Up::
Input, key, V L1 t0.5 E, % endKeys
If (Errorlevel ~= "Alt") {
Double_ALT := true
Sleep 2000
Double_ALT := false
}
return
; Press a key within two seconds after double tapping the Alt key, to activate an action:
#If (Double_ALT)
a:: MsgBox, Test
b:: MsgBox, Test
c:: MsgBox, Test
d::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
Double_ALT :=false
return
s:: MsgBox, Test
f:: MsgBox, Test
; return <-- likely your problem.
; #IF <-- Use this if you don't want your hotkeys below effected by the IF Directive
LShift & RShift::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
return
RShift & LShift::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
return

AuthoHotKey exclusive double tap shift followed by action key

I'm trying to complete an autohotkey script where you can double tap any SHIFT key twice and follow it by a certain letter to activate a macro.
I have the following script but i have two "bugs" i can't figure out how to solve. I've tried other forums with no luck. Hoping someone can help me out.
I'm a little new at this.
Bug 1: The macro is activated if you press any letter in between the two SHIFTs where it should only be activated if you press the SHIFTs exclusively. For example, pressing SHIFT, s, SHIFT, d will enable the macro.
Bug 2: I'm not sure how this happens but with the following code, I periodically get all or some of the macros to activate while i'm typing. It seems to happen when I type the first capital letter in a sentence. But only sometimes. For example... "bla bla bla profiles. S"
I've fooled around with the timeouts but that doesn't seem to make much of a difference. Any help is appreciated.
Thanks,
Jeff.
~Shift Up::
If (A_ThisHotkey == A_PriorHotkey && A_TimeSincePriorHotkey < 400)
{
Double_SHIFT := true
Sleep, 2000
Double_SHIFT := false
}
return
; Press a key within two seconds after double tapping the Shift key, to activate an action:
#If (Double_SHIFT)
d::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
Double_SHIFT :=false
return
a:: MsgBox, Test
s:: MsgBox, Test
f:: MsgBox, Test
return
Fix for Bug 1:
endKeys := "{BS}{Enter}{Insert}{Home}{Pgup}{PdDwn}End}{Delete}"
. "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
. "{LShift}{RShift}{Tab}{Esc}{CAPSLOCK}{Ctrl}{PrintScreen}{NumLock}"
. "{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}"
. "{Numpad7}{Numpad8}{Numpad9}{NumpadDel}{Up}{Down}{Left}{Right}"
. "{LAlt}{RAlt}{.}{,}{/}"
~Alt Up::
Input, key, V L1 t0.5 E, % endKeys
If (Errorlevel ~= "Alt") {
Double_ALT := true
Sleep 2000
Double_ALT := false
}
return
; Press a key within two seconds after double tapping the Alt key, to activate an action:
#If (Double_ALT)
d::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
Double_ALT :=false
return
a:: MsgBox, Test
s:: MsgBox, Test
f:: MsgBox, Test
return

AutoHotKey: Merge scripts that monitor selection in Firefox and other programs

I am struggling with a script that monitors selections by mouse in FireFox, Adobe Acrobat and one more program and then copies this selection to clipboard and changes it according to a regex. For each program another regex is needed. Each script works as a separate program, but when I merge them, the copied text is not changed according to my regex.
Script for Adobe Acrobat:
#ifWinActive ahk_class AcrobatSDIWindow
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
copied := true
}
return
OnClipboardChange:
if !copied
return
copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer, ToolTipOff, -1000
return
ToolTipOff:
ToolTip
return
And stript for Firefox:
#ifWinActive ahk_class MozillaWindowClass
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
copied := true
}
return
OnClipboardChange2:
if !copied
return
copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "[0-9]\.\s*|\s?\([^)]*\)|\.")
SetTimer, ToolTipOff1, -1000
return
ToolTipOff1:
ToolTip
return
The #If does only work on hotkeys, not on labels. Using OnClipboardChange seems unnecessary. When you press ctrl+c you already know that the clipboard changed.
I also really recommend setting indentations for hotkeys and also #If statements.
Here is how I would do it:
#If WinActive("ahk_class AcrobatSDIWindow") || WinActive("ahk_class MozillaWindowClass")
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
if WinActive("ahk_class AcrobatSDIWindow")
{
regex := "\r\n"
replace := " "
}
else if WinActive("ahk_class MozillaWindowClass")
{
regex := "[0-9]\.\s*|\s?\([^)]*\)|\."
replace := ""
}
ToolTip % Clipboard := RegExReplace(Clipboard, regex, replace)
SetTimer, ToolTipOff, -1000
}
return
#If
ToolTipOff:
ToolTip
return
(untested)
edit:
.....
~Left::
~Right::
~Up::
~Down::
now := A_TickCount
while GetKeyState("Shift", "P")
continue
if (A_TickCount-now > 600 )
{
oldShiftState := GetKeyState("Shift", "P")
Send, {Shift Up}
Send ^c
If (oldShiftState)
Send, {Shift Down}
.....
(untested)