Using keys in widgets

Hi, I was wondering how can I use an input key or just a key in a menu to close it or to go back. For example: pressing the right mouse button to close the settings menu. Also, what’s the alternative to a delay to use in a function?