Prevent keyboard viewer from displaying
This problem hasn’t bothered me for some time, but I remember it used to occur (though I couldn’t pinpoint the exact circumstances). I have tried two different things:


After invoking the Keyboard Viewer, don’t close it by clicking the red close window button, but by clicking Hide Keyboard Viewer on the Input Menu in the menu bar.
If that doesn’t help, try deleting the Keyboard Viewer preference file: ~/Library/Preferences/com.apple.KeyboardViewer.plist.