मैं केवल जावास्क्रिप्ट कोर का उपयोग करके इस तरह की समस्या को हल करने में कामयाब रहा। कोड दो हिस्सों, परिभाषा और कार्यान्वयन में विभाजित है लेकिन यदि आवश्यक हो तो भागों को विलय किया जा सकता है।
परिभाषा
/**
* addKeyActions takes an array of objects that looks like this:
*
* {
* keyCodes: [120], //F9
* modifiers: ['ctrlKey', 'shiftKey', 'altKey', 'altGraphKey', 'metaKey'], //Only legal modifiers
* callback: function(event){};
* }
*
* A good resource to find the keycodes you are looking for is:
* http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
*
* However, there are more keycodes than written on this page, so if you can't find what you are looking for,
* I have provided a function that you can call to display the keycode for the current button pressed - alertKeyCodes
*
*
* @Version 1.01
*/
/*
* --HOW TO USE--
*
var allModifiersKeys = {
keyCodes: [120, 121], //F9, F10
modifiers: ['ctrlKey', 'shiftKey', 'altKey'],
callback: function(event){alert("All modifiers pressed + Function-button!");}
};
var altKeys = {
keyCodes: [122, 123], //F11, F12
modifiers: ['altKey'],
callback: function(event){alert("ALT pressed + Function-button!");}
};
var shiftCtrlKeys = {
keyCodes: [116, 117], //F5, F6
modifiers: ['ctrlKey', 'shiftKey'],
callback: function(event){alert("CTRL+SHIFT+Function-button pressed!");}
};
var noModifierKeys = {
keyCodes: [118, 119], //F7, F8
callback: function(event){preventDefaultEventPropagation(); alert("Function-button!");} //Do not forget to also declare the preventDefaultEventPropagation-function
//that is listed below for this example to work
};
addKeyActions([allModifiersKeys, altKeys, shiftCtrlKeys, noModifierKeys]);
*/
function addKeyActions(key){
document.onkeydown = function(){
var matchingKeyCode = findMatchingKeyCode();
var match = matchModifiers(matchingKeyCode);
if(match){
matchingKeyCode.callback(event);
}
};
function findMatchingKeyCode(){
var eventKeyCode = event.keyCode || event.which || event.charCode;
var returnVal;
for(var i=0; i<key.length; i++){
var tempKey = key[i];
var containsKeyCode = isValueInArray(eventKeyCode, tempKey.keyCodes);
if(containsKeyCode){
returnVal = tempKey;
}
}
return returnVal;
}
function isValueInArray(value, array){
var hasValue = false;
for(var i=0; i < array.length; i++){
var tempValue = array[i];
if(tempValue === value){
hasValue = true;
}
}
return hasValue;
}
function matchModifiers(key){
var match = false;
if(key != undefined){
match = true;
if(key.modifiers instanceof Array){
for(var i=0; i<key.modifiers.length; i++){
var modifier = key.modifiers[i];
match = match && event[modifier];
}
}
}
return match;
}
}
/**
* Useful callbacks/functions are listed below
*/
function preventDefaultEventPropagation(){
if(event.cancelBubble) event.cancelBubble = true;
if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
event.keyCode = 0;
return false;
}
/**
* Useful methods during developement
*/
function alertKeyCodes(){
document.onkeydown = function(){
alert(event.keyCode || event.which || event.charCode);
if(event.cancelBubble) event.cancelBubble = true;
if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
event.keyCode = 0;
return false;
};
}
कार्यान्वयन
$(document).ready(function() {
var SingleKeys = {
keyCodes: [114, 116, 121, 122], //F3, F5, F10, F11
callback: function(event){return preventDefaultEventPropagation();}
};
var BackspaceKeys = {
keyCodes: [8], //Backspace
callback: function(event){
var sourceOfEvent = event.target || event.srcElement;
if(sourceOfEvent.tagName == "BODY"){
return preventDefaultEventPropagation();
}
}
};
var EnterKeys = {
keyCodes: [13], //Enter
callback: function(event){
var sourceOfEvent = event.target || event.srcElement;
if(sourceOfEvent.tagName == "BODY"){
return preventDefaultEventPropagation();
}
if(sourceOfEvent.tagName == "INPUT" && sourceOfEvent.type == "text"){
return preventDefaultEventPropagation();
}
}
};
var AltKeys = {
keyCodes: [36, 37, 39], //Home, Left, Right
modifiers: ['altKey'],
callback: function(event){return preventDefaultEventPropagation();}
};
var CtrlKeys = {
keyCodes: [66, 68, 69, 72, 73, 76, 78, 79, 82], //B, D, E, H, I, L, N, O, R
modifiers: ['ctrlKey'],
callback: function(event){return preventDefaultEventPropagation();}
};
addKeyActions([SingleKeys, BackspaceKeys, EnterKeys, AltKeys,CtrlKeys]);
});
बस कीकोड आप रोकना और कॉलबैक संपादित करना चाहते हैं जोड़ने तदनुसार
तुम सिर्फ एक रूप reposting को रोकने के लिए करना चाहते हैं? –
क्या ऐसा करने के लिए कोई अच्छा कारण है? मैं शायद एक ऐसी साइट से बहुत नाराज हो जाऊंगा जो उस तरह की मूल ब्राउज़र कार्यक्षमता को ओवरराइड करता है। – Spudley
ऐसा करने के बहुत अच्छे कारण हैं। डेस्कटॉप-आधारित टाइम शीट डेटा एंट्री सिस्टम पर विचार करें जहां सैकड़ों उपयोगकर्ताओं को कुछ गर्म कुंजी का उपयोग करने के लिए प्रशिक्षित किया गया है। आप एप्लिकेशन को एक वेब-आधारित एप्लिकेशन में माइग्रेट करना चाहते हैं जिसमें डेस्कटॉप एप्लिकेशन के लिए बाधाएं हैं। सही या गलत के लिए, यदि वह प्रबंधन का अंतिम निर्णय है, तो फ़ंक्शन कुंजियों के लिए डिफ़ॉल्ट व्यवहार को ओवरराइड करने का एक तरीका ढूंढना अनिवार्य है। ऐसे कई अन्य परिदृश्य हैं जहां आप डिफ़ॉल्ट कीबोर्ड व्यवहार को ओवरराइड करना चाहते हैं, उनमें से अधिकतर मैं आंतरिक वेब अनुप्रयोगों के लिए कल्पना करना चाहता हूं। –