Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to raise an event when tab key is pressed from key board in asp.net?

i need to trigger an event when user click tab button from keyboard?

how can i achieve it?

please help..
Posted

You would have to do it by JavaScript, for example like it was discussed in this[^] SO thread. An overview of the key press event implementations can be found here[^], use this[^] test page to try it out.

<script language="JavaScript">
init();
</script>


function init()
{
    document.testform.t.value+= &#39;&#39;;
    lines= 0;

    if (document.addEventListener)
    {
       document.addEventListener(&quot;keydown&quot;,keydown,false);
       document.addEventListener(&quot;keypress&quot;,keypress,false);
       document.addEventListener(&quot;keyup&quot;,keyup,false);
       document.addEventListener(&quot;textInput&quot;,textinput,false);
    }
    else if (document.attachEvent)
    {
       document.attachEvent(&quot;onkeydown&quot;, keydown);
       document.attachEvent(&quot;onkeypress&quot;, keypress);
       document.attachEvent(&quot;onkeyup&quot;, keyup);
       document.attachEvent(&quot;ontextInput&quot;, textinput);
    }
    else
    {
       document.onkeydown= keydown;
       document.onkeypress= keypress;
       document.onkeyup= keyup;
       document.ontextinput= textinput;   // probably doesn&#39;t work
    }
}

function showmesg(t)
{
   var old= document.testform.t.value;
   if (lines &gt;= maxlines)
   {
    var i= old.indexOf(&#39;\n&#39;);
    if (i &gt;= 0)
        old= old.substr(i+1);
   }
   else
    lines++;

   document.testform.t.value= old + t + &#39;\n&#39;;
}

function keyval(n)
{
    if (n == null) return &#39;undefined&#39;;
    var s= pad(3,n);
    if (n &gt;= 32 &amp;&amp; n &lt; 127) s+= &#39; (&#39; + String.fromCharCode(n) + &#39;)&#39;;
    while (s.length &lt; 9) s+= &#39; &#39;;
    return s;
}

function keymesg(w,e)
{
    var row= 0;
    var head= [w, &#39;        &#39;];
    if (document.testform.classic.checked)
    {
    showmesg(head[row] +
            &#39; keyCode=&#39; + keyval(e.keyCode) +
        &#39; which=&#39; + keyval(e.which) +
            &#39; charCode=&#39; + keyval(e.charCode));
    row= 1;
    }
    if (document.testform.modifiers.checked)
    {
    showmesg(head[row] +
            &#39; shiftKey=&#39;+pad(5,e.shiftKey) +
        &#39; ctrlKey=&#39;+pad(5,e.ctrlKey) +
        &#39; altKey=&#39;+pad(5,e.altKey) +
        &#39; metaKey=&#39;+pad(5,e.metaKey));
    row= 1;
    }
    if (document.testform.dom3.checked)
    {
    showmesg(head[row] +
        &#39; key=&#39;+e.key +
        &#39; char=&#39;+e.char +
        &#39; location=&#39;+e.location +
        &#39; repeat=&#39;+e.repeat);
    row= 1;
    }
    if (document.testform.olddom3.checked)
    {
    showmesg(head[row] +
        &#39; keyIdentifier=&#39;+ pad(8,e.keyIdentifier)+
        &#39; keyLocation=&#39;+e.keyLocation);
    row= 1;
    }
    if (row == 0)
    showmesg(head[row]);
}

function pad(n,s)
{
   s+= &#39;&#39;;
   while (s.length &lt; n) s+= &#39; &#39;;
   return s;
}

function suppressdefault(e,flag)
{
   if (flag)
   {
       if (e.preventDefault) e.preventDefault();
       if (e.stopPropagation) e.stopPropagation();
   }
   return !flag;
}

function keydown(e)
{
   if (!e) e= event;
   keymesg(&#39;keydown &#39;,e);
   return suppressdefault(e,document.testform.keydown.checked);
}

function keyup(e)
{
   if (!e) e= event;
   keymesg(&#39;keyup   &#39;,e);
   return suppressdefault(e,document.testform.keyup.checked);
}

function keypress(e)
{
   if (!e) e= event;
   keymesg(&#39;keypress&#39;,e);
   return suppressdefault(e,document.testform.keypress.checked);
}

function textinput(e)
{
   if (!e) e= event;
   //showmesg(&#39;textInput  data=&#39; + e.data);
   showmesg(&#39;textInput data=&#39;+e.data);
   return suppressdefault(e,document.testform.textinput.checked);
}</pre>
 
Share this answer
 
If you want to check Tab press in particular, then you have to check the KeyCode which is 9.
Refer - Call onBlur event only when tab key is pressed[^]
JavaScript
if ((e.which || e.keyCode) == 9) {
  // tab key was pressed, do stuff
}

Otherwise, you can handle onblur event, which is fired on Tab press as well as on clicking outside the element.
 
Share this answer
 
Try this

JavaScript
<script type="text/javascript">
       document.onkeyup = PresTab;

       function PresTab(e)
       {
           var keycode = (window.event) ? event.keyCode : e.keyCode;
           if (keycode == 9)
           alert('tab key pressed');
       }

   </script>
 
Share this answer
 
Comments
Member 10517120 30-Jan-14 23:27pm    
tnk u karthik..

by the way my name i karthik too..
Karthik_Mahalingam 30-Jan-14 23:28pm    
Welcome karthik :
Same Pinch :)
Karthik_Mahalingam 30-Jan-14 23:29pm    
update your name as Karthik, it is showing some auto generated name...
Member 10517120 30-Jan-14 23:42pm    
k i will update ...


wonderfull peice of code .. working very fine..
Karthik_Mahalingam 30-Jan-14 23:45pm    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900