jQuery捕获CTRL+;S和Command+;S(Mac)
我知道这个问题在stackoverflow上被问了好几次,我尝试了一些不同的解决方案,但找不到适合我的代码 我想在Mac上捕获键盘命令Ctrl+S和command+S,目前我使用以下代码(使用jQuery 2.1.0): 此代码在以下情况下运行良好:jQuery捕获CTRL+;S和Command+;S(Mac),jquery,macos,google-chrome,keyboard-shortcuts,keypress,Jquery,Macos,Google Chrome,Keyboard Shortcuts,Keypress,我知道这个问题在stackoverflow上被问了好几次,我尝试了一些不同的解决方案,但找不到适合我的代码 我想在Mac上捕获键盘命令Ctrl+S和command+S,目前我使用以下代码(使用jQuery 2.1.0): 此代码在以下情况下运行良好: Safari:使用Ctrl+S和Command+S Firefox:使用Ctrl+S和Command+S Chrome:只有Ctrl+S有效 你会发现问题出在Google Chrome上,这里我只能捕获Ctrl+S 有人知道我如何解决这个问题吗
- Safari:使用Ctrl+S和Command+S
- Firefox:使用Ctrl+S和Command+S
- Chrome:只有Ctrl+S有效
有人知道我如何解决这个问题吗?这段代码适合我。我在Chrome(v33)、Firefox(v24)和Safari(v6.1.1)中测试了它。Control+S和Command+S都可以工作 请注意,我使用的是
keydown
,而不是keypress
。在jQuery中,它们表示:
注:由于按键事件未被任何官方报道
规范,使用时遇到的实际行为可能
不同的浏览器、浏览器版本和平台会有所不同
我会避免使用它
jQuery(window).on('keypress', function(event){
if (!(event.which == 115 && (event.ctrlKey||event.metaKey)) && !(event.which == 19)) return true;
// my save function
event.preventDefault();
return false;
});
$(document).keydown(function(event) {
// If Control or Command key is pressed and the S key is pressed
// run save function. 83 is the key code for S.
if((event.ctrlKey || event.metaKey) && event.which == 83) {
// Save Function
event.preventDefault();
return false;
};
}
);