簡體   English   中英

按下%后如何檢測所有按鍵

[英]How to detect all keypresses after pressing a %

在jQuery中按下“%”后是否可以檢測按鍵? 我想要的是,在用戶按下“%”之后,腳本將在此之后開始檢測所有按鍵,在列表中搜索匹配項,並在用戶單擊該列表中的列表項時停止。

 $(iframeDocument).find('body').on('keypress', function(event) {
    if(event.which === 37) {
      $('#tokens-menu dd').trigger("click");
    } else {
      // search through a list
      $(iframeDocument).find('body').handleBackspace(event); 
    }
  });

在檢測到%時設置一個變量,然后在此之后處理所有按鍵。 當他們單擊按鈕時,清除變量。

 var percent_pressed = false; $(window).on('keypress', function() { var key = event.which; if (percent_pressed) { // use this keypress $("#output").append(String.fromCharCode(key)); } else if (key == 37) { percent_pressed = true; $("#output").empty(); } }); $("#tokens-menu").click(function() { percent_pressed = false; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h4>Type % to start appending to output</h4> Output: <span id="output"></span> <br> <button id="tokens-menu">Click to restart</button> 

這是一個根據您的描述的示例:

$(document).on('keyup', function (e) {
    var c = String.fromCharCode(e.which) // or e.keyCode;
    if (logging) {
        rec.push(c);
    }

    // start logging if '%' pressed
    if (e.shiftKey && e.which == 53) { 
        logging = true;
    }
});

看到它在這里運行: https : //jsfiddle.net/ddan/kuotty5v/

按“%”后,它將開始記錄您輸入的內容。 您可以單擊“顯示已記錄”按鈕以查找到目前為止已記錄的內容。 我希望它可以幫助您構建解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM