Skip to main content

How To Detect Right Mouse Click + Delete Using Jquery/Javascript


I want to track Right Mouse Click + Delete event on a html text input. I Succeed in mapping Right Mouse Click + Paste/Cut/Copy as below




$("#evalname").bind('paste/cut/copy', function(e)
{
do something

});



Here 'evalname' is the id of my html text input. I tried like




$("#evalname").bind('delete', function(e)
{
do something

});



but not working. Is there any way to map Right Mouse Click + Delete event in Jquery/Javascript ?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. As already answered, it isn't possible to pick up on the browsers contextmenu delete being used, in fact, using .bind('copy', func....) will not only listen to the contextmenu's copy, but also CTRL+c as it's actually binding to the clipboard.

    I have put together a plugin, which to be honest is a bit of a hack, but it will allow you to catch:

    Context COPY, CUT, PASTE, DELETE - ONLY
    Context COPY, CUT, PASTE, DELETE - AND - CTRL+c, CTRL+x, CTRL+v

    Or just one, two, three or four item(s) in either of the above ways. Of course one problem was IE, it doesn't trigger jQuerys .bind('input', func.... to listen for changes, so I needed to trigger it for IE, hence there could be a vary small delay (milliseconds).

    The plugin:

    (function($) {
    $.fn.contextDelete = function(options) {
    var set = {
    'obj': $(this),
    'menu': false,
    'paste': false,
    'cut': false,
    'copy': false,
    'set': '',
    'ie': null,
    };
    var opts = $.extend({
    'contextDelete': function() {},
    'paste': function() {},
    'cut': function() {},
    'copy': function() {},
    'contextOnly': false,
    }, options);

    $(window).bind({
    click: function() {
    set.menu = false;
    },
    keyup: function() {
    set.menu = false;
    }
    });

    set.obj.bind({
    contextmenu: function() {
    set.menu = true;
    set.paste = false;
    set.cut = false;
    set.copy = false;
    set.val = set.obj.val();

    // Hack for IE:
    if ($.browser.msie) {
    set.ie = setInterval(function() {
    set.obj.trigger($.Event('input'));
    if (!set.menu) {
    clearInterval(set.ie);
    }
    }, 300);
    }
    // End IE Hack
    },
    paste: function(e) {
    set.paste = true;
    if (opts.contextOnly) {
    if (set.menu) {
    opts.paste(e);
    set.menu = false;
    }
    }
    else {
    opts.paste(e);
    }
    },
    cut: function(e) {
    set.cut = true;
    if (opts.contextOnly) {
    if (set.menu) {
    opts.cut(e);
    set.menu = false;
    }
    }
    else {
    opts.cut(e);
    }
    },
    copy: function(e) {
    set.copy = true;
    if (opts.contextOnly) {
    if (set.menu) {
    opts.copy(e);
    set.menu = false;
    }
    }
    else {
    opts.copy(e);
    }
    },
    input: function(e) {
    if (set.menu && (!set.paste) && (!set.cut) && (!set.copy)) {
    if (set.obj.val().length < set.val.length) {
    opts.contextDelete(e);
    set.menu = false;
    }
    }
    }
    });
    };
    })(jQuery);


    One example usage, contextmenu delete + context copy ONLY:

    $('#evalname').contextDelete({
    contextDelete: function(e) {
    alert('You just deleted something!');
    },
    copy: function(e) {
    alert('You just copied something!');
    },
    contextOnly: true,
    });


    Click Here for a DEMO

    ReplyDelete
  2. To expand on Stefan's comment, and UberNeet's answer:

    You can't detect a choice of "Delete" from the context menu.

    You can detect a change to the input's contents, either at keyup (that'll catch the delete key) or on change or blur (that'll detect if they empty the field and click somewhere else).

    If you want to know the moment it's emptied, even if they've not left the field, then you could try setting a timer to poll every half second, and check if the field is empty. Beware of using too tight a timer for fear of overworking the poor user's browser.

    None of these are ideal solutions, but that's the joy of working inside the browser!

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex