# Why does unlocking cursor with keyup behave the same as unlocking it with keydown?

To understand the difference, let me review a bit about the behavior of GetKeyUp and GetKeyDown with the following code snippet.

void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Debug.Log("Escape Key Down");
}

if (Input.GetKeyUp(KeyCode.Escape))
{
Debug.Log("Escape Key Up");
}
}


Right after pressing the Esc key (and holding it), a message "Escape Key Down" is produced. Later right after releasing the key, a message "Escape Key Up" will show. The behavior does make sense.

Now I apply this for toggling (locking/unlocking) the mouse cursor. Here is my code snippet.

void Update()
{

if (Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}

if (Input.GetKeyUp(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}


Right after clicking the left mouse button, the cursor becomes invisible. However, right after pressing down the Esc key, the cursor becomes visible again. The expected behavior is that the cursor will be visible right after releasing the Esc key rather than right after pressing the key.

# Question

How to fix this abnormal behavior?

• Doesn't pressing the escape key always break out of cursor lock, even if you delete your GetKeyUp code block entirely? May 7 '20 at 11:20
• Ah, if that turned out to be the solution, post it as an Answer. It might help others. :) May 7 '20 at 11:31
• You're welcome to, since you're the one who's tested it first-hand. May 7 '20 at 11:40

So I have to change Escape key to another key to avoid this problem.