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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.