admin管理员组文章数量:1291062
I am working with ctrl+c and ctrl+v event in javascript i want to bind a function on ctrl+v event. and i am able to do it with event.keyCode in windows system but in mac os on mand press i am not able to figure out the event. My code is
$('.hindi_content').keyup(function(event){
console.log('UP'+event.keyCode);
console.log('in window::'+ event.ctrlKey+'in mac os'+event.metaKey+'####'+event.META_MASK+'##$&&'+event.CTRL_MASK);
// this is working with windows not with mac os.
if(event.keyCode==86 && event.ctrlKey)
{
console.log('ctrl press'+event.ctrlKey);
col_val = $('#'+this.id).val();
console.log('col val'+col_val);
$('#hidden_'+this.id).val(col_val);
console.log('hidden val'+ $('#hidden_'+this.id).val());
//converter_new(event,this.lang);
// return;
}
});
i search and found event.metaKey but it is for ctrl key in mac i just want mand key in mac os.
I am working with ctrl+c and ctrl+v event in javascript i want to bind a function on ctrl+v event. and i am able to do it with event.keyCode in windows system but in mac os on mand press i am not able to figure out the event. My code is
$('.hindi_content').keyup(function(event){
console.log('UP'+event.keyCode);
console.log('in window::'+ event.ctrlKey+'in mac os'+event.metaKey+'####'+event.META_MASK+'##$&&'+event.CTRL_MASK);
// this is working with windows not with mac os.
if(event.keyCode==86 && event.ctrlKey)
{
console.log('ctrl press'+event.ctrlKey);
col_val = $('#'+this.id).val();
console.log('col val'+col_val);
$('#hidden_'+this.id).val(col_val);
console.log('hidden val'+ $('#hidden_'+this.id).val());
//converter_new(event,this.lang);
// return;
}
});
i search and found event.metaKey but it is for ctrl key in mac i just want mand key in mac os.
Share Improve this question asked Aug 22, 2013 at 6:44 Sandy JainSandy Jain 911 silver badge8 bronze badges3 Answers
Reset to default 5Things seem to have gotten easier since this question was first asked. I found this answer which states that event.metaKey
will work for cmd
on mac. I just tested it and it works fine.
document.body.addEventListener("keydown", function(event) {
var key = event.key;
var cmd_held = event.metaKey;
if(cmd_held && key.toLowerCase() == "v")
pasta();
});
mousetrap is a library that makes those things really easy:
http://craig.is/killing/mice
//control + v
Mousetrap.bind('ctrl+v', function(e) {
//do things here
});
//mand + k & control +v
Mousetrap.bind(['mand+v', 'ctrl+v'], function(e) {
//do things here
});
Live demo
Individual detect<br>
<input id="myinput" placeholder="Click mand key on your mac"><br>
<div class="output" id="output"></div><br><br>
right or left detect<br>
<input id="myinput2" placeholder="Click mand key on your mac"><br><br>
<div class="output" id="output2"></div>
<script type="text/javascript">
$(document).on('keydown', '#myinput', function() {
if (event.keyCode == '91') {
$("#output").empty().append("left mand clicked");
}
if (event.keyCode == '93') {
$("#output").empty().append("right mand clicked");
}
});
$(document).on('keydown', '#myinput2', function() {
if (event.keyCode == '91' || event.keyCode == '93') {
$("#output2").empty().append("right or left mand clicked");
}
});
$("input").on('blur', function() {
$(".output").empty();
});
</script>
本文标签: How to detect command key is pressed in javascript at mac osStack Overflow
版权声明:本文标题:How to detect command key is pressed in javascript at mac os - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741499205a2381978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论