Toggle widget on and off using a key?

I have removed this but its still not working for some reason. When i click escape it comes up but when i click it again it doesn’t close.