toggling a set of hotkeys with Autohotkey - autohotkey

I have toggle variable outside of #IfWinActive block but the variable is not initialized with the #IfWinActive block. How can I toggle a set of hotkeys?
toggle := false
f12::
toggle:=!toggle
SendInput {F12}
return
#IfWinActive ahk_class Chrome_WidgetWin_1
;toggle := false ;this doesn't do anything either
if (toggle) {
^a::SendInput {HOME}
}
#IfWinActive

You can try this code.
; [^ = Ctrl] [+ = Shift] [! = Alt] [# = Win]
#SingleInstance Force
Mode := 0
GroupAdd, Browser, ahk_class Chrome_WidgetWin_1 ; Chrome or Iron
;GroupAdd, Browser, ahk_class IEFrame ; Internet Explorer
;GroupAdd, Browser, ahk_class MozillaWindowClass ; FireFox
;GroupAdd, Browser, ahk_class ApplicationFrameWindow ; Edge
f12::
mode:=!mode ;not! toggle
return
#If mode ; All hotkeys below this line will only work, if mode is true and Browser is active.
^a::
If WinActive("ahk_group Browser")
{
SendInput {HOME}
} else {
send ^a
mode := 0
}
return
q::
If WinActive("ahk_group Browser")
{
send 1
} else {
send q
mode := 0
}
return
w::
If WinActive("ahk_group Browser")
{
send 2
} else {
send w
mode := 0
}
return
#If

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

AHK Opening last opened windows explorer window if active, else starting new

This code figures out the current explorer windows open,
I would like to open the first in the list, and if the list is empty open a
new explorer instead.
I hope to the open/activate the either window at the current mouse position
#e::
list := ""
numberOfwindows := ""
wins := ""
WinGet, id, list, ahk_class CabinetWClass ahk_exe explorer.exe
Loop, %id%
{
numberOfwindows := A_Index
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
wins .= A_Index A_Space title ? A_Index A_Space title "`n" : ""
}
MsgBox, number of explorer windows = %numberOfwindows%`n`n%wins%
return
This solves it. - however it can be optimized, anyone have any suggestions ?
#e::
list := ""
numberOfwindows := ""
wins := ""
WinGet, id, list, ahk_class CabinetWClass ahk_exe explorer.exe
Loop, %id%
{
numberOfwindows := A_Index
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
if (A_Index = 1) { ; if it's the first index of the loop
;MsgBox %title%
win = %title% ; store the title in " win "
}
wins .= A_Index A_Space title ?½½ A_Index A_Space title "`n" : ""
}
IfWinNotExist ahk_class CabinetWClass
{
Run C:\Windows\explorer.exe
win := File Explorer
WinWait, %win% ahk_class CabinetWClass
WinMove, mxpos_new , mypos_new
WinActivate
}
;MsgBox, number of explorer windows = %numberOfwindows%`n`n%wins%
; above msgbox displays number and the names of the windows.
;~ ; we now know the win
; and its title, exe and class.
; we want it's current position.
WinGetPos, X, Y, Width, Height,%win% ahk_class CabinetWClass
;MsgBox, %X%, %Y%, %Width%, %Height%
; and we want the mouse position.
CoordMode, Mouse, Screen ; Coordinates are relative to the desktop (entire screen).
MouseGetPos, mxpos , mypos,
;MsgBox, %mxpos%, %mypos%
mxpos_new := mxpos - (Width / 2)
mypos_new := mypos - (Height / 2)
;MsgBox, %mxpos% %mypos% %Width% %Height% %mxpos_new% %mypos_new%
; activate that specific window
WinWait, %win% ahk_class CabinetWClass
WinMove, mxpos_new , mypos_new
WinActivate
return

AutoHotKey issue with AltTab shortcut

i was trying to create a simple script with AHK to switch between windows (similar to the "recent" button on a samsung mobile) with this code:
XButton2::
Send, {Alt down}{Tab}
Return
the problem is that i don't know how to make the script to release the Alt key when i strike Enter or click the Left mouse button.
Any help please?
XButton2::
AltTabMenu := true ; assign the Boolean value "true" or "1" to this variable
Send, {Alt down}{Tab}
return
; The #If directive creates context-sensitive hotkeys:
#If (AltTabMenu) ; If this variable has the value "true"
; The * prefix fires the hotkey even if extra modifiers (in this case Alt) are being held down
*Enter::
*MButton::
Send {Enter}
Send {Blind}{Alt Up} ; release Alt
MouseCenterInActiveWindow()
AltTabMenu := false
return
~*LButton::
Click
Send {Blind}{Alt Up} ; release Alt
MouseCenterInActiveWindow()
AltTabMenu := false
return
; menu navigation by scrolling the mouse wheel:
*WheelUp:: Send {Right}
*WheelDown:: Send {Left}
#If ; turn off context sensitivity
MouseCenterInActiveWindow(){
WinGetPos,,Y, Width, Height, A ; get active window size
Xcenter := Width/2 ; calculate center of active window
Ycenter := Height/2
MouseMove, Xcenter, Ycenter, 0
}

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)

How To Detect Ctrl+V In AHK?

I am trying to detect Ctrl+V. Then, make Xbutton1act as Enter for a few seconds, but I can't get it working.
Transform, CtrlV, Chr, 3
Input, OutputVar, L1 M
XButton1::
if OutputVar = CtrlV
{
SetTimer, SendEnter, 0
Sleep, 2000
SetTimer, SendEnter, Off
}
else
{
Send ^t
}
Return
SendEnter:
Send {Enter}
Return
~^v::lastPaste := A_TickCount ;stores counter when ctrl+v is pressed
Xbutton1::
If A_TickCount - lastPaste < 2000 ;checks if 2 seconds gone after ctrl+v was clicked
{
Send, {Enter} ;sends enter
Return
}
else
{
Send, ^t ;sends ctrl+t
Return
}