Help on a "desk toggling" function


I’m trying to do the following:

I have bound the key F20, with the modifier M, to a function. This function is all about “toggling between specific virtual desks, depending on the state of applications residing within these specific desks” (make sense?).

The function needs to say: Aha! application Foo_1, in desk 1(notice the numbering?) is iconic and application Foo_2 in desk 2 doesn’t exist right now, so, go to desk 0, check to see which of 2 known applications residing on desk 0 is currently visible (Foo_0a or Foo_0b). When it knows which one is visible, the visible app needs to recieve focus, as well as FakeKeypresses.
if Foo_1 isnt iconified, and it’s not on the current desk, the function needs to toggle to desk1.
else if Foo_2 currently exists, and it’s not on the current desk, toggle to desk 2

The basic psuedo code is as follows:

if (Foo_2 doesn' exist AND Foo_1 is iconified)
    Find the which app (Foo_0a or Foo_0b) is visible on Desk 0
    Toggle to Desk 0 
     place the focus on the visible app
     pass the Fakekeypress's
else if (Foo_1 !Iconfied AND Not on the current Desk)
      Toggle to Desk 1 and Focus on Foo_1
else if (Foo_2 exists AND Not on current Desk
      Toggle to Desk 2 and Focus on Foo_2

The actualy crappy function I have written is here:

Key F20 A A FuncCatchKeyPress
DestroyFunc FuncCatchKeyPress
AddToFunc   FuncCatchKeyPress
+ I FuncToggleScreens "Foo_1" "Foo_2"

        AddToFunc FuncToggleScreens
        + I Next ($$0, Iconic, !CurrentDesk, CirculateHit) Function LoadFoo_2
        + I TestRc (Match) Break
        + I Next ($$0, !Iconic, !CurrentDesk, CirculateHit) Function ToggleFoo_1
        + I TestRc (Match) Break
        + I Next ($$1, Maximized, !CurrentDesk, CirculateHit) Function ToggleFoo_2
        + I TestRc (Match) Break

        #helper function
        DestroyFunc LoadFoo_2
        AddToFunc   LoadFoo_2
        + I CheckFoo_2 "Foo_2"

        DestroyFunc CheckFoo_2
        AddToFunc   CheckFoo_2
        + I Next ($$0, Maximized, CirculateHit)
        + I TestRc (NoMatch) Function LoadFoo_0

        DestroyFunc LoadFoo_0
        AddToFunc   LoadFoo_0
        + I PropogateFoo_0 "Foo_0a" "Foo_0b"
        #helper function
        DestroyFunc PropogateFoo_0
        AddToFunc   PropogateFoo_0
        + I Next ($$0, Visible CirculateHit) Function SwitchApps
        + I TestRc (Match) Break
        + I Next ($$1, Visible, CirculateHit) Function SwitchApps

        # Helper function
        DestroyFunc SwitchApps
        AddToFunc   SwitchApps        
        + I GoToDesk 0 0
        + I Focus
        + I WindowId $[] FakeKeypress press F20 press Meta_L release F20 release Meta_L

        # Helper function
        DestroyFunc ToggleFoo_1
        AddToFunc   ToggleFoo_1
        + I Raise
        + I Focus
        + I GoToDesk 0 1

        # Helper function
        DestroyFunc ToggleFoo_2
        AddToFunc   ToggleFoo_2
        + I Raise
        + Focus
        + I GoToDesk 0 2

I admit that this is convoluted… but I don’t know how else to accomplish my desired goal.

the function will appropriately toggle between desks 1 and 2 if Foo_1 isn’t iconified and Foo_2 exists, but I can’t get Foo_0a or Foo_0b to gain focus, or for the synthetic key presses to pass to them correctly. Anyone have any thoughts?

If anyone knows an easier way to do this type of thing (controlling where and how toggling between specific desks can be done, depending on the state of applications residing in those desks) in a more simple fashion than the crap I have typed above, I would gladly accept any and all criticism in the hopes of recieving some help.


It does, yes, although what you’re describing here is no different to the other post(s) that already go some way to answering your question – binding the resultant function to a key-binding, is easy.

Bear in mind that FakeKeyPress is not guaranteed to work, as the events are often synthetic (recall we’ve discussed this in the past.)

All ("Foo_0a|Foo_0b") ThisWindow (CurrentDesk) ...


It’s not crap, Skender, you just need to piece together everything myself and others have suggested, and to be honest, what you’ve produced thus far is quite good.

– Thomas Adam

thanks Thomas,

I’ll give the

a shot… and I’ll play with it a bit.

And yes, FakeKeyPress is unstable… but the real concern you addressed with the getting the focus to shift to the desk 0 0.

Thanks again,


  • One of these days you’re going to have to let me buy you a pint… or 5.

Woops. I noticed that I made a mistake. I fixed it in my original post. You probably want to use:

All ("Foo_0a|Foo_0b")

… instead.

No worries. Hmm, I wouldn’t say it was ‘unstable’, more ‘unreliable’ given the event type generated. There’s just no guarantee that the event can be propogated down to the application, or indeed, whether the application will respond to it. Fingers crossed in your case, though. :slight_smile:

Thank you :slight_smile:. A nice offer, but I don’t drink alcohol. :slight_smile: Lemonade will do, of course (or ginger beer). I bet you’re not in the UK, though. :slight_smile:

– Thomas Adam