@hunter129

          天天學習,好好向上!

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            21 隨筆 :: 5 文章 :: 37 評論 :: 0 Trackbacks
          做web開發的時候,有時候需要根據鍵盤進行一些操作,例如按下Enter的時候提交表單,禁止用戶輸入某些特殊字符,設置快捷鍵等等。這時候需要找出用戶按下的是那些按鍵,每次都找對照表太麻煩了.so..寫了這么個小程序來測試按鍵。^_^
          其中的charCode是根據ascii表轉換的,不一定準確。

          keyCode :
          charCode :
          shift-key :
          ctrl-key :
          alt-key :

          下面是ascii編碼表:
          ascii編碼表
          源代碼:
          <script type="text/javascript">
          function showKey(e){
          = e || window.event;
          document.getElementById(
          "keyCode").value = e.keyCode;
          document.getElementById(
          "charCode").value = String.fromCharCode(e.keyCode);
          document.getElementById(
          "shiftKey").value = e.shiftKey;
          document.getElementById(
          "ctrlKey").value = e.ctrlKey;
          document.getElementById(
          "altKey").value = e.altKey;
          }
          $(document).ready(
          function(){
          document.onkeydown 
          = showKey;
          });
          </script>
          <br />
          <table>
              
          <tbody>
                  
          <tr>
                      
          <td>keyCode : </td>
                      
          <td><input id="keyCode" type="text" /> </td>
                  
          </tr>
                  
          <tr>
                      
          <td>charCode  : </td>
                      
          <td><input id="charCode" type="text" /> </td>
                  
          </tr>
                  
          <tr>
                      
          <td>shift-key : </td>
                      
          <td><input id="shiftKey" type="text" /> </td>
                  
          </tr>
                  
          <tr>
                      
          <td>ctrl-key : </td>
                      
          <td><input id="ctrlKey" type="text" /> </td>
                  
          </tr>
                  
          <tr>
                      
          <td>alt-key : </td>
                      
          <td><input id="altKey" type="text" /> </td>
                  
          </tr>
              
          </tbody>
          </table>
          其中我使用了jquery來初始化document的onkeydown事件。如果你不使用jquery可以用下面這段代碼
          window.onload = function(){
            document.onkeydown 
          = showKey;
          };

          代替
          $(document).ready(function(){
          document.onkeydown 
          = showKey;
          });

          posted on 2008-02-02 17:00 hunter129 閱讀(1618) 評論(5)  編輯  收藏 所屬分類: js

          評論

          # re: 測試你按下的是那個按鍵(附 ascii編碼表) 2008-02-29 11:39 CoderDream
          請把源代碼貼出來!  回復  更多評論
            

          # re: 測試你按下的是那個按鍵(附 ascii編碼表) 2008-03-04 13:37 落Nicety
          @CoderDream
          這位仁兄,其實你可以在頁面源代碼里面看到^_^
          不過為了方便 還是貼出來吧~~  回復  更多評論
            

          # re: 測試你按下的是那個按鍵(附 ascii編碼表) 2008-03-27 11:07 zzb
          好東西!!!  回復  更多評論
            

          # re: 測試你按下的是那個按鍵(附 ascii編碼表) 2008-04-04 14:38 落Nicety
          @zzb
          thanks~  回復  更多評論
            

          # re: 測試你按下的是那個按鍵(附 ascii編碼表) 2009-01-05 18:17 fdfd
          范德薩范德薩  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 京山县| 南靖县| 隆回县| 临海市| 黎川县| 沁阳市| 邵阳市| 亚东县| 邵阳县| 万盛区| 东明县| 云安县| 当涂县| 吴江市| 临洮县| 常德市| 宁武县| 英吉沙县| 剑河县| 山东省| 通海县| 苍南县| 察隅县| 正蓝旗| 清丰县| 长宁县| 三都| 张北县| 崇义县| 吴川市| 砚山县| 金川县| 开远市| 泸定县| 铜梁县| 济宁市| 肥东县| 喀什市| 重庆市| 文成县| 峨边|