-1

I had implemented code to detect Key combination of "window logo key" + "up arrow" key event. I am showing alert message when user click inside text box and hold "Win + up arrow" . It was working fine manually. But i am trying to trigger same on page load, Its not working.

$(document).ready(function() {

  var e = jQuery.Event('keyup');
  e.metaKey = true;
  e.which = 38; //up arrow

  // Not working
  $("#test").trigger(e);

  // or

  // Not working
  $("#test").keyup();


  $("#test").keyup(function(e) {

    if (e.which == 38 && e.metaKey == true) {
      alert('win + Up arrow pressed');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="test" type="text" />
tgogos
  • 23,218
  • 20
  • 96
  • 128
PavanKumar GVVS
  • 859
  • 14
  • 45
  • Why are you calling keyup event explicitly like this `//Not working $("#test").trigger(e); or //Not working $("#test").keyup();`? – Himanshu Upadhyay Sep 13 '17 at 11:16
  • You need to move the event handler above the trigger. When you call `$("#test").keyup();` there is no event handler set. – George Sep 13 '17 at 11:17
  • Umm,I cant understand your problem, checkout this jsbin link its working fine after i just removed two lines from your code http://jsbin.com/bolaqimaxa/edit?html,js,output – Ankush Malik Sep 13 '17 at 11:19
  • Thanks for your suggestions. It worked for me when i moved event handler above trigger. Why minus point, is not my question valid? – PavanKumar GVVS Sep 13 '17 at 11:31

2 Answers2

0

Have you tried this way? You should have declared you event handler before calling a trigger.

 $( "#test" ).on( "keyup", function (e){ 
     if(e.which == 38  && e.metaKey == true) 
     {
        alert('win + Up arrow pressed');
     }
 });
 $( "#test" ).trigger( "keyup" );
proti
  • 213
  • 3
  • 11
  • I tried your code also , its working. But why window is not maximizing? Perhaps i did not mentioned in my question.sorry for that – PavanKumar GVVS Sep 13 '17 at 11:48
  • Yes, you didn't mention it. I am not sure you can maximised the browser window fullscreen but you can resize it to screen.width and screen.height via javascript. Similarly to: function maximize() { window.moveTo(0, 0); window.resizeTo(screen.width, screen.height); } – proti Sep 13 '17 at 15:33
0

This is because you have declared your event handler after the call to keyup.

So to make your code work, you have to write it ike this:

$(document).ready(function() {

    $("#test").keyup(function (e){ 
        if(e.which == 38  && e.metaKey == true) {
            alert('win + Up arrow pressed');
        }
    });

    $("#test").keyup();

});
ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35