This can be done without resorting to frameworks that are always bulky and slow. The trick is to record which event occurred at which point in time, and then work on top of it for a period of time. When you keypress event, the click event fires right after that, here is a list of how quickly the click event fires after the keypress event ...
Chrome 39.0 : 0ms
Firefox 31.0 ESR : 18 ~ 20 ms
IE 11 : 2 ~ 4 ms
Opera 12.1 : 0ms
Chrome and (real) Opera are not waiting, IE is fast enough, and Firefox takes so long to talk; the range of operation with (at least on my system) is 0 ~ 20 ms. Programmatically, I set a limit of 50 ms for people who are still using junky computers that are too busy with 150 useless processes in the background.
Please note that you will have to use window -level global events, although this seems to work reliably for me in all the browsers I mentioned.
var option = new function() {this.name = '';} window.onclick = function(event) { option.clickDate = new Date().getTime(); } window.onkeypress = function(event) { option.keyCode = event.keyCode; option.keyCodeDate = new Date().getTime(); } function MyFunc(e,obj) { if (option.keyCodeDate && option.clickDate && ( (option.clickDate - option.keyCodeDate)>=0 && (option.clickDate - option.keyCodeDate)<51) {alert('keypress event');} else {alert('click event');} }
source share