使用javascript为web应用程序自定义键盘快捷键



我正在学习java脚本。我想为我的网络应用程序使用键盘快捷键。因此,我需要使用java脚本在asp.net中为我的应用程序创建自定义键盘快捷键。我没有找到一个合适的方法来做这件事。我想用纯java脚本或j查询来做。创建自定义密钥的最佳方法是什么??请帮我解决这个问题。

如果有任何小提琴或任何现成的代码来了解它的确切工作原理,那将更有帮助。此外,我可能需要这些键来创建动态数据并连接到数据库。在java脚本中创建还可以连接到数据库的密钥的最佳方法是什么??

您可以使用类似的方法来确定按下了哪个键:

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

然后按键Action():

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;
    }
}

使用window.onkeyup()侦听器。接受一个带有参数的函数,该参数将"捕获"事件触发器。在我们的例子中,这将是您的密钥,由其ascii代码表示。

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

在JSFiddle上查看:https://jsfiddle.net/w0qafmLy/

干杯!

这是我制作的一些键盘快捷键!

<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>

或者我做了这个JSFiddle也检查一下

我建议您签出Keystrokes。我写它是为了让这类事情变得容易。

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"'))

最新更新