[英]How to detect command key is pressed in javascript at mac os
I am working with ctrl+c and ctrl+v event in javascript i want to bind a function on ctrl+v event. 我在javascript中使用ctrl + c和ctrl + v事件我想在ctrl + v事件上绑定一个函数。 and i am able to do it with event.keyCode in windows system but in mac os on command press i am not able to figure out the event. 我能够在Windows系统中使用event.keyCode,但在命令按下的mac os中,我无法弄清楚事件。 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 command key in mac os. 我搜索并找到了event.metaKey,但它是用于mac中的ctrl键我只想在mac os中使用命令键。
Things 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. 我发现这个答案表明event.metaKey
适用于mac上的cmd
。 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: mousetrap是一个让这些东西变得非常简单的库:
http://craig.is/killing/mice http://craig.is/killing/mice
//control + v
Mousetrap.bind('ctrl+v', function(e) {
//do things here
});
//command + k & control +v
Mousetrap.bind(['command+v', 'ctrl+v'], function(e) {
//do things here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.