Improved stateChanged event handling based on keys