Not certain that this is the only gap, but neither the key press event for skills nor the skill button press function checks the battle state before doing stuff. Adding the following couple of lines to the start of pressskill(), should cover this gap.
func pressskill(skill): if period != 'base' && period != 'skilltarget': return
Note that this site converts leading tabs to spaces, so you will need to convert them back to tabs.
This should mean that the window for pressing the 1 key after a mouse click was only about 1/30 of a second.