jQuery Autocomplete to Fire Before KeyDown

I edited the extension texteditor.js to offer a list of possible autocomplete values to use for a textarea. To do this, I used the jQuery UI autocomplete function. Additionally, I am using textValidation to make sure the user only uses a value in my list. After the user starts typing, they can hit tab or enter to pick the first item that appears in the list. However, the keydown event listener triggers before the autocomplete item is picked and populated. This tries to accept the text first then populates the text with the item from the list. This results in an error because it is submitting the text before populating it from the autocomplete list. So, my question is whether or not there is a way I can get autocomplete to run before acceptText on the tab or enter key?

Here is the code I added to texteditor.js:

var textarea = document.createElement('textarea');
textarea.id = 'go-textarea';

$(function() {
    var availableTypes = ['values', 'go', 'here'];
    $(textarea).autocomplete({
         source: availableTypes,
         delay: 0,
         autoFocus: true
    });
} );
...

Screenshot showing autocomplete:
image

Screenshot showing what happens after hitting tab:
image

Screenshot showing what happens after hitting tab again:
image

Essentially, what I want to happen is that it goes straight from the first screenshot to the second without the error, but for this to work, it needs to populate first then run acceptText instead of the other way around.

Thanks in advance for any help you can offer.

Nevermind, I just changed:

textarea.addEventListener('keydown', function(e) {
    [code]
}

to

textarea.addEventListener('keydown', function(e) {
	if (e.key === 'Enter') {
		e.preventDefault();
		return false;
	}
});
textarea.addEventListener('keyup', function(e) {
    [code]
}

If you have a better approach, I would be interested in knowing your thoughts.