1

How can I use

$('div').bind('keyup', function(e) {

   if(e.which == '@') {

   }

});

Wondering how I can get the @ symbol for keyup ?

Andy
  • 18,723
  • 12
  • 46
  • 54

3 Answers3

2

Use the String.fromCharCode method. The reverse function is "@".charCodeAt(0).
The e.which will only hold sensible data about the pressed key at a keypress event. This event will trigger multiple times though, while a key is pressed down.

If you need a reliable method to check the character of a key during the keyup event, create a character map. This page will aid you.

$('div').bind('keypress', function(e) {
   var char = String.fromCharCode(e.which);
   if(char == '@') {

   }
});
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 2
    This doesn't work, key codes for characters can vary by locale. For example, English (UK) '@' is on key 192, along with `'`. – Andy E Oct 27 '11 at 16:50
  • @AndyE `ev.which` shows `64` at the `keypress` event. Improved answer. – Rob W Oct 27 '11 at 17:02
0

You could try yourself before post such question

Go to JSBin and do a simple test:

http://jsbin.com/afapar/2/edit#javascript,html,live

Then you will know exactly what to look for.


by the way, because you need to worry about plenty on all mobile devices and browsers, I will suggest that you use indexOf() or a variant of it, for example:

$("#txt").bind("keyup", function(evt) {
   if( $(this).val().indexOf("@") > 0 ) {
       // got it
   }
});

or if you need to know if it's the last character written:

if( $(this).val().lastIndexOf("@") == $(this).val().length - 1) 
{
    // got it
}
balexandre
  • 73,608
  • 45
  • 233
  • 342
-1

This seems to be working.. http://jsfiddle.net/jNeH9/2/

$(document).bind('keyup', function(e) {
   if(e.which == 50 && e.shiftKey)
   {

   }

});
Jishnu A P
  • 14,202
  • 8
  • 40
  • 50