我可以确定之前按过哪个键吗? JavaScript的

时间:2011-10-25 09:01:02

标签: javascript jquery html

我可以确定之前按过哪个键吗?

$("#divex").keypress(getKey);

    function getKey(e)
    {
       //alert previous key pressed to e ???
       alert(e.which); //gets current key ascii code
    }

1 个答案:

答案 0 :(得分:8)

您可以将其存储在变量中:

var prevKey = null;

$("#divex").keypress(getKey);

function getKey(e)
{
   if (prevKey !== null) alert(prevKey);
   prevKey = e.which;
   alert(e.which); //gets current key ascii code
}
相关问题