Locking the mouse

Im attempting to script something that locks the mouse when pressing A.
Now it just jumps back to the center of the screen.

var locked = true;
 
 function Update(){
	 if(Input.GetKeyDown(KeyCode.A)){
		if(locked == false){
		Screen.lockCursor = true;
		locked = true;
		}
		if(locked == true){
		Screen.lockCursor = false;
		locked = false;
		}
	}
}

Your code is incorrect. You should add an else statement.

if(Input.GetKeyUp(KeyCode.A))
{
       Screen.lockCursor = !Screen.lockCursor 
}

In the code you have now, it will get unlocked right away.

Edit:
You don’t even need the locked Boolean.

Edit edit:
You don’t even need the IF-statement.

Last edit:
It is better to use GetKeyUp to prevent pressing it multiple times.