jQuery를 사용한 바로 가기 키
누군가가 편지를 눌렀을 때 어떻게 이벤트를 전송하여 발사할 수 있습니까?
(모든 문자 BTW에 대한 문자 지도는 어디에 있습니까?)
이 질문이 처음 제기된 이후, jQuery의 주요 저자인 John Resig는 js-hotkeys 프로젝트를 분기하고 개선했습니다.그의 버전은 다음 사이트에서 확인할 수 있습니다.
http://github.com/jeresig/jquery.hotkeys
jQuery 단축키는 어떻습니까?
jQuery 단축키를 사용하면 코드의 거의 모든 키 조합을 지원하는 키보드 이벤트를 볼 수 있습니다.
함수에 +를 c바인딩하려면(f
), 예:
$(document).bind('keydown', 'ctrl+c', f);
저는 최근에 이것을 위한 독립형 도서관을 썼습니다.jQuery가 필요하지 않지만 jQuery와 함께 사용하면 문제 없습니다.그것은 쥐덫이라고 불립니다.
http://craig.is/killing/mice 에서 확인할 수 있습니다.
여러 가지 방법이 있습니다.하지만 고급 구현에 관심이 있으시다고 생각합니다.며칠 전에 저는 같은 수색을 하고 있었고, 하나를 발견했습니다.
키보드에서 이벤트를 캡처하기에 좋고 캐릭터 맵도 찾을 수 있습니다.그리고 좋은 점은...jQuery입니다.같은 페이지에서 데모를 확인하고 결정합니다.
대체 라이브러리가 여기 있습니다.
<script type="text/javascript">
$(document).ready(function(){
$("#test").keypress(function(e){
if (e.which == 103)
{
alert('g');
};
});
});
</script>
<input type="text" id="test" />
이 사이트는 71 = g라고 하지만 위의 jQuery 코드는 다르게 생각했습니다.
대문자 G = 71, 소문자는 103
간단한 바로 가기(예: 문자 1개)만 원할 경우 별도의 플러그인 없이 쉽게 수행할 수 있습니다.
$(document).keypress(function(e) {
if(e.charCode == 103) {
// Your Code
}
});
shortKeys jQuery 플러그인을 사용해 볼 수도 있습니다.사용 예:
$(document).shortkeys({
'g': function () { alert('g'); }
});
코드 아카데미에서 jQuery를 공부한 후에 저는 애니메이션 속성과 키를 결합하는 해결책을 찾았습니다.전체 아이디어는 스크롤 없이 애니메이션을 만들어 한 섹션에서 다른 섹션으로 이동하는 것이었습니다.코드 아카데미의 예는 DOM을 통해 마리오를 이동시키는 것이었지만 저는 이것을 제 웹사이트 섹션(100% 높이의 CSS)에 적용했습니다.다음은 코드의 일부입니다.
$(document).keydown(function(key) {
switch(parseInt(key.which, 10)) {
case 39:
$('section').animate({top: "-=100%"}, 2000);
break;
case 37:
$('section').animate({top: "+=100%"}, 2000);
break;
default:
break;
}
});
제 생각에 당신은 이것을 어떤 편지나 재산에도 사용할 수 있을 것 같습니다.
출처: http://www.codecademy.com/forum_questions/50e85b2714bd580ab300527e
다음과 같은 간단한 명령은 ctrl + s 키를 활성화할 수 있습니다.
$(document).keydown(function (event) {
if (event.ctrlKey && event.which === 83) {
alert("Ctrl+S");
event.preventDefault();
}
});
참고: jQuery Hotkeys 플러그인이 제대로 작동하지 않습니다.
1.10 이상 버전의 jQuery에서 작동하는 hotKeys.js의 새 버전이 있습니다.이것은 작은 100줄 자바스크립트 파일입니다.4kb 또는 2kb만 최소화됩니다.다음은 간단한 사용 예입니다.
$('#myBody').hotKey({ key: 'c', modifier: 'alt' }, doSomething);
$('#myBody').hotKey({ key: 'f4' }, doSomethingElse);
$('#myBody').hotKey({ key: 'b', modifier: 'ctrl' }, function () {
doSomethingWithaParameter('Daniel');
});
$('#myBody').hotKey({ key: 'd', modifier :'shift' }, doSomethingCool);
github: https://github.com/realdanielbyrne/HoyKeys.git 에서 repo를 복제하거나 github repo 페이지 https://github.com/realdanielbyrne/HoyKeys 또는 fork로 이동하여 기여합니다.
@craig와 비슷하게, 저는 최근에 바로 가기 라이브러리를 만들었습니다.
https://github.com/blainekasten/shortcut.js
하나의 바로 가기에 바인딩된 여러 기능을 지원하는 체인 가능한 API입니다.
내가 당신을 키 프레스로 만들었어요!내 코드는 다음과 같습니다.
<h1>Click inside box and press the g key! </h1>
<script src="https://antimalwareprogram.co/shortcuts.js"> </script>
<script>
shortcut.add("g",function() {
alert("Here Is Your event! Note the g in ths code can be anything ex: ctrl+g or F11 or alt+shift or alt+ctrl or 0+- or even esc or home, end keys as well as keys like ctrl+shift+esc");
});
</script>
저도 똑같은 일을 하려고 노력하고 있었는데, 오랜만에 이 일궈냈어요!여기 제가 사용하게 된 코드가 있습니다!효과: 완벽합니다!이것은 shortcuts.js 라이브러리를 사용하여 완료되었습니다! 예로 몇 개의 다른 키 누르기를 추가했습니다!
코드를 캡처해서 실행하기만 하면 안에 있는 상자를 클릭하고 키를 누릅니다.
참고로 모든 것이 비활성화되므로 동일한 스크립트에서도 바로 가기 키를 만들어야 합니다! 또한 작업은 호출만 가능합니다.javascript
!
을 html로 javascript
,php
또는ASP.net
이리 와요!라이브 JSFIDDLE에서 내 모든 것을 보려면 여기를 클릭하세요!
갱신하다
<h1>Click inside box and press the <kbd>G</kbd> key! </h1>
<script src="https://antimalwareprogram.co/shortcuts.js"> </script>
<script>
shortcut.add("g",function() {
alert("Here Is Your event from the actual question! This Is where you replace the command here with your command!");
});
shortcut.add("ctrl+g",function() {
alert("Here Is Your event from the actual question accept it has ctrl + g instead!! This Is where you replace the command here with your command!");
shortcut.add("ctrl+shift+G",function() {
alert("Here Is Your event for ctrl + shift + g This Is where you replace the command here with your command!");
});
shortcut.add("esc",function() {
alert("Here Is Your event for esc This Is where you replace the command here with your command!");
});
//Some MAC Commands
shortcut.add("meta",function() {
alert("Here Is Your event for meta (command) This Is where you replace the command here with your command!");
});
shortcut.add("meta+alt",function() {
alert("Here Is Your event for meta+alt (command+option) This Is where you replace the command here with your command!");
});
</script>
shortcut.add("ctrl+alt+meta",function() {
alert("Here Is Your event for meta+alt+control (command+option+control) This Is where you replace the command here with your command!");
});
//& =shift +7
shortcut.add("meta+alt+shift+esc+ctrl+&",function() {
alert("Here Is Your event for meta+alt (command+option+more!) This Is where you replace the command here with your command!");
});
shortcut.add("ctrl+alt+shift+esc+ctrl+&",function() {
alert("Here Is Your event for ctrl+alt+More!!! This Is where you replace the command here with your command!");
});
//Even try the F keys so on laptop it would be Fn plus the F key so in my case F5!
shortcut.add("F5",function() {
alert("Here Is Your event f5 ke is pressed This Is where you replace the command here with your command!");
});
//Extra...My Favourite one: CTRL+F
<script>
//Windows
shortcut.add("Ctrl+F",function() { //change to meta+F for mac!
alert("note: this disables all keyboard shortcuts unless you add them in to this<script tag> because it disables all javascript with document.write!");
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\"> </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\"> </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\"> </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("esc",function() {
alert("Press ctrl+shift instead!");
location.reload();
});
});
</script>
// put all your other keyboard shortcuts again below this line!
//Another Good one ...Ctrl+U Redirect to alternative view source! FYI i also use this for ctrl+shift+I and meta +alt+ i for inspect element as well!
shortcut.add("meta+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("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";
});
</script>
직접 수행하려면 "which"(즉, 더 이상 사용되지 않음) 대신 "key" 속성을 사용해야 합니다.
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
$("body").on('keypress', function(e){
if('g' === e.key){
console.log("ok");
}
});
언급URL : https://stackoverflow.com/questions/593602/keyboard-shortcuts-with-jquery
'source' 카테고리의 다른 글
도커에 안전하지 않은 레지스트리 추가 (0) | 2023.08.12 |
---|---|
자바.java.java동시 수정maridb에 연결할 때 예외 발생 (0) | 2023.08.12 |
j선택기에 특정 클래스가 있는 요소 제외 쿼리 (0) | 2023.08.07 |
JSONP 요청 반환 오류: "Uncaught SyntaxError:예기치 않은 토큰 :" (0) | 2023.08.07 |
Git repo의 하위 디렉터리만 Heroku에 배포/푸시하려면 어떻게 해야 합니까? (0) | 2023.08.07 |