Answers for "js detect cmd+s"

0

js detect cmd+s

// To detect Cmd key pressed, check Event.key === 'Meta'
document.querySelectorAll('#form').forEach(form => {
	let meta_pressed = false;
	form.addEventListener('keydown', e => {
		if ( e.key === 'Meta' ) {
			meta_pressed = true;
		} else if ( e.key === 's' ) {
          	// Keep the browser from running its own
          	// Cmd+S (Save Page As...) function
			e.preventDefault();
			form.submit();
		}
	});
	form.addEventListener('keyup', e => {
		if ( e.key === 'Meta' ) {
			meta_pressed = false;
		}
	});
});
Posted by: Guest on July-07-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language