JSFiddle - React, Tailwind, and code Playground
HTML
<form>
<input type="text" id="txtfld" onkeypress="f2()"/>
<input type="button" onclick="typeKey()" value=go >
</form>
<script>
function fire(el,evttype,code) {
if (document.createEvent) {
var evt = document.createEvent('HTMLEvents');
evt.initEvent( evttype, false, false);
evt.charCode= evt.keyCode = code;
el.dispatchEvent(evt);
} else if (document.createEventObject) {
var evt = document.createEventObject();
evt.charCode= evt.keyCode = code;
el.fireEvent('on' + evttype,evt);
}
}
function typeKey(){
var el = document.getElementById('txtfld');
ev = window.event ? window.event : ev;
fire( el,'keypress',68);
}
function f2(ev){
var KeyID = (window.event) ? event.charCode : ev.keyCode;
alert('key pressed '+KeyID);
}
</script>