如何在asp.net的键盘上按Tab键时引发事件 [英] how to raise an event when tab key is pressed from key board in asp.net

查看:98
本文介绍了如何在asp.net的键盘上按Tab键时引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net的键盘上按Tab键时引发事件?



i需要在用户点击键盘上的标签按钮时触发事件?



我怎样才能实现它?



请帮助..

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..

推荐答案

你必须通过JavaScript来完成它,例如在 this [ ^ ] SO线程。可以在此处 [ ^ ],请使用 [ ^ ]测试页面试用。



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>


如果您想特别检查Tab按,那么您必须检查 KeyCode 这是9.

参考 - 仅当tab键为时才调用onBlur事件按下 [ ^ ]

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[^]
if ((e.which || e.keyCode) == 9) {
  // tab key was pressed, do stuff
}



否则,您可以处理onblur事件,该事件在Tab按下以及在元素外部单击时触发。


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


尝试这个



Try this

<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>


这篇关于如何在asp.net的键盘上按Tab键时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆