The words “keyboard panel” and “textView” make this sound like a question about the AppKit or UIKit libraries, which would belong elsewhere. This forum is about the Swift language itself.
However, NSTextViewDelegate might be a good place to start looking. Otherwise, Apple’s developer forums or stackoverflow.com would be good places to ask.
Just please, please don’t listen for keypresses if you are working with text. I’ve used too many programs that listened to and overrode only the keys they cared about, and let the rest through as unprocessed text. But since they misjudged my keyboard layout, the result left me with two of some letters and none of others.
And if you are not working with text, please, please don’t listen for text input. I’ve also tried to play enough games where the controls incorrectly assumed a certain keyboard layout, resulting in the buttons for left and right movement being on opposite ends of the keyboard and backwards.