03-18-2012 04:26 PM
I am learning how to write Lua scripts for G13 and was attempting to create a "toggle" type script that would work similar to what the LGS already provides with the recorded macros. Namely repeat pressing key/s when the G key is pressed and then stop when the G key is pressed again.
Below is an example of what I tried, but it would appear that OnEvent() is not multi-threaded and will not assert new events until the while loop is running (unless of course I am missing something).
if (event == "G_PRESSED" and arg == 1 and MKey == 2) then if (ScriptRunning == 0) then ScriptRunning = 1 OutputLCDMessage("Script is running", 3000) while(ScriptRunning == 1)
end end -- G2 -- if (event == "G_PRESSED" and arg == 1 and MKey == 2) then if (ScriptRunning == 1) then ScriptRunning = 0 OutputLCDMessage("Script should stop", 3000) end end
Would appreacite some help if someone could help me out with this. But more importantly to understand how the events are handled.
Solved! Go to Solution.
03-18-2012 04:56 PM
You are correct about the looping issue.
There are a couple ways to get around this. You could have your loop test for modifiers with IsModifierPressed(), which will read the current state of the modifiers (shift/ctrl/alt..) or you could use a technique called polling, which would still require you to exit the loop, but would allow you to run the loop every time it polls until you tell it to stop.
Polling is a technique that at the end of every event, the script will ensure it starts another loop shortly after. It will require you to generate events with the use of SetMKeyState, but you only want it to generate new events at the end of the last generated event, or they'll multiple.
Anyways, if Polling is interesting to you, I'd advise searching for a few examples.
03-18-2012 05:26 PM
Thanks a lot for the quick reply and confirming my suspicion
IsModifierPressed works, but it's a bit out of band as it does not involve the keypad.
I will definitelt look into implementing polling as well.