Skip to content

Instantly share code, notes, and snippets.

@varlog23
Last active April 1, 2020 07:26
Show Gist options
  • Save varlog23/ef33933ccf6319b69430f97feee6a465 to your computer and use it in GitHub Desktop.
Save varlog23/ef33933ccf6319b69430f97feee6a465 to your computer and use it in GitHub Desktop.
var evnts = ["click", "focus", "blur", "keyup", "keydown", "keypressed"];
// You can also Use mouseup/down, mousemove, resize and scroll
for (var i = 0; i < evnts.length; i++) {
window.addEventListener("" + evnts[i] + "", function (e) {
myFunction(e);
}, false);
}
function myFunction(e) {
var evt = e || window.event;
if (evt) {
if (evt.isPropagationStopped && evt.isPropagationStopped()) {
return;
}
var et = evt.type ? evt.type : evt;
console.log("Detected event: "+et)
var trgt = evt.target ? evt.target : window;
console.log(trgt)
var time = Math.floor(Date.now() / 1000);
var x = 0,
y = 0,
scrolltop = 0;
if (evt.pageX) {
x = evt.pageX;
}
if (evt.pageY) {
y = evt.pageY;
}
if (trgt.scrollTop) {
scrolltop = trgt.scrollTop;
}
if (trgt.className && trgt.id) {
trgt = "." + trgt.className + "#" + trgt.id;
} else if (trgt.id) {
trgt = "#" + trgt.id;
} else if (trgt.className) {
trgt = "." + trgt.className;
}
if (typeof (trgt) != "String") {
if (trgt.tagName) {
trgt = trgt.tagName;
} else {
trgt = trgt.toString().toLowerCase();
trgt = trgt.replace("[object ", "");
trgt = trgt.replace("]", "");
trgt = trgt.replace("htmlbodyelement", "BODY");
}
}
var xtra = "";
if (evt.keyCode) {
key = evt.keyCode
//xtra += " KeyCode: " + evt.keyCode;
xtra += String.fromCharCode((96 <= key && key <= 105) ? key-48 : key);
}
if (evt.shiftKey) {
xtra += " ShiftKey ";
}
if (evt.altKey) {
xtra += " altKey ";
}
if (evt.metaKey) {
xtra += " metaKey ";
}
if (evt.ctrlKey) {
xtra += " ctrlKey ";
}
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
xx = w.innerWidth || e.clientWidth || g.clientWidth,
yy = w.innerHeight || e.clientHeight || g.clientHeight;
//xtra += " RES:" + xx + "X" + yy;
//document.getElementById("op").innerHTML += "<tr><td>" + et + "</td><td>" + trgt + "</td><td>" + x + "</td><td>" + y + "</td><td>" + scrolltop + "</td><td>" + time + "</td><td>" + xtra + "</td></tr>";
console.log("x:"+x+", y:"+y)
console.log("time: "+time)
console.log("Key: "+xtra)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment