當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML KeyboardEvent altKey用法及代碼示例


HTML DOM中的KeyboardEvent altKey屬性是隻讀屬性,用於返回指示是否按下alt鍵的布爾值。如果按下alt鍵,則返回True,否則返回false。

用法:

event.altKey

下麵的程序演示了HTML中的KeyboardEvent altkey屬性:



例:本示例檢查是否按下了“ALT”鍵。

<!DOCTYPE html> 
<html> 
      
<head>  
    <title> 
        HTML DOM KeyboardEvent altKey property 
    </title> 
</head> 
  
<body> 
  
    <h1>GeeksforGeeks</h1>  
      
    <h2>KeyboardEvent altKey Property</h2> 
      
    <p> 
        Check whether the alt key is pressed or not 
    </p>  
      
    <input type="text" onkeydown="keyboard(event)"> 
      
    <p id = "test"></p> 
      
    <!-- script to check alt key event -->
    <script> 
        function keyboard(event) { 
            var a = document.getElementById("test"); 
              
            if (event.altKey) { 
                a.innerHTML = "The ALT key has been pressed!"; 
            }  
            else { 
                a.innerHTML = "The ALT key has not been pressed!"; 
            } 
        } 
    </script> 
</body> 
  
</html>                                                

輸出:
按下鍵之前:

之後按鍵:

支持的瀏覽器:下麵列出了KeyboardEvent altKey屬性支持的瀏覽器:

  • Opera
  • IE瀏覽器
  • 穀歌瀏覽器
  • Firefox
  • 蘋果Safari



相關用法


注:本文由純淨天空篩選整理自Shubrodeep Banerjee大神的英文原創作品 HTML DOM | KeyboardEvent altKey Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。