Tarun Dugar
Tarun Dugar

Reputation: 8971

Detect special character produced by combination of shift key and some other key

For instance, how can I detect the @ character(using keyup, keypress) which is produced when I use shift + 2 keys simultaneously?

I tried using which to detect if both shift and 2 were entered one after the other but how can I detect if shift was kept pressed and then 2 was pressed?

Note: I don't just want to detect if the shift and 2 keys are pressed one after the other, but if shift and 2 keys were pressed at the same time to generate @.

Upvotes: 1

Views: 548

Answers (1)

Dhara
Dhara

Reputation: 1972

I have faced this problem and I got solution is like: Keycode is just detecting which key is pressed..May b not which is another character on that

$.ctrl = function(key, callback, args) {
  $(document).bind('keydown', function(e) {
    if (!args) args = []; // IE barks when args is null
    if (e.shiftKey && e.keyCode == key.charCodeAt(0)) {
      e.preventDefault();
      callback.apply(this, args);
      return false;
    }
  });
}
$.ctrl('2', function() {
  alert('@');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions