按 Ctrl + F 将焦点更改为输入和显示搜索

Pushing Ctrl + F to change focus to input and display search

我有以下 Javascript/jQuery 专注于输入字段的代码。

window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) { 
        e.preventDefault();
        $('#search').focus();
    }
})

但是我想这样做,如果用户在搜索字段中获得焦点后再次按 Ctrl+F,它将打开默认的浏览器搜索 window。

我的想法是将代码更改为:

window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)){
        if($('#search').not(":focus")) {
            e.preventDefault();
            console.log("Search is not in focus");
            $('#search').focus();
        } else {
            console.log("Default action of CtrlF")
            return true;
        }
    }
})

但这没有用(它总是认为搜索不是焦点,尽管它是焦点)

试试这个

$(document).ready(function(){
 
  window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)){
        if($('#search').is(":focus")) {
            console.log("Default action of CtrlF")
            return true;
        } else {
          
            e.preventDefault();
            console.log("Search is not in focus");
            $('#search').focus();
        }
    }
})
  

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input type="search" id="search" />