Two of the things I check when I record with a keyboard are making sure i have automation write and read enabled on the midi track i am recording to. The other one is a cakewalk option in preferences/project/midi try unchecking the following two options (from the help file)
Other Options
●
Zero Controllers When Play Stops. If you choose this option, SONAR sets the controller values of the pitch wheel, the sustain pedal, and the modulation wheel on all 16 MIDI channels to zero. It also sends a “zero all continuous controllers” MIDI message which turns off other continuous controllers on newer synthesizers. If you experience frequent stuck notes when playback stops, try checking this option.
●
Patch/Controller Searchback Before Play Starts. If you choose this option, SONAR finds and sends the most recent patch changes, wheel events, and pedal events before starting playback. This way, all settings are correct even if you start playback at an arbitrary point in the project.