2
votes

HTML button that when pressed simulates a ESC kayboard key pressed. So clicking the button would have same effect as user pressing ESC key on their keyboard

If it is certainly not possible in anyway please tell me.

Any method is fine.

EDIT: I dont want pressing ESC key to trigger something, i want the reverse, something that triggers ESC key

2
You have an event that is listening to the ESC key and you want to trigger it? Is that it? - acdcjunior
Dear you should read this answers carefully. stackoverflow.com/questions/9230308/… - Muhammad Irfan
And what should pressing the escape button do? You can't simulate keypress events that are handled by the browser or operating system, only keypress events that are handled by javascript itself (or native events). - adeneo
@Muhammad I dont understand the code in that link - Friedpanseller
Did You Check Any Of The Fiddle Below??? - Kiranramchandran

2 Answers

2
votes

Try This Code

JS

$('body').keydown(function(e){
    if (e.which==27){
        alert("hh");
    }

});

$('#trigger').click(function(){
    var e=$.Event('keydown');
    e.which=27;
    $('body').trigger(e);
});

HTML

<input type="button" id="trigger" value="trigger button" />

DEMO HERE

0
votes

using JQuery you can Create Key Press Event and bind that Event with Button Click. Like:

<input type="button" id="esc" value="Esc like button">


<script>
$('body').keydown(function(e) {
    if (e.keyCode == 27) {
        // put your code here if any (that will run after Esc pressed).
        alert('Esc Pressed');
    }    
});

var e = $.Event("keydown", {
    keyCode: 27
});

$('#esc').click(function() {
    $("body").trigger(e);
});
</script>

Fiddle DEMO