开发者

Bind A key using Jquery?

I am trying to bind a key combination Ctrl+t with some event, so if i press Ctrl+t in m开发者_如何学Cy application it should go to specified url How can i do it using Jquery ?


Check out the jQuery HotKeys plugin:

http://plugins.jquery.com/project/hotkeys

Update:

Added example

<html>
<head>
    <script src="jquery-1.3.2.min.js"></script>
    <script src="jquery.hotkeys.js"></script>

    <script>
    $(document).ready(function(){

        $(document).bind('keydown', 'Ctrl+t', function(event){ alert('Ctrl t pressed')});

    });
    </script>
</head>
<body>

</body>
</html>


You can do this very simply with keydown, event.which and event.ctrlKey. These are normalized by jQuery, so you don't need to fuss with sorting out cross-browser stuff.

$(document).keydown(function(event) { // or whatever selector
    if (event.ctrlKey && (event.which === 84)) {
        window.location = 'http://example.com'; // or whatever url
    }
});


try this:

var isCtrl = false;
$(document).keyup(function(e) {
    if (e.which == 17) isCtrl = false;
}).keydown(function(e) {
    if (e.which == 17) isCtrl = true;
    if (e.which == 84 && isCtrl == true) {
        window.open('http://www.google.com', '_self', 'resizable,location,menubar,toolbar,scrollbars,status');
        return false;
    }
});


<html>
<head>
    <script src="jquery-1.3.2.min.js"></script>
    <script src="jquery.hotkeys.js"></script>

    <script>
    $(document).ready(function(){
        function test(evt){
          var keyCode = (evt.which?evt.which:(evt.keyCode?evt.keyCode:0))
            if(keyCode==116)
                window.location.href='your redirection';
        }
    });
    </script>
</head>
<body>
    <input type="Text" onkeypress=" test(event);"
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜