I want to generate gun fire whenever i press Space button

I want to generate gun fire , not continuous bust of fire images I want some delay Because gun will take time to load,and when I release that button firing should stop. For this I have used InvokeRepeating function.

if(key Pressed Down)
{
InvokeRepeating("fire",0.001f,0.5f)
}
if(key pressed up)
{
 CancelInvoke("fire");
}

I don’t know why it is not working. I work sometime but most of the time fire is keep on generating.
Thank you so much for helping in advance.

void Update(){
if (Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}