Something like this might do the trick.
var pressingEnter = false; $(document).on({ keydown: function(e) { if(e.which == 13) { // enter is being pressed, set true to flag variable pressingEnter = true; } }, keyup: function(e) { if(e.which == 13) { // enter is no longer pressed, set false to flag variable pressingEnter = false; } }, click: function() { if (pressingEnter) { console.log('click and enter pressed'); } } });
BTW: no need to do var code = e.keyCode || e.which; var code = e.keyCode || e.which; since jQuery allows this for you. You can use e.which in any browser.
EDIT
This version should allow any order of keystroke / mouse click. I assume that only the left click is locked. Logic for input input + mouse click is placed on keydown and mousedown (it can be moved to keyup and mouseup , if that makes sense)
Changed alert to console.log , since the first prevents the mouseup event. We currently have hundreds of better ways to show a message to the user than the built-in pop-up pop-ups, so I assume that it works for him, this is not a requirement.
var pressingEnter = false; var clickingMouseButton = false; $(document).on({ keydown: function(e) { if(e.which == 13) { pressingEnter = true; } if (clickAndEnterPressing()) { console.log('click and enter pressed'); } }, keyup: function(e) { if(e.which == 13) { pressingEnter = false; } }, mousedown: function(e) { if (e.which == 1) { clickingMouseButton = true; } if (clickAndEnterPressing()) { console.log('click and enter pressed'); } }, mouseup: function(e) { if (e.which == 1) { clickingMouseButton = false; } } }); function clickAndEnterPressing() { return pressingEnter && clickingMouseButton; }
source share