Keyboardevent listener

I would like to make a generic keyboardevent listener, or place a keyboardeven listener method in a class, but every time i do this it doesn’t seem to work. I can only get it to work in my main class.