Detect Mac Command key being pressed in IE (Javascript)
-
I have a web page that is using Javascript to detect whether the Command key on the Mac is being pressed when the user clicks a link. This works fine on Mozilla, but it appears that IE does not implement the metaKey attribute on events (event.metaKey on Mozilla detects the Command key). Here's some sample code: <script> function ck(e) { alert(e.metaKey); if (e.metaKey) { alert("metaKey is true! You are pressing the Command key.") } else { alert("metaKey is false or undefined."); } } </script> <a href=# onclick="ck(event); return false;">press Command and click here</a> Is there a way to detect if the Command key is being pressed for IE (on the Mac)? My underlying goal is to intercept the click to do some processing, but then to go to a certain url. If the Command key is not pressed, then it just uses location.href = destination_url, which sends the page to the url. If the Command key is pressed, I want to behave the same way as Mac browsers do by default, which is to open the url in a new window; in this case I use window.open(destination_url). For various reasons, I cannot simply return "true" from the onClick in order to follow the default url of the anchor tag. (I know that would allow the browser to automatically open the url in the same or new window depending on the modifier keys being pressed.)
-
Answer:
I strongly suspect the event.ctrlKey is the one you want: <script> function ck(e) { var ctrl = null; if (window.event) ctrl = event.ctrlKey; else if (e) ctrl=(Event.META_MASK || Event.CTRL_MASK) if (ctrl) { alert("metaKey is true! You are pressing the Command key.") } else { alert("metaKey is false or undefined."); } } document.onkeypress =ck; window.onkeypress =ck; if (document.layers || (document.getElementById && !document.all)) document.captureEvents(Event.KEYPRESS); </script> Please let me know if I am correct since I do not have access to a Mac. Here are some interesting links I have come across recently. Events browser compatibility http://www.xs4all.nl/~ppk/js/events_compinfo.html Event Capturing - Netscape 4 http://www.xs4all.nl/~ppk/js/evcapnn4.html Working with Javascript - Event Models http://developer.apple.com/internet/javascript/eventmodels.html Good luck, mplungjan-ga
bay-ga at Google Answers Visit the source
Related Q & A:
- How do you pause a program until a button is pressed in JAVA?Best solution by Stack Overflow
- How to detect Android in JavaScript?Best solution by Stack Overflow
- How to programmatically turn on Shift key when spacebar is pressed in UITextField?Best solution by Stack Overflow
- How to detect compatibility mode in IE and actual version?Best solution by social.msdn.microsoft.com
- What is the difference between internal command and external command in Linux?Best solution by answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.