Abhishek Nath
Abhishek Nath

Reputation: 25

How to detect @ in keydown or keypress in jquery

I was trying to use the keypress or keydown event on '@' to generate a dropdown on a contenteditale div. But I didnot find the key code for '@' and i cannot use shift and 2 beacause it will be problem for many keyboard layout including mobile keyboard layout

Upvotes: 0

Views: 72

Answers (1)

jfriend00
jfriend00

Reputation: 707328

You can look for the keycode for @ which is decimal 64 in jQuery's e.which:

$(document).on("keypress", function(e) {
    // look for @ key
    if (e.which === 64) {
        alert("@ key pressed");
    }
});

Working demo: http://jsfiddle.net/jfriend00/s7KGb/


You could also code it like this which might be a bit more readable:

$(document).on("keypress", function(e) {
    // look for @ key
    if (String.fromCharCode(e.which) === '@') {
        alert("@ key pressed");
    }
});

Upvotes: 2

Related Questions