Nicolai Lissau
Nicolai Lissau

Reputation: 8312

Toggle between keyboard shortcuts with AutoHotKey

I am trying to map a key to toggle between two different shortcuts. The purpose is to easily switch between desktops in Windows 10 (instead of having to press three buttons)

What I am trying is:

toggle      := false
½:: Toggle = false ? ( ^#Right, Toggle := true ) : ( ^#Left, Toggle := false )

She script runs without errors, but it does not work.

Can someone give me a hint?

Upvotes: 0

Views: 1067

Answers (2)

stevecody
stevecody

Reputation: 676

If you want to toggle with more then two [Keyboard Shortcuts] toggle's,

you can use this AHK code.

Example1.ahk

; [+ = Shift] [! = Alt] [^ = Ctrl] [# = Win] 
#SingleInstance ignore
a := 1 

; If you want to toggle with more the two toggle's you can use this code.
;a = 1   => send {^#Right}
;a = 2   => send {^#Left}
;a = 3   => send {????}

;click the f1 key to toggle 
f1::
if (a=1)
{
Menu, Tray, Icon,c:\icons\32x32\icon1.ico,1,1 ; change tray icon
send {^#Right}
a := 2
}else{

if (a=2)
{
Menu, Tray, Icon,c:\icons\32x32\icon2.ico,1,1 ; change tray icon
send {^#Left}
a := 3
}else{

if (a=3)
{
Menu, Tray, Icon,c:\icons\32x32\icon3.ico,1,1 ; change tray icon
;send {????}
a := 1
}}}
return

esc::exitapp

Upvotes: 1

phil294
phil294

Reputation: 10822

It should be send ^#Right, but you cannot put extra commands into the ternary operator. You may only specify a value to be stored into toggle (as shown here).

Toggle = false ? ... has to be Toggle := false ? ..., for the right-hand side is an expression, not a string.

Try

%::
    toggle := !toggle
    if(toggle)
        send ^#{Right}
    else
        send ^#{Left}
return

I personally can't think of a more compact way of doing it, which you obviously want to achieve.

Upvotes: 1

Related Questions