Вы не можете заставить набор текста происходить из события, которое вы запускаете с помощью кода, однако вы можете настроить прослушиватель и создать собственное событие, чтобы не перепутать инициированное событие с обычным нажатием клавиши.
$("#button").on("click", function() {
$("#input").trigger({
type: "myKeypress",
which: "A".charCodeAt(0)
});
});
$("#input").on("myKeypress", function(e) {
$(this).val($(this).val() + String.fromCharCode(e.which));
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<input type="text" id="input">
<button id="button">Type A</button>
Вы также можете выполнить эту работу со всеми клавишами:
$("#button").on("click", function() {
$("#input").trigger({
type: "myKeypress",
which: prompt("Enter a letter").charCodeAt(0)
});
});
$("#input").on("myKeypress", function(e) {
$(this).val($(this).val() + String.fromCharCode(e.which));
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<input type="text" id="input">
<button id="button">Type a letter</button>