Knowing whether control key is pressed while an event happened

If you are listening to click events of anchors to do some operation of showing a dialog or doing something else, you have to know whether ctrl key is pressed or not as you may not want your custom logic to kick in when ctrl key is pressed as user wants to open the link in the new tab.

He is the solution, assuming jquery is used
jQuery("a.link").click(function(event) {
if(event.metaKey || event.ctrlKey) {
//ctrl key is pressed
}
});

checking for metaKey apart from ctrlKey property of event as mac's command key press will set metaKey property to true and ctrlKey for remaining browsers.

Comments

Popular posts from this blog

Proper way to have an anchor tag with onclick event

Some thoughts on MongoDB Nodejs driver

CORS issues with IE9 and workarounds