用于检测单击了哪两个键盘键的javascript

时间:2012-10-25 09:14:52

标签: javascript html

我有一个html页面,上面有菜单。菜单有一个JavaScript。 在这个javascript中如果按下键盘键1然后我调用第一个菜单的链接,这工作正常,但现在要求增加到17-18菜单。也就是说,如果用户按下16(即键1然后键6),则必须显示第16个编号的菜单。我想帮助确定同时按下哪两个键。

if(event.keyCode==49)
            {
                self.location="pages/abc/finishgoods.jsp";

            }
            if(event.keyCode==50)
            {
                window.navigate("pages/submenu.jsp");

            }

任何人都可以帮我检测一下。我不能使用jquery,因为我的页面适用于IE 6

1 个答案:

答案 0 :(得分:1)

这些方面的东西(未经测试,但理论在这里)

var current_keys = [], // to store keypresses
    key_timer;

// on press, store the current key and give the user a little while to press another
// on keypress {
  current_keys.push(event.keyCode);
  clearTimeout(keyTimer); // refresh the timer
  keyTimer = setTimeout(interpret_keys, 250);
// }

// they've had their chance, work out what they pressed
function interpret_keys ()
{
  var keys = -1
      key,
      i = 0;

  for (i; i < current_keys.length; i++)
  {
    key = current_keys[i] - 48; // turn 48 to 0, 49 to 1, etc
    key >= 0 && key <= 9 && keys += '' + key; // only 0-9 is valid here
  }

  keys = parseInt(keys); // make sure it's a number
  current_keys = []; // reset the tracking variable

  // keys now contains (theoretically) a number such as 1, 2, 16, etc, which can map to your selectable item
}