本文介绍了如何检测在asp.net文本框中按下的多个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

如果要在asp.net文本框中按下,我想检测 ctrl + a 键.

我正在使用javascript的onkeydown事件来检测按下的键
对于单键,它可以正常工作,但对于多键,则不适合使用此方法,因为当我按下一个键时会立即调用它.

任何人都可以指导我使用错误的功能或者该功能是否存在服务器端功能.


任何帮助将不胜感激.

friends,

i want to detect ctrl + a key if pressed in asp.net textbox.

i am using onkeydown event of javascript to detect keys pressed
for single key it works fine but for multiple keys it is not suitable method because it is called immediately when i press a key.

any one guide me what should i do am i using wrong function or is there server side function exists for this.


any help would be appreciated.

推荐答案

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control == true)
    {
          //(int) 'A' = 65  
          //(int) 'a' = 97
          if (e.KeyValue.Equals((int)'A') || e.KeyValue.Equals((int)'a'))
          {
                 textBox1.Text = "Pressed";
          }
    }
}



<HTML>
<Head>
<Script type="text/javascript">
function getPressedKeysCode(obj)
{
    //In textbox we don''t capture the event for backspace and delete button who are used to delete text
   if(event.keyCode != 8 && event.keyCode != 46)
   {
        var text = "";
        var value="";
        if(event.ctrlKey || event.ctrlLeft)
            text  = "ctrl";
        if(event.shiftKey || event.shiftKey)
            text  += ((text !="")?"+":"")+"shift";
        if(event.altKey || event.altLeft)
            text  += ((text !="")?"+":"")+"alt";
        if(event.keyCode && (event.keyCode == 13/*ENTER KEY*/ || (event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 112 && event.keyCode <= 123))) {

            if (event.keyCode == 13)
                text += ((text !="")?"+":"")+"Enter";
            else if (event.keyCode >= 65 && event.keyCode <= 90)
                text += ((text !="")?"+":"")+String.fromCharCode(event.keyCode);
            else
                text += ((text !="")?"+":"")+"F"+(event.keyCode-111);
        }
        obj.value =  text;
        event.returnValue= false;
        event.cancelBubble = true;
    }
}

</Script>
</Head>
<Body>
<input type="text" onkeydown="getPressedKeysCode(this)">
</Body>
</HTML>


这篇关于如何检测在asp.net文本框中按下的多个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:49