0

我試着如果當用戶點擊進入按下轉變打開新窗口,如果他們打CTRL一個新的標籤。該轉變部分作品,但CTRL部分不...爲什麼我的代碼不能檢測到ctrl鍵狀態?

var ctrlPressed = false; 
var shiftPressed = false; 
var stb = null; 
function onload() { 
stb = document.getElementById("searchTextBox"); 
} 
    function enter(e) { 

     if (e.keyCode == 13) { 
      if (!ctrlPressed && !shiftPressed) { 
      window.location = "http://search.yahoo.com/search?p=" + encodeURI(stb.value) + "&fr2=sb-top&fr=404_web&pqstr=" + encodeURI(stb.value); 
      } 
      else if (ctrlPressed) { 
      window.open("http://search.yahoo.com/search?p=" + encodeURI(stb.value) + "&fr2=sb-top&fr=404_web&pqstr=" + encodeURI(stb.value)); 
      } 
      else if (shiftPressed) { 
      window.open("http://search.yahoo.com/search?p=" + encodeURI(stb.value) + "&fr2=sb-top&fr=404_web&pqstr=" + encodeURI(stb.value), "_blank"); 
      } 
     } 
    } 
    function searchdown(e) { 
     if (e.keyCode == 17) { 
      ctrlPressed = true; 
     } 
     else if (e.keyCode == 16) { 
      shiftPressed = true; 
     } 
    } 
    function searchup(e) { 
     if (e.keyCode == 17) { 
      ctrlPressed = false; 
     } 
     else if (e.keyCode == 16) { 
      shiftPressed = false; 
     } 
    } 

[編輯]我不能使用jquery

+0

你的活動代表團在哪裏? – 2012-04-10 04:37:16

回答

2

您知道event.ctrlKey它告訴你,如果控制鍵是壓下?

+0

是的,我有這第一,它沒有工作,所以我試過這種方式 – Oztaco 2012-04-10 01:13:34

相關問題