Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery key code for command key

I have read jQuery Event Keypress: Which key was pressed? and How can i check if key is pressed during click event with jquery?

However my question is if you can get the same key event for all browsers? Currently I know that Firefox gives the command button (Mac) the code 224 while Chrome and Safari give it the value 91. Is the best approach to simply check what browser the user is using and base the key pressed on that or is there a way so that I can get 1 key code across all browsers? Note I am getting the value with the:

var code = (evt.keyCode ? evt.keyCode : evt.which); 

I would love to not use a plugin if possible just because I only need to know about the command/ctrl (windows system) key pressed.

like image 771
Craig Avatar asked Sep 30 '10 19:09

Craig


People also ask

What is e keycode === 13?

key 13 keycode is for ENTER key.

How do you find out which key was pressed in jQuery?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

What is keycode jQuery?

Key codes are the keyboard keys to which have digital values mapped to the keys based on the key code description. jQuery keycode is a part of Themes in jQuery UI API category, there are many more API's like disableSelection(), enableSelection(), . uniqueId(), . zIndex(), . focus() provided by jQuery.


1 Answers

jQuery already handles that. To check if control was pressed you should use:

$(window).keydown(function (e){     if (e.ctrlKey) alert("control"); }); 

The list of modifier keys:

e.ctrlKey e.altKey e.shiftKey 

And as fudgey suggested:

e.metaKey 

Might work on MAC. Some other ways here as well.

like image 99
BrunoLM Avatar answered Sep 24 '22 19:09

BrunoLM