简体   繁体   English

当任何键盘按键按下html javascript时触发什么事件

[英]what event fired when any keybord key press in html javascript

i want to know that what event fired what happens when any key pressed by keyboard on html page. 我想知道什么事件触发了html页面上的键盘按下任何键时会发生什么。 i want to do that with javascript function ie if any html page have a text box with focus and then any key will be pressed it will write in text box. 我想用javascript函数做到这一点,即如果任何html页面都有一个具有焦点的文本框,然后将按下任何键,它将在文本框中写入。 so i want fire same event with javascript function i dont want to change text box value directly. 所以我想用javascript函数触发同一事件,但我不想直接更改文本框的值。

1 more example for make clear what i wanna say. 还有1个例子可以说明我想说的话。 if i have add a event listener for key press and alert something every time when any key pressdown or up. 如果我已经为按键添加了事件侦听器,并且每次按下或按下任何键时都会发出警报。 so if any key will be press by keyboard msg will alert. 因此,如果将要按下键盘上的任何键,则味精将发出警报。 so i want to do same via javascript function or any thing what is available. 所以我想通过javascript函数或可用的任何东西做同样的事情。 and then message should be alert every time when any key press via javascript 然后每当通过javascript按任意键时,消息都应该处于警报状态

I am not sure this is the excat same that you want: 我不确定这是否是您想要的专长:

<body>
    <input type="text" style="width: 300px;" id="captureInput" />
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            var captureInput = $('#captureInput');
            $(captureInput).focus();
            var e = jQuery.Event("keydown");
            e.which = 77; // # Some key code value
            $(captureInput).val(String.fromCharCode(e.which));
            $(captureInput).trigger(e);
        });
    </script>
</body>

and this is jsfiddle demo 这是jsfiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM