Here we are not checking that the shiftkey is pressed or not. What i need is to press shiftkey on a function call
We have code where a key can be pressed on element keypress.
e = jQuery.Event("keypress");
e.which = 13 //choose the one you want
$("#test").keypress(function(){
alert('keypress triggered');
}).trigger(e);
My requirement is Shift Key should be kept pressed on my function and released on another function called.
function called(){
.. code ..
// need shift key pressed
}
function called_another_func(){
.. code ..
// need shift key released
}
Continuous Shift key press can be triggered programmatically by below code (16 is shift key keycode):
$("selector").keydown(function(event){
if(event.which === 16){
//any code you want to execute before key press
e=$.Event('keydown')
e.which=16
$("selector").trigger(e)
}
});
However this may hang the application because of recursive event calls and throw an error in console. To prevent this you will have to do something like this:
$("selector").keydown(function(event){
if(event.which === 16){
if("flag to check if you need to continue shift key press"){
//any code (or function call) you want to execute before key press and
//toggle the variable to break out of this recursive event trigger
e=$.Event('keydown')
e.which=16
$("selector").trigger(e)
}
}
});
$("selector").keyup(function(event){
if(event.which === 16){
//call your function
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With