How to get event when input mode is ui only?

Hello. I’m trying to script pause menu. Now my script is looking like that:


I want to exit menu when I’m clicking the same key, like P key - open menu, P key - close menu. But when I’m setting input mode to ui only, it doesn’t get any of key’s anymore. So, how to do it? I tried to make close event inside the widget and call it from my script, but it still doesn’t work.