|
|
Subject:
Detect Mac Command key being pressed in IE (Javascript)
Category: Computers > Internet Asked by: bay-ga List Price: $4.00 |
Posted:
15 May 2002 11:29 PDT
Expires: 22 May 2002 11:29 PDT Question ID: 16409 |
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.) |
|
Subject:
Re: Detect Mac Command key being pressed in IE (Javascript)
Answered By: mplungjan-ga on 15 May 2002 14:22 PDT Rated: |
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
rated this answer:
The actual solution was suggested by the answerer, but in the comments section below. I put a comment there that has an example script that solves the problem. Thanks! |
|
Subject:
Re: Detect Mac Command key being pressed in IE (Javascript)
From: riegel-ga on 15 May 2002 13:21 PDT |
I use a PC keyboard on my mac with a Belkin adaptor. The Alt key is mapped to the command key. I wonder if you queried for the alt key if IE would return true when actually pressing the command key? |
Subject:
Re: Detect Mac Command key being pressed in IE (Javascript)
From: mplungjan-ga on 15 May 2002 14:35 PDT |
IE property pages at msdn: http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/ctrlkey.asp |
Subject:
Re: Detect Mac Command key being pressed in IE (Javascript)
From: mplungjan-ga on 15 May 2002 23:03 PDT |
Some more investigation reveals that IE4 on mac has some issues http://www.webreference.com/js/column11/modifierkeys.html Please run the following and see what it says in the status line (if anything) when you press the meta key <SCRIPT LANGUAGE="JavaScript"><!-- // cloak /* Scan code program (c) 2000-2002 Michel Plungjan "michel at irt.org" */ ns4 = (document.layers) ?1:0; ie4 = (document.all) ?1:0; dom = (document.getElementsById) ?1:0; if (ns4) document.captureEvents(Event.KEYDOWN); document.onkeydown = keyDown; window.onkeydown = keyDown; function keyDown(e) { var keyPress = (ie4) ? window.event.keyCode : e.which ; shft = ((ns4) ? e.modifiers && Event.SHIFT_MASK : window.event.shiftKey) ? " shft " :""; alt = ((ns4) ? e.modifiers && Event.ALT_MASK : window.event.altKey) ? " alt " :""; ctrl = ((ns4) ? e.modifiers && Event.CONTROL_MASK : window.event.ctrlKey) ? " ctrl " :""; meta = ((ns4) ? e.modifiers && Event.META_MASK : window.event.metaKey) ? " meta " :""; var key = String.fromCharCode(keyPress); window.status= key + ' (' +keyPress + ') ' + shft + ' ' + alt + ' ' + ctrl + ' ' + meta; return false; } // uncloak --></SCRIPT> |
Subject:
Re: Detect Mac Command key being pressed in IE (Javascript)
From: bay-ga on 16 May 2002 15:36 PDT |
This is what is displayed in the status bar when I press the Mac Command key with the above Javascript code: [ (91) So looking for keycode 91 does seem to be a reliable way of detecting the Command keypress itself. Great! This code then does exactly what I needed: <script> // simplified to handle IE only var commandkey = false; function keyDown() { if (event.keyCode == 91) commandkey = true; } document.onkeydown = keyDown; function keyUp() { if (event.keyCode == 91) commandkey = false; } document.onkeyup = keyUp; function checkcommand() { alert(commandkey ? "command key pressed!" : "command key NOT pressed"); } </script> <a href=# onclick="checkcommand()">click me</a> Asides: For IE-Windows, this detects whether the Windows key is being pressed. There is a danger of the commandkey variable getting out of sync with the actual state of the commandkey if the focus is not on the browser window when the command key is pressed or released. Thanks mplungjan. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |