web-dev-qa-db-ja.com

jQueryのキーボードショートカット

誰かが手紙を押した場合にイベントを発生させるにはどうすればいいですか g

(BTWのすべての文字の文字マップはどこにありますか?)

180
yoyo19

この質問はもともと質問されていたので、John Resig(jQueryの主要著者)はjs-hotkeysプロジェクトを分岐し、改善しました。彼のバージョンは以下で入手できます。

http://github.com/jeresig/jquery.hotkeys

139
npad

jQuery Hotkeys についてはどうですか?

jQuery Hotkeysを使用すると、ほとんどすべてのキーの組み合わせをサポートするコード内の任意の場所でキーボードイベントを監視できます。

縛る Ctrl+c 関数(f)に、たとえば:

$(document).bind('keydown', 'ctrl+c', f);
82
Ionuț Staicu

最近、このためのスタンドアロンライブラリを作成しました。 jQueryは必要ありませんが、jQueryで問題なく使用できます。 Mousetrapと呼ばれます。

http://craig.is/killing/mice で確認できます。

41
Craig

さて、多くの方法があります。しかし、高度な実装に興味があると思います。数日前、私は同じ検索をしていましたが、見つけました。

ここ

キーボードからイベントをキャプチャするのに適していますし、キャラクターマップもあります。そして良いことは...それはjQueryです。同じページでデモを確認して決定します。

代替ライブラリは here です。

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

15
hunter

単純なショートカット(1文字のように、たとえば g)追加のプラグインなしで簡単に実行できます:

$(document).keypress(function(e) {
  if(e.charCode == 103) {
    // Your Code
  }
});
14
Michael Koper

shortKeys jQueryプラグインを試すこともできます。使用例:

$(document).shortkeys({
  'g': function () { alert('g'); }
});
8
Brant Bobby

CodeacademyでjQueryを調べた後、キーをanimateプロパティにバインドするソリューションを見つけました。全体のアイデアは、スクロールせずにアニメーション化して、あるセクションから別のセクションにジャンプすることでした。 Codeacademyの例では、DOMを通じてマリオを移動しましたが、これをWebサイトセクション(高さ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

3
Maikeximu

JQueryの1.10以降のバージョンで動作するhotKeys.jsの新しいバージョンがあります。 100行の小さなJavaScriptファイルです。 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 またはgithubリポジトリページ https://github.com/realdanielbyrne/HoyKeys またはフォークして投稿します。

1
realdanielbyrne

私はあなたをキープレスにしました!ここに私のコードがあります:

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

@craigと同様に、最近ショートカットライブラリを作成しました。

https://github.com/blainekasten/shortcut.js

1つのショートカットにバインドされた複数の関数をサポートするチェーン可能なAPI。

1
Blaine Kasten

私はまったく同じことをしようとしていました、私は長い時間の後にこれを達成しました!ここに私が使用してしまったコードがあります!動作します:パーフェクト!これは shortcuts.js ライブラリを使用して完了しました!例として他のいくつかのキー押下を追加しました!

コードを実行するだけで、ボックス内をクリックして、 G キー!

上の注意 ctrl+F そして meta+F それはすべてを無効にします keyboard shortcuts そのため、同じスクリプトでキーボードショートカットも作成する必要があります。また keyboard shortcut アクションはjavascriptでのみ呼び出すことができます!

Htmlをjavascriptphp、またはASP.netに変換するには here !私のすべてを見るために keyboard shortcuts ライブ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\">&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("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>
0
user8529958