Key Released isn't working correctly - releasing too early

Hello! I’m working on a project and have found the key released node isn’t working correctly. Basically what I want is spacebar pressed → menu pops up → key released → menu disappears. The first two steps work, however I’ve found that if I click a button in the menu, the game thinks that the spacebar is released and the menu disappears. If I don’t click a button (using the mouse), then everything works fine. Does anybody know why this happens? Thanks in advance!