Skip to content

Instantly share code, notes, and snippets.

@IamManchanda
Created June 17, 2018 22:19
Show Gist options
  • Save IamManchanda/2ad0c95acb52a63975ae58518b5243c2 to your computer and use it in GitHub Desktop.
Save IamManchanda/2ad0c95acb52a63975ae58518b5243c2 to your computer and use it in GitHub Desktop.
jQuery Mouse and Keyboard events
$(document).ready(() => {
const onEvent = (event, eventLog = event) => {
let box;
let pageCoords;
if (event.startsWith('key')) {
box = $(document);
pageCoords = (e) => `(${e.which})`;
} else {
box = $('.element');
pageCoords = (e) => `(${e.pageX}, ${e.pageY})`;
}
return box.on(event, (e) => {
console.log(eventLog, pageCoords(e));
});
};
onEvent('click', 'You just clicked the box');
onEvent('dblclick', 'You just double clicked the box');
onEvent('mouseenter', 'You entered the box');
onEvent('mouseleave', 'You just left the box');
onEvent('mousedown', 'You just pressed the mouse');
onEvent('mouseup', 'You just released the mouse');
onEvent('mousemove', 'You are moving the mouse');
onEvent('keydown', 'You just pressed the key');
onEvent('keyup', 'You just released the key');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment