Hi I'm new to autohotkey (and programming in general) and I wanted to write a script that lets me conveniently switch to a specific desktop. For example, in my script Capslock+3 switches to desktop 3.
As you can see or if you try it out, it's not very robust. The script only knows a desktop number separate from the real one. For example, if you run the script while on desktop 4, the script still starts with the desktop set to 1 and you have to press Caps+4 then Caps+1 to set it in the right direction. And if there is a flashing window in another desktop and you click it, it switches to that desktop while the script still thinks you're in the previous one.
I've searched for ways autohotkey can detect which desktop you're on, but could not find any.
Can anyone give any tips on how to improve it? Thanks! :D
SetCapsLockState, AlwaysOff
desktop = 1
Switch(d)
{
global
;Determine how far away the desired desktop is from current one
press := (d-desktop)
desktop = %d%
;Determine which direction to switch desktops and stop script if already on current desktop
If press < 0
direction = Left
else if press > 0
direction = Right
else
return
press := Abs(press)
Loop, %press%
{
SendInput, ^#{%direction%}
Sleep, 75
}
return
}
CapsLock & 1::
Switch(1)
return
CapsLock & 2::
Switch(2)
return
CapsLock & 3::
Switch(3)
return
CapsLock & 4::
Switch(4)
return
;In case user switches desktop with traditional shortcuts
^#Left::
SendInput ^#{Left}
If desktop > 1
desktop--
return
^#Right::
SendInput ^#{Right}
If desktop < 4
desktop++
return