简体   繁体   English

如何在文本框中捕获关键事件?

[英]how to catch key event on textbox?

if i have focus on textbox and i press enter or Esc 如果我专注于文本框,然后按Enter或Esc

how to catch this event ? 如何赶上这一事件?

You can use the JavaScript onkeyup event directly: 您可以直接使用JavaScript onkeyup事件:

<input id="Text1" type="text" onkeyup="keyPress(event)" />

Ore use the C# alternative: 矿石使用C#替代方法:

Text1.Attributes.Add("onkeyup", "keyPress(this)");

Then the script code: 然后是脚本代码:

<script type="text/javascript">
    function keyPress(e)
    {
        var textBox = document.getElementById('Text1');

        var keynum;

        if (window.event) // IE
            keynum = e.keyCode;
        if (e.which) // Other browser
            keynum = e.which;

        switch (keynum)
        {
            case 13:
                //enter key
                break;
            case 27:
                //esc
                break;
        }
    }
</script>

Here there are some guidelines to catch the keystrokes in JavaScript, with a sample at the bottom. 这里有一些指南来捕获JavaScript中的击键,底部有一个示例。

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

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