2

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

how to catch this event ?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.