Help with switching desktops

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
frndrmn
Posts: 1
Joined: 20 Dec 2023, 18:48

Help with switching desktops

20 Dec 2023, 18:51

The code is meant to switch to the next desktop when f12 is pressed, and then switch backwards once f12 is pressed a second time, right now it switches to the next desktop, but doesn't switch backwards.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1

#Persistent
desktopSwitched := false
Return

*F12::
    ; Wait for F12 to be released
    While GetKeyState("F12", "P")
        Sleep, 10

    ; Toggle desktops
    ToggleDesktop()
Return

ToggleDesktop()
{
    if (desktopSwitched)
    {
        ; Switch back to the previous desktop
        Send {Ctrl Down}{LWin Down}{Left}{LWin Up}{Ctrl Up}
    }
    else
    {
        ; Switch to the next desktop
        Send {Ctrl Down}{LWin Down}{Right}{LWin Up}{Ctrl Up}
    }

    ; Toggle the desktopSwitched variable
    desktopSwitched := !desktopSwitched
}
[Mod edit: Moved topic from AHK v2 help to v1 help, based on posted code.]
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Help with switching desktops

22 Dec 2023, 16:44

Read Global Variables
to learn how to access global variables within a function.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton, FortniteSpanishGuy, Google [Bot] and 210 guests