1

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 @.

Tarun Dugar
  • 8,921
  • 8
  • 42
  • 79
  • 1
    Possible duplicate of [Check Ctrl / Shift / Alt keys on 'click' event](http://stackoverflow.com/questions/2847135/check-ctrl-shift-alt-keys-on-click-event) – Andrey Feb 11 '16 at 07:50
  • 1
    The keypress event should give you the code for the @ character. If not, just check for the 2 key's keycode and the .shiftKey property. – nnnnnn Feb 11 '16 at 07:51
  • No, it gives me the code for '2' and 'shift' keys but not '@'. – Tarun Dugar Feb 11 '16 at 07:52
  • @Andrey - the linked question isn't a duplicate because it is about mouse click plus shift/ctrl. – nnnnnn Feb 11 '16 at 07:53
  • @nnnnnn, as you say, but still, answer from there is applicable here. Totally same principe – Andrey Feb 11 '16 at 07:55
  • 1
    to put @nnnnnn's comment and the answer together - `e.which==50&&e.shiftKey` – JNF Feb 11 '16 at 08:42

1 Answers1

1

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>
Dhara
  • 1,914
  • 2
  • 18
  • 37
  • Summarizing the answer here as mentioned by JNF: (e.shiftKey && e.which === 50) will return true if shift and 2 are pressed. – Tarun Dugar Feb 11 '16 at 08:47