I'm trying to implement a feature to focus "Search"
field if typed anywhere in the "body"
. Things are kind of working fine as you see form jsfiddle demo. But in MAC Safari
maching if i type "Command + C"
it will focus to the search field which disable copying text. Where as in Windows
"Ctrl + C"
works fine.
How can we avoid "Command + C"
focusing on "Search"
field
Tested with Chrome on Mac, works fine. On safari, console.log("keyCode:"+e.keyCode) log 99 when key 'c' pressed: Try following code:
https://jsfiddle.net/pengyanb/s8w5do2q/1/
jQuery(document).ready(function($){
$("body").keypress(function(e) {
var nodeName = e.target.nodeName,
charCode = e.which || e.keyCode,
charStr = String.fromCharCode(charCode);
console.log("keyCode:"+e.keyCode);
if ((e.keyCode == 67 || e.keyCode==99) && (e.ctrlKey || e.metaKey)){
console.log("keydown-Copy");
return;
}
console.log(e.keyCode,"keypress");;
if ( nodeName == 'INPUT' || nodeName == 'TEXTAREA' ) {
return;
}
if (!(/[a-z0-9]/i.test(charStr))) {
return;
}
$("#search").focus();
});
});
<div class="searchbg">
<div>
Search : <input type="text" id="search" />
</div>
<div>
<div>
Name : <input type="text" id="name" />
</div>
<div>
Address : <textarea name="address"></textarea>
</div>
Try Copy Me
</div>
</div>