키 누르기 이벤트를 시뮬레이션하려면 이벤트 핸들러를 사용하십시오. 다음 코드를 실행하여 키 누름 이벤트를 시뮬레이션할 수 있습니다.
예시
라이브 데모
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
jQuery(document).ready(function($) {
$('body').keypress(function(e) {
alert(String.fromCharCode(e.which));
});
});
jQuery.fn.simulateKeyPress = function(character) {
jQuery(this).trigger({
type: 'keypress',
which: character.charCodeAt(0)
});
};
setTimeout(function() {
$('body').simulateKeyPress('z');
}, 2000);
</script>
</head>
<body>
<p>press any key</p>
</body>
</html>