Thanks for your suggestions Bvideo and Tobiaslindhal.
It is not whilst I am switching between synths, but during live playing through Sonar. The note getting stuck does not appear to be related to the sustain pedal (CC#64). If I am recording, the stuck note is actually printed in the Piano Roll view as a long MIDI event. It happens with various soft synths, so I can’t blame the virtual instrument.
I guess I could try the Midiox test, though I am reluctant to download more software.
If it was the MIDI hardware causing the problem, I wonder why pressing Play on Sonar cuts the note? One would think the stuck note would continue to be generated for as long as it is being monitored through the DAW?
Another strange thing that happens is if I have other soft synths loaded in Sonar, after I cut the stuck note by pressing Play in Sonar, and then resume playing the keyboard again, the first note I play generates sound from the other soft synths as well. E.g. I have a string pad and a virtual drum instrument loaded in Sonar; I am playing the string pad and out of nowhere a note I have not played sounds, and continues to sound; I hit Play in Sonar which cuts the note; I go to play the string pad again and the first note I strike generates the string pad
and the drums.