2

Problem: I can not check whether the shift Key was being held on a form submit. This returns an undefined instead of true or false.
Why does this happen?

Example of the problem: http://jsfiddle.net/DRSDavidSoft/fFYKs/

What I want: I want to know whether the shift key was being held when a form is submitted.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
David Refoua
  • 3,476
  • 3
  • 31
  • 55
  • Just curious, but why do you want to detect specifically that key? – Christopher Marshall May 01 '14 at 21:20
  • Ctrl key and Alt key would help that too! I just want to alter a button's function. I can give you a link to my project if you want. – David Refoua May 01 '14 at 21:22
  • 2
    This is really easy, the onsubmit event has no concept of keys as there should be no reason to check keys on submit. Change it to onclick, and it will work. – adeneo May 01 '14 at 21:25
  • True, but shouldn't event report which keys are being pressed or not at any time? – David Refoua May 01 '14 at 21:30
  • 2
    That depends on where the `event` object is coming from, it's not some magical object that is always the same. Some event handlers will have an `event` object that has an `event.target`, key events will have `event.which` and mouse events will usually have `event.pageX` etc. the list goes on, so the `event` object is different for each event. There's no reason for a submit event to have an `event.shiftKey` property, or any other property that deals with keys, as keys are irrelevant when submitting a form. – adeneo May 01 '14 at 21:45
  • try looking at this question, it may have the answer that you're looking for.. http://stackoverflow.com/questions/10599214/is-the-shiftkey-held-down-in-javascript – Hawk May 01 '14 at 21:31
  • Thanks, but if you mean using "evt", I had no success at that either. – David Refoua May 01 '14 at 21:33

1 Answers1

2

This is because submit event know nothing about keys. I'd advice to replace your submit input with button input. And there check will work fine. Here is the sample: http://jsfiddle.net/fFYKs/2/

<input type="button" value="Click here to submit" onclick='isPressed(event);'/>

And JS:

function isPressed(e){
    if (e.shiftKey) {
        window.myForm.submit();
    }
}

Also for cross-platform details recommend to visit this link: http://goo.gl/wKdJMO

sleepwalker
  • 1,032
  • 1
  • 9
  • 15
  • Thanks, but I already knew this. My form contains a text input and the form should be submitted by enter. I just want to change what that form do when the shift key is pressed. I think it's better to stick to the radio boxes here. Thanks anyway! ;) – David Refoua May 01 '14 at 22:10
  • 1
    One more idea I have is to bind the window.keydown event, where to set in JS variable that shift was pressed. And windows.keyup - to set that variable as false, when shift was released. And in your check use mentioned variable. That should also work fine, but will add overhead like additional check at each key down/up. – sleepwalker May 01 '14 at 22:15