To better understand how the scripts are executed, imagine your window has the following script attached:
some_code_1
on "SomeEvent"
{
event_handling_code
}
some_code_2
function SomeFunc()
{
function_code
}
some_code_3
When the window is created, all the code outside functions and event handlers is executed, in this case the script will execute: some_code_1, some_code_2, some_code_3.
After that the script stays persistent in memory and only waits for the events to be triggered.
It means the "if" code in your example will only be executed once, when the window is created.
I really don't see any reason why this code couldn't be inside the AddKey() function. You need to check the number of pressed key only when some of them has been pressed anyway...