MVC3 jQuery keyup event

扶醉桌前 提交于 2019-12-12 05:03:54

问题


I have a C#.NET MVC3 web app and I want to trap the key up event on the document. Namely, I want to know if the usre has selected "CTL->Z" to undo their data changes on the web View. How might I do this?


回答1:


I think this is what you're looking for:

var ctrlDown = false;
$(document).keydown(function (e) {
    if (e.which == 17)
        ctrlDown = true;
    if (e.which == 90)
        if (ctrlDown)
            console.log("control Z"); 
});
$(document).keyup(function (e) {
    if (e.which == 17)
        ctrlDown = false;
});

EDIT

I'm not sure if e.ctrlKey from mesiesta's answer is supported cross-browser, but if it is, you could do more simply:

$(document).keydown(function (e) {
    if (e.which == 90 && e.ctrlKey)
        console.log("control Z");
});



回答2:


You can try to use this Ctrl + Key Combination – Simple Jquery Plugin . I've never tried to use it, but it seems a good solution for that)))

Here is the link

http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/

So here is that function code

$.ctrl = function(key, callback, args) {
$(document).keydown(function(e) {
    if(!args) args=[]; // IE barks when args is null
    if(e.keyCode == key.charCodeAt(0) && e.ctrlKey) {
        callback.apply(this, args);
        return false;
    }
});
};

And then in your code you must write only

$.ctrl('Z', function() {
    //What you want to do
});


来源:https://stackoverflow.com/questions/8391266/mvc3-jquery-keyup-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!