简体   繁体   中英

how to catch key event on textbox?

if i have focus on textbox and i press enter or Esc

how to catch this event ?

You can use the JavaScript onkeyup event directly:

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

Ore use the C# alternative:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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