Javascript How to Detect Tab key is pressed by User in C# VB.Net

In many times we need to check or detect Tab key by javascript method to implement some business logic. In this regard here i want to show you how one can detect Tab key whether a user press the TAB key or not by using a cross browser script. So from next time you can do a lots of work based on TAB key, depends on user or client requirement.

Note: Keep in mind one thing that TAB Key only fires on onkeydown and the onkeyup event. No onkeypress event is fired for TAB key.

Tab Key Detection Javascript Function:

<script type="text/javascript">
    function DetectTab(e) {
        var KeyCode = (e.which) ? e.which : e.keyCode
        if (KeyCode == 9) {
            alert("You have pressed Tab Key.");

Call the Function From C#/VB.Net TextBox Event:

<asp:TextBox runat="server" ID="txt" onkeydown="DetectTab(event)"></asp:TextBox>

Note: 9 is the key code of TAB key.

Hope now you can take necessary action by detecting the TAB key.

Script tested for:
1. Internet Explorer
2. Mozilla Firefox
3. Opera
4. Google Chrome

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code