As a reminder, midi is just a set of instructions. Midi instruments typically don't 'record' in the analog sense of the word.
If a keyboard has a midi out port or midi over USB then it will send the instructions out to be captured (recorded) by Sonar. The only keyboards that I'm aware of that capture (record) midi are the keyboard arranger/workstations. But then, I'm not sure so much as they record midi in or just allow you to save what you play on the keyboard to various internal midi channels for layering and arranging in the instrument.
Midi keyboards as opposed to controllers, typically have a midi in port to allow other gear to control their instrument library but don't typically record. Yes, they are some that do but I'm not sure that you would really want one of those if you have Sonar - kinda overkill.
For example, the 'keyboard' I have is actually just a controller. It makes as much sound as the keyboard I'm typing on. It sends midi instruction out to other gear so I can control the synth module or send it to a lighting console. While these pieces receive midi they don't 'record' midi. Sonar is the only thing that captures and saves the midi instructions.