-3

I'm trying to simulate keyboard click for Ctrl+Esc via jQuery but it is NOT working.

var e=jQuery.Event("keydown",{keyCode:17} && jQuery.Event("keypress",{keyCode:107}));
jQuery("body").trigger(e);

Any idea?

TylerH
  • 20,799
  • 66
  • 75
  • 101
user3535039
  • 21
  • 1
  • 5

1 Answers1

1

This?

var press = jQuery.Event("keypress");
press.ctrlKey = true;
press.which = 27;
$(document).trigger(press);

To check which is the keycode of the button: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

I got the main idea from this answer: Simulate Keypress With jQuery

What you have done:

var e =
   jQuery.Event(
      "keydown", {keyCode:17}
      &&
      jQuery.Event(
         "keypress",{keyCode:107}
      )
   );
jQuery("body").trigger(e);

As you can see, this is a wrong syntax.

Community
  • 1
  • 1
Al.G.
  • 4,327
  • 6
  • 31
  • 56
  • Yes, this is exactly what I did before post my code but it's not working ? – user3535039 Apr 16 '14 at 13:53
  • @user3535039 It works as expected, so what are you expecting? That was the purpose of my previous comments. FYI, you cannot trigger any native OS behaviour using javascript, fortunately for security purpose. See expected behaviour: http://jsfiddle.net/6t8wG/ – A. Wolff Apr 16 '14 at 13:54
  • OK, So let's try backspace for prev page respose still not working ... get this : var event = jQuery.Event('keypress'); event.which = 8; event.keyCode = 8; jQuery(this).trigger(event); – user3535039 Apr 16 '14 at 13:56
  • @user3535039 Still for security purpose... Even you could play with history API – A. Wolff Apr 16 '14 at 13:57
  • @user3535039 you can get the previous visited page so: http://stackoverflow.com/q/3528324/3132718 – Al.G. Apr 16 '14 at 13:59
  • It's just a backspace and still not working. js can help here or it's useless to try because of "security purpose" ? – user3535039 Apr 16 '14 at 13:59
  • I used backspace for example no need to go for prev page – user3535039 Apr 16 '14 at 14:00
  • 1
    So what do you want to do? – Al.G. Apr 16 '14 at 14:02
  • @user3132718 if you can't help or can't touch it just don't say anything... Just LEARN the method how it works !! you can do many things with this script it does NOT matter what is the key combination for now !!! – user3535039 Apr 16 '14 at 14:06
  • 1
    @user3535039 You should LEARN how to ask a question... Maybe you are looking instead for dispatchEvent, but who knows... (and who cares!) – A. Wolff Apr 16 '14 at 14:11