问题
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
回答1:
source
RegRead, cur, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops, CurrentVirtualDesktop
RegRead, all, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
ix := floor(InStr(all,cur) / strlen(cur))
msgbox current desktop index: %ix%
回答2:
If you need to stay with your utility and can't use VirtuaWin, for example, then I would not try to create an "aware" script that "detects" the current desktop. I would store the current desktop in a variable based upon the hotkeys you use to switch desktops, as you have been trying.
I think the problem lies in your Left and Right supporting traditional shortcuts. If there is a way to disable those shortcuts, you would have an easier and more robust script to maintain.
来源:https://stackoverflow.com/questions/47778700/how-can-autohotkey-detect-which-virtual-desktop-youre-on