0

I need to capture an onkeyupevent on an image element. Here's the markup for that element:

<input class="globalbuttons" type="image" alt="save" title="save"
  onclick="resetScrollPosition(); tabindex="0" 
  src="/kc-dev/kr/static/images/buttonsmall_save.gif" 
  name="methodToCall.save">

The reason is to force all users to use the mouse and CLICK the button, rather than allow the user to tab into the image and press Space or Enter to emulate a mouse click.

I got the following simple test to work:

<input class="globalbuttons" type="image" alt="save" title="save"
 onclick="resetScrollPosition(); tabindex="0"
 onkeyup="alert(event.keyCode);"
 . . .

The alert properly shows the key codes for every key that I press while the image remains in focus. Next, I tried replacing the alert with a function and passing the function the event, but this was not working.

Also, I'm wondering if this is the best approach:

  1. Should I only ignore the key presses that could "emulate a click" (e.g. Space or Enter) .

    OR

  2. Is better to not to discriminate the key that got pressed, but rather ignore ALL keys for this element?

Thanks.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • Do you know the event.preventDefault() method? Maybe it works for you. – Rodrigo Apr 08 '14 at 16:02
  • Ahh, good point @Rodrigo. I'll give that a try. So, it sounds like you agree that its best to ignore all keys in this case, rather than only have the event.preventDefault() if the keyCode == '13' or keyCode == '32' –  Apr 08 '14 at 16:06
  • Hi- FYI you have a quote missing .. onclick="resetScrollPosition(); tabindex="0" <- no double quote after (); – user2808054 Apr 08 '14 at 16:18
  • @user2808054 - yeah, I manually typed the markup, I was having issues copying and pasting...in my webapp - it is correct as you have suggested. Thanks for looking out though... –  Apr 08 '14 at 16:22
  • no worries- damn I was hoping that wat the problem lol – user2808054 Apr 08 '14 at 16:26
  • I don't know what is the best in your case, ivan. The best way is to test both ways (preventDefault inside an if, and not inside an if) and see what makes you feel better. – Rodrigo Apr 08 '14 at 19:55

1 Answers1

0

I can't see a case for keeping only some keyups. So, what I recommend are two things.

Add tabindex="-1" to your input to remove it from 'being tabbed to' by default.

<input class="globalbuttons" type="image" alt="save" title="save" onclick="resetScrollPosition();" tabindex="-1" src="http://placehold.it/350x150" />

Return false on keyups for these elements.

function avoidKey() {
    /* keyCode logic if you decide to go that route */
    return false;
}

var gb = document.getElementsByClassName('globalbuttons');
for (var i=0; i < gb.length; i++) {
    // Would be better to have a function that abstracts binding multiple events
    gb[i].addEventListener('keyup', avoidKey, false);
    gb[i].addEventListener('keypress', avoidKey, false);
    gb[i].addEventListener('keydown', avoidKey, false);
}

Browser support for getElementsByClassName()

Might be easier to go the jQuery route and just use $('.globalbuttons').on('keyup keydown keypress', function() { });

John
  • 417
  • 2
  • 6
  • I'll give this a try John. I believe the getElementsByClassName is not crossbrowser friendly, so I'll slightly tweak your suggestion. –  Apr 08 '14 at 16:19
  • John - I tried this fix you suggested, but I'm still having issues. The form is still getting submitted even when no elements are in focus and I hit either the SPACEBAR or ENTER. So, it seems that I need to block both of these keys on the form, but the problem with that is that I have text areas where these key strokes should be allowed. Also, I implemented your fix via jQuery approach. addEventListener is definitely NOT cross-browser friendly. –  Apr 09 '14 at 17:35
  • For some reason, I was thinking that you only wanted to prevent keyups on your inputs. Yes, the form will submit by default on enter. Please see http://stackoverflow.com/questions/895171/prevent-users-from-submitting-form-by-hitting-enter – John Apr 09 '14 at 17:38