Page 1 of 1

Detecting keycodes on a Mac

Posted: Fri Sep 23, 2011 2:46 pm
by califdon
I have a little demo web page with interactive features operated by keyboard keys ("c" toggles the curtains, etc.). So jQuery

Code: Select all

$(document).keydown (function(e) { ...
will set e.which to 67 if the "c" key is pressed, etc. It works pretty well on PCs, but apparently Macs use different keycodes. A friend who is testing it for me on a Mac says that the keys do work (sort of) if he holds down the Command key, but then that triggers some Mac special function, like bringing up a context menu or something, in addition to triggering the action in my script. I haven't found anything useful from a Google search. Anyone have some experience with this?

If you're interested, the demo is at http://ravey.net/stage

Re: Detecting keycodes on a Mac

Posted: Tue Sep 27, 2011 2:50 pm
by pickle
It works perfectly fine for me. I've used keycodes before & they're all the same regardless of the OS. With that said, you might get some differences for OS-specific keys, such as the Windows key, or the Mac's Command key.

jQuery normalizes e.keyCode, so I'd recommend using that instead of e.which. You can also use String.fromCharCode(e.keyCode) to get the uppercase version of whatever key was typed.

Re: Detecting keycodes on a Mac

Posted: Tue Sep 27, 2011 5:36 pm
by califdon
Thanks, pickle. I'm afraid I don't have a Mac available to test it myself. The only keys I need for this app are a couple of alphabet keys and the 4 arrow keys. The Command key issue only arose because my friend with the Mac tried that when the plain alpha keys didn't work. That's a good tip, about e.keyCode, I'll sure try that. I hadn't expected that the ordinary keycodes were different, but that's what my friend reported. I'll try to find someone else with a Mac to see if it was just a misunderstanding with my friend (he is a retired Classics teacher and knows everything about the Romans, the Greeks, Shakespeare, etc., but is not so good with computer stuff, so that could be the problem. But when I want to translate a Roman phrase, he's my man! (Doesn't happen very often!) :)

Re: Detecting keycodes on a Mac

Posted: Tue Sep 27, 2011 6:11 pm
by pickle
I should have mentioned - I'm on a Mac (which is why I felt it relevant to report that it worked fine for me).

If you need a tester, I can help.

Re: Detecting keycodes on a Mac

Posted: Tue Sep 27, 2011 9:55 pm
by califdon
Ah! I wasn't sure, though I should have been. So you're saying that it DID work? THANKS! Apparently the problem is with my friend. There is sometimes an initial delay, I think perhaps due to loading the jQuery library from Google APIs. My friend "said" that he had tried it repeatedly, but I suspect that he was experiencing that delay. That's good news, because I wasn't expecting there to be a difference, other than maybe the special keys, as you mentioned.