Code:
void toggle_switch(){
   while(key_space){ wait(1); }
   //turn off switch here
}

void main(){

   ...
   on_space = toggle_switch;
   ...
}