[英]How to detect keyboard modifier (Ctrl or Shift) through JavaScript
I have a function which detect max length.我有一个检测最大长度的功能。 but the problem is that when the max length reached Ctrl+A combination does't work.
但问题是当最大长度达到 Ctrl+A 组合不起作用。 How can I detect Ctrl+A combination through javascript.
如何通过 javascript 检测 Ctrl+A 组合。
This is my maxlength code.这是我的最大长度代码。
if (event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 ){
return true;
} else {
if((t.length)>=50) {
return false;
}
}
Check event.ctrlKey
:检查
event.ctrlKey
:
function keyHandler(event) {
event = event || window.event;
if(event.keyCode==65 && event.ctrlKey) {
// ctrl+a was typed.
}
}
key codes:关键代码:
shift 16
ctrl 17
alt 18
your jQuery:你的jQuery:
$(document).keydown(function (e) {
if (e.keyCode == 18) {
alert("ALT was pressed");
}
});
You can use the following:您可以使用以下内容:
document.onkeypress = function(evt) { evt = evt || window.event; etv = evt; switch (etv.keyCode) { case 16: // Code to do when Shift presed console.log('Pressed [SHIFT]'); break; case 17: // Code to do when CTRL presed console.log('Pressed [CTRL]'); break; case 32: // Code to do when ALT presed console.log('Pressed [ALT]'); break; } };
I needed a solution for this too, so found some stuff that worked, cleaned it up to be a lot less code, and ES6... JSFiddle link我也需要一个解决方案,所以找到了一些有效的东西,清理它以减少代码,ES6 ... JSFiddle链接
function isCapsLock(event=window.event) { const code = event.charCode || event.keyCode; if (code > 64 && code < 91 && !event.shiftKey) { return true; } return false; } document.getElementById("text").addEventListener("keypress", event => { const status = document.getElementById("status"); if (isCapsLock(event)) { status.innerHTML = "CapsLocks enabled"; status.style.color = "red"; } else { status.innerHTML = "CapsLocks disabled"; status.style.color = "blue"; } }, false);
<input type="text" id="text" /><br> <span id="status"></span>
This is a very old question.这是一个非常古老的问题。 gilly3's answer is valid only if we have at hand an event object of type KeyboardEvent passed as a function argument.
仅当我们手头有一个作为函数参数传递的 KeyboardEvent 类型的事件对象时,gilly3 的答案才有效。 How to detect the current control key state if we have not event object available such as in this function?
如果我们没有可用的事件对象(例如在此函数中),如何检测当前的控制键状态?
function testModifierKey() {
// have I some modifier key hold down at this running time?
}
I found the solution after a long search from https://gist.github.com/spikebrehm/3747378 of spikebrehm.我从spikebrehm的https://gist.github.com/spikebrehm/3747378经过长时间的搜索后找到了解决方案。 his solution is tracing the modifier key state at any time using jQuery with a global variable.
他的解决方案是使用带有全局变量的 jQuery 随时跟踪修饰键状态。
The global variable window.modifierKey can be used in any circonstance without requiring event object.全局变量window.modifierKey可以在任何情况下使用而无需事件对象。
function testModifierKey() {
// have I have some modifier key hold down at this executing time?
if(window.modifierKey) {
console.log("Some modifier key among shift, ctrl, alt key is currently down.");
// do something at this condition... for example, delete item without confirmation.
} else {
console.log("No modifier key is currently down.");
// do something at other condition... for example, delete this item from shopping cart with confirmation.
}
}
Here is his script to load in your HTML document:这是他要加载到 HTML 文档中的脚本:
// source: https://gist.github.com/spikebrehm/3747378
// modifierKey used to check if cmd+click, shift+click, etc.
!function($, global){
var $doc = $(document);
var keys;
global.modifierKey = false;
global.keys = keys = {
'UP': 38,
'DOWN': 40,
'LEFT': 37,
'RIGHT': 39,
'RETURN': 13,
'ESCAPE': 27,
'BACKSPACE': 8,
'SPACE': 32
};
// borrowed from Galleria.js
var keyboard = {
map: {},
bound: false,
press: function(e) {
var key = e.keyCode || e.which;
if ( key in keyboard.map && typeof keyboard.map[key] === 'function' ) {
keyboard.map[key].call(self, e);
}
},
attach: function(map){
var key, up;
for(key in map) {
if (map.hasOwnProperty(key)) {
up = key.toUpperCase();
if (up in keyboard.keys) {
keyboard.map[keyboard.keys[up]] = map[key];
} else {
keyboard.map[up] = map[key];
}
}
}
if (!keyboard.bound) {
keyboard.bound = true;
$doc.bind('keydown', keyboard.press);
}
},
detach: function() {
keyboard.bound = false;
keyboard.map = {};
$doc.unbind('keydown', keyboard.press);
}
};
$doc.keydown(function(e) {
var key = e.keyCode || e.which;
if (key === 16 || key === 91 || key === 18 || key === 17) {
modifierKey = true;
} else {
modifierKey = false;
}
});
$doc.keyup(function(e) {
modifierKey = false;
});
}(jQuery, window);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.