If this is a level cheat and you have a World subclass for each level, then the code I provided should go in your World subclass act method within the key detection code. That means you would enclose the 'if' part inside some code like the following:
String key = Greenfoot.getKey();
if (key != null)
{
// my 'if' code block here
// rest of key processing (if any) here
}
