大网数据广告活动
网站运营与故障问答

Track visitor use of "Ctrl + F" on a website?

· 52CE 我爱测

Would it theoretically be possible to detect (e.g. via JavaScript) if a user queries a webpage with the Windows shortcut Ctrl + F, or the Mac-equivalent Command + F in their browser? The first step would be to detect whether that…

问题原文

原帖链接 · Baumr · CC BY-SA 3.0

Would it theoretically be possible to detect (e.g. via JavaScript) if a user queries a webpage with the Windows shortcut Ctrl + F, or the Mac-equivalent Command + F in their browser?

The first step would be to detect whether that function was called, but it'd be a different ball game to track their keystrokes...


I'm asking because a lot of the time, web apps seem to re-wire keyboard shortcuts to do other things (or they get disabled completely, for example, in some cases of HTML5 canvas, the shortcut Command + W to close the tab doesn't work). This is likely a different mechanism, and I don't have high hopes. Furthermore, only about 10% of users know how to use the Ctrl + F function anyway.

已采纳解答

原帖链接 · RTOSkit · CC BY-SA 3.0

If you want capture the event keyboard Ctrl + F and continue with the opening of the standard searchbox of the browser, you could do this:

var KEY_CTRL = 17;
var KEY_f = 70;

var isReserved = false;
var useExclusiveKeyEvent = false;
var isNewSearchEvent = false;


/*THIS VARIABLE IS RENDERED BY SERVER SIDE*/
var clientRandomKey = "78349DE472AA30032DE2DF344FF28374";

window.onkeyup=function(ev){
  if(ev.which == KEY_CTRL){
    isReserved=false;
  }
}
window.onkeydown=function(ev){
  //!IMPORTANT NOT MAKE UI DELAY IN THIS FUCTION
  if(ev.which == KEY_CTRL) {
       isReserved=true;
  }
    if((isReserved==true)&&(ev.which == KEY_f)) {       
    var currentDate = new Date();
    console.log( "INTO EVENT FUNCTION >\n   "+ currentDate+ " \n   ID: "+clientRandomKey+"\n   launch search a word");
    isNewSearchEvent=true;
    if(useExclusiveKeyEvent){
      return false;}
    }
}

window.setInterval(function(){            
    if(isNewSearchEvent){
      isNewSearchEvent=false;
      //TODO ajax send code
      var currentDate = new Date();
      var msg = "OUTSIDE EVENT FUNCTION >\n   " + currentDate+ " \n   ID: "+clientRandomKey+"\n   launch search a word"
      console.log(msg);
      alert(msg);
    }
},2500);

This is a live example.

As you can see in the example, when the user presses the right keyboard combination, event function sends a "log string" in the console ("EVENT INTO FUNCTION"), while changing the state of a flag(isNewSearchEvent).

This flag is evaluated in a timer to send another "log string" in the console ("OUTSIDE EVENT FUNCTION")

and open an alert with the same message.

This double passage is necessary because cannot create breaks into the event function, that should listen in a correct speed of capture.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2013-01-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。