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


HTML KeyboardEvent shiftKey用法及代碼示例


HTML DOM中的KeyboardEvent shiftKey屬性是隻讀屬性,用於返回一個布爾值,該布爾值指示是否按下SHIFT鍵。如果按下SHIFT鍵,則KeyboardEvent shiftKey屬性返回true,否則返回false。

用法:

event.shiftKey

以下示例程序旨在說明HTML DOM中的KeyboardEvent shiftkey屬性:


例:本示例檢查是否按下了SHIFT鍵。

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

輸出:
之前按鍵:

之後按鍵:

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

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


相關用法


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