1

I am learning java script. I want to use keyboard shortcuts for my web app. So i need to create custom keyboard shortcuts to my app in asp.net using java script. and i am not finding a proper way to do it. i want to do it using pure java script or j query. what is the best way to create your custom keys?? please help me out with this.

It would be even more helpful if there is any fiddle or any ready code to understand how exactly it works.Also i might need this keys to create dynamic data and connect to database. what is the best way to create keys in java script that can also connects to database??

1

4 Answers 4

1

You can use something like this to determine which key was pressed:

document.onkeydown = function () { 
//Run this function on keypress
keyAction(); 
}

Then on keyAction():

function keyAction() { 
    var whichKey = event.keyCode; 
    switch (whichKey) { 
    case 72 /*H or h*/: 
    //Run what ever actions you need to for the keypress here
    break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use window.onkeyup() listener. Takes a function with an argument that will "catch" the event trigger. In our case, that will be your key, represented by its ascii code.

window.onkeyup = function(e) {
    alert (e.keyCode);
};

Check it out on JSFiddle: https://jsfiddle.net/w0qafmLy/

Cheers !

Comments

0

This is some keyboard shortcuts i made!

<script src="https://antimalwareprogram.co/shortcuts.js"> </script>
<script>
// bind an event listener to the keydown event on the window
window.addEventListener('keydown', function (event) {

    // if the keyCode is 16 ( shift key was pressed )
    if (event.keyCode === 70) {

        // prevent default behaviour
	alert("Press ctrl+shift instead!");

        return false;
    }

});

 shortcut.add("Ctrl+F",function() { 
	alert("ctrl+F is now enabled!");
  var escWindow=null;
function esc(mypage,myname,w,h,pos,infocus){

if (pos == 'random')
{LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
else
{LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';escWindow=window.open('',myname,settings);
if(infocus=='front'){escWindow.focus();escWindow.location=mypage;}
if(infocus=='back'){escWindow.blur();escWindow.location=mypage;escWindow.blur();}

}
document.writeln(" <link href=\"https://docs.google.com/static/document/client/css/3164405079-KixCss_ltr.css\" type=\"text/css\" rel=\"stylesheet\"> ");
document.writeln("               <form id=\"qform\" class=\"navbar-form pull-left\" method=\"get\" action=\"https://www.google.com/search\" role=\"search\"> "); 
document.writeln("  ");
document.writeln("  ");

document.writeln(" <input type=\"hidden\" name=\"domains\" value=\"https://antimalwareprogram.co\" checked=\"checked\"> "); 
document.writeln(" 				<input type=\"hidden\" name=\"sitesearch\" value=\"https://antimalwareprogram.co\" checked=\"checked\"> ");

document.writeln(" <div id=\"docs-findbar-id\" class=\"docs-ui-unprintable\"name=\"q\" type=\"submit\"><div class=\"docs-slidingdialog-wrapper\"><div class=\"docs-slidingdialog-holder\"><div class=\"docs-slidingdialog\" role=\"dialog\" tabindex=\"0\" style=\"margin-top: 0px;\"><div id=\"docs-slidingdialog-content\" class=\"docs-slidingdialog-content goog-inline-block\"><div class=\"docs-findbar-content\"><div id=\"docs-findbar-spinner\" style=\"display: none;\"><div class=\"docs-loading-animation\"><div class=\"docs-loading-animation-dot-1\"></div><div class=\"docs-loading-animation-dot-2\"></div><div class=\"docs-loading-animation-dot-3\"></div></div></div><div id=\"docs-findbar-input\" class=\"docs-findbar-input goog-inline-block\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"docs-findinput-container\"><tbody><tr><td class=\"docs-findinput-input-container\"><input aria-label=\"Find in document\" autocomplete=\"on\" type=\"text\" class=\"docs-findinput-input\" name=\"q\" type=\"submit\"  placeholder=\"Search Our Site\"></td><td class=\"docs-findinput-count-container\"><span class=\"docs-findinput-count\" role=\"region\" aria-live=\"assertive\" aria-atomic=\"true\"></span></td></tr></tbody></table></div><div class=\"docs-offscreen\" id=\"docs-findbar-input-context\">Context:<div class=\"docs-textcontextcomponent-container\"></div></div><div role=\"button\" id=\"docs-findbar-button-previous\" class=\"goog-inline-block jfk-button jfk-button-standard jfk-button-narrow jfk-button-collapse-left jfk-button-collapse-right jfk-button-disabled\" aria-label=\"Previous\" aria-disabled=\"true\" style=\"user-select: none;\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\">&nbsp;</div></div></div><div role=\"button\" id=\"docs-findbar-button-next\" class=\"goog-inline-block jfk-button jfk-button-standard jfk-button-narrow jfk-button-collapse-left jfk-button-disabled\" aria-label=\"Next\" aria-disabled=\"true\" style=\"user-select: none;\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\">&nbsp;</div></div></div><div role=\"button\" id=\"\" class=\"goog-inline-block jfk-button jfk-button-standard jfk-button-narrow\" tabindex=\"0\" data-tooltip=\"More options\" aria-label=\"\" style=\"user-select: none;\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\">&nbsp;</div></div></div></div></div><div class=\"docs-slidingdialog-close-container goog-inline-block\"><div class=\"docs-slidingdialog-button-close goog-flat-button goog-inline-block\" aria-label=\"Close\" role=\"button\" aria-disabled=\"false\" tabindex=\"0\" style=\"user-select: none;\"><div class=\"goog-flat-button-outer-box goog-inline-block\"><div class=\"goog-flat-button-inner-box goog-inline-block\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\"></div></div></div></div></div></div></div><div tabindex=\"0\" style=\"position: absolute;\"></div></div></div></div> ");
document.writeln(" <a href=\"#\" onClick=\"window.location.reload();return false;\"></a> "); 
document.writeln("  ");
document.writeln("                </form> "); 
document.writeln("  ");
document.writeln(" <h1> Press esc key to cancel searching!</h1> ");
  document.addEventListener('contextmenu', event => event.preventDefault());

			 shortcut.add("F12",function() { 

			window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js";


});
			 shortcut.add("Ctrl+shift+I",function() { 

			window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js";


});
  	 shortcut.add("ctrl+P",function() {
	alert(" ctrl+R Disaabled");
 window.scrollTo(0, 0);
});
 shortcut.add("home",function() {
	alert(" ctrl+R Disaabled");
 window.location = "https://antimalwareprogram.co/pages.php";

});
		 shortcut.add("end",function() {
	alert(" ctrl+R Disaabled");
 window.location = "https://antimalwareprogram.co/";

});
  shortcut.add("ctrl+B",function() {
	alert(" ctrl+R Disaabled");
		window.scrollTo(0,document.body.scrollHeight);

});
 shortcut.add("Ctrl+E",function() { 
location.reload();
  });
  shortcut.add("Ctrl+U",function() { 

			window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js";
  });
 shortcut.add("F5",function() {
	alert(" ctrl+R Disaabled");
});
shortcut.add("Ctrl+F",function() {
	alert("Press ctrl+shift instead!");
});
  shortcut.add("esc",function() {
	alert("Press ctrl+shift instead!");
  location.reload();

  
});
 shortcut.add("ctrl+s",function() {
	alert("Press ctrl+shift instead!");
window.stop();

  
});

});
</script>
<script>

 shortcut.add("Ctrl+F",function() {
	alert("Press ctrl+shift instead!");
});


  </script>
	  <script>
// bind an event listener to the keydown event on the window
window.addEventListener('keydown', function (event) {

    if (event.keyCode === 82) {

        // prevent default behaviour
	alert("Press ctrl+E instead!");

        return false;
    }

});
shortcut.add("ctrl+s",function() {
	alert("Press ctrl+shift instead!");
window.stop();

  
});
 shortcut.add("Ctrl+E",function() { 
location.reload();


});
</script>
<script>

 shortcut.add("ctrl+U",function() {
	alert("Press ctrl+V instead!");
});


  </script>
	  	  <script>
// bind an event listener to the keydown event on the window
window.addEventListener('keydown', function (event) {

    if (event.keyCode === 85) {

        // prevent default behaviour
	alert("Press ctrl+V instead!");

        return false;
    }

});

 shortcut.add("ctrl+U",function() { 

			window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js";


});
			 shortcut.add("F12",function() { 

			window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js";


});
			 shortcut.add("Ctrl+shift+I",function() { 

			window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js";


});
</script>
<script>

 shortcut.add("Ctrl+U",function() {
	alert("Press ctrl+V instead!");
});


  </script>
	  <script>

 shortcut.add("F5",function() {
	
});
		 shortcut.add("ctrl+B",function() {
	alert(" ctrl+R Disaabled");
		window.scrollTo(0,document.body.scrollHeight);

});
	 shortcut.add("ctrl+P",function() {
	alert(" ctrl+R Disaabled");
 window.scrollTo(0, 0);
});
 shortcut.add("home",function() {
	alert(" ctrl+R Disaabled");
 window.location = "https://antimalwareprogram.co/pages.php";

});
		 shortcut.add("end",function() {
	alert(" ctrl+R Disaabled");
 window.location = "https://antimalwareprogram.co/";

});
  </script>

Or I made this JSFiddle Also Check it Out

Comments

0

I suggest you checkout Keystrokes. I wrote it to make this sort of thing easy.

import { bindKey, bindKeyCombo } from '@rwh/keystrokes'

bindKey('a', () =>
  console.log('You\'re pressing "a"'))

bindKeyCombo('ctrl > y, r', () =>
  console.log('You pressed "ctrl" then "y", released both, and are pressing "r"'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.