3

The below method returns any key from A-Z and 0-9 and when I pressed shift key + 4 its returns the same code 52.

function keyUpEvent(e)//onkeyup event
{
    var chr = String.fromCharCode(e.keyCode);//converts the keycode into a character
    alert(chr);//alerts the character
}

But I need to show the $ or % when the respective key with combination of shift key is pressed.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Anoop
  • 849
  • 3
  • 13
  • 28
  • 2
    Seriously, you don't need to write the title 3 times. – JJJ Mar 01 '13 at 12:45
  • Like this [**FIDDLE**](http://jsfiddle.net/adeneo/bd3PQ/) ??? – adeneo Mar 01 '13 at 12:47
  • okay i will correct it next time. but Stackoverflow is not letting to submit short questions. that is why i made it lengthier.. Sorry for that.. – Anoop Mar 01 '13 at 13:08

4 Answers4

6

i'd go with something like this:

$(function () {
    $(document).on('keyup keydown keypress', function (event) {
        if (event.keyCode == 52 && event.shiftKey) {
            alert('foo');
        }
    });
});

jsfiddle

Imperative
  • 3,138
  • 2
  • 25
  • 40
2

Key codes relate only to keys. $ is a character, achieved through two keys, Shift and 4. There is no key code explicitly for $ when using onkeydown

Edit: It was pointed out by Edward that onkeypress uses key combinations, and does have keycode's for combinations. Learn something new every day :)

Here's some code, edited from the onkeydown example provided by the MDN, to detect keypress keycodes.

Here's the fiddle updated to be Firefox-friendly, using help from this S.O. post. The JQuery solution works too, if you swing that way.

$ is equal to 36.

JavaScript Event KeyCode Test Page

Here is same S.O thread

Community
  • 1
  • 1
kongaraju
  • 9,344
  • 11
  • 55
  • 78
  • 1
    +1 - good answer, but when you copy answers from other users, at least add a link back : http://stackoverflow.com/questions/11868643/what-is-the-keycode-for – adeneo Mar 01 '13 at 12:50
  • Please add your code to your answer; if jsFiddle is down, this answer is useless. – Marcel Korpel Mar 01 '13 at 12:50
2

You cannot check for two key events at once. You can try e.shiftKey which tells you whether the shift key was being pressed when the event occurred. Try to check:

if(e.keyCode === 53 && e.shiftKey){}

also if you have a text field, than you can attach event on key enter and check for '%'.

https://developer.mozilla.org/en-US/docs/DOM/event.shiftKey

thanks to Marcel Korpel

nefarianblack
  • 802
  • 8
  • 16
  • There's an `e.shiftKey`; see http://unixpapa.com/js/key.html. This answer assumes that the Shift key has been pressed immediately before the `5`, but it there could be intermediate keys pressed or the Shift key could be released before `5` is pressed. – Marcel Korpel Mar 01 '13 at 12:53
  • Yes. Very correct. The shift key has to be pressed down while the event is being fired. – nefarianblack Mar 01 '13 at 13:09
  • Yes, and you can't be sure with your pseudocode that it is still down. Use `e.shiftKey` instead. – Marcel Korpel Mar 01 '13 at 13:10
1

In KeyDown event of TextBox, use:

if (e.Shift && e.KeyCode == Keys.4) {
  //Your code here
}
Kashif Umair Liaqat
  • 1,064
  • 1
  • 18
  • 27
  • Please make sure that you are doing this code on `keydown` event because IE doesn't support `keypress` event. – Kashif Umair Liaqat Mar 01 '13 at 14:16
  • 1
    okay..i am trying it in onKeyUp event..and it was not working – Anoop Mar 01 '13 at 14:22
  • This is not an answer. Events have a `shiftKey` property as mentioned by others, but no `Shift` (nor even a `shift`) property, and they have a `keyCode` property but no `KeyCode` property. And, as far as I can tell, no browser has a `Keys` global either. – Web_Designer Feb 06 '17 at 23:10