How to insert text to ANY selected textfield

Hi I'm building a custom keyboard using swift UIKit and SwiftUI. I need help figuring out how to insert text after a key is pressed to the selected/current text field. If I create a textfield I can insert text to that textfield with no problem. However, the issue is that I want to be able to insert text to any textfield that the keyboard is currently accessing just as Apples' default keyboard does. Whenever a textfield is found/selected Apples' keyboard can insert/delete text to that field.

I built my keyboard user interface (UI) with SwiftUI and used the target (template) 'Custom Keyboard Extension' that uses the view controller UIInputViewController (KeyboardViewController) to add a view to the area where Apples Default keyboard is placed. Because I'm using both SwiftUI and UIKit I created a virtual controller so that my SwiftUI view (KeyboardView) and the view controller could communicate and overlap the view of the UIInputViewController. The images below shows the main code of the KeyboardViewController

I am trying to create a function either in the KeyboardView (swiftui) or KeyboardViewController (UIKit) that inserts text to any selected textfield after a key is pressed. I read on Developer.Apple.com that textDocumentProxy is what I should use to insert text but this method is not working.

Also for clarity this is the KeyboardView that creates the UI for the keyboard.

Does anyone have an idea on why the textDocumentProxy doesn't work for inserting text?

I wanted to post more images but the site restricts me to only 1 image because I'm a first time user. Just let me know if you need to see more images