Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting when the @ key is pressed

I am trying to detect when a user pressed the @ key in a text box. I can use JQuery to handle the keyup event like so...

$('#target').keyup(function(event) {

});

But what do I do from here to test for the @ character? I know I can use event.which to get a key code. But in this instance I would need to also check for shift - technically this is not a problem, but I know the @ key can move around with different language settings and I am worried that this may prove to be inconsistent. Maybe I am worried wrongly, and I can rely on it always being SHIFT + 192?

Ideally I would like something like the following to allow for easier configuration later on...

event.something == "@";

Thanks for any help

like image 261
musefan Avatar asked Dec 21 '22 05:12

musefan


1 Answers

Try checking for event.which == 64, as 64 is the ascii of the at sign (not tested, but should work)

$("#target").keypress(function(event) {
  if ( event.which == 64 ) {
    alert("weehee");
  }
});

If this fails, you can check after keyup (when user releases key), if the last character he inputted into the text field is a @ sign, and act accordingly.

Shai

like image 110
Shai Mishali Avatar answered Jan 03 '23 02:01

Shai Mishali