I made a new empty .cap. Just to check for switching between Layouts.
When pressing Space, it should change to the next Layout.
Now what You see is this:
1) Space Pressed - Reloads same Layout
2) Space Pressed - Loads wanted/next Layout
3) 4) 5) ... Space Pressed - Works as expected.
But every time the first press (after running the application) reloads the same Layout.
2nd EDIT: Okay, here's what I found out now. It has nothing to do with the MouseKeyboard Object being global. It is indeed the Layout Switch making trouble. The first time You make a layout switch after application start, it will in fact read switch to the other Layout AND continue reading the Events of the new Layout in the SAME Tick. It will add the Events of the new Layout to the current Tick.
The next time You switch Layouts, it works as expected.
You can check this by switching the layout upon a button press, and increase a global variable by one.
Now on the second Layout, for the same button press, You switch the layout back to the first, and increase the same variable by one.
If You now press the button the first time, the variable will increase by 2, after that each consecutive press will just increase by one.