When someone clicks a button I want my function to return false and the keydown function will be disabled.
Then, if I click this same button a second time, it will return true and the keydown function will be enabled.
I did like this but it only returns false and keydown function disabled. I also need to enable this keydown function by clicking this button.
How can I do this?
var controlsEnabled = true;
$(".keyboard-btn").on('click', function() {
controlsEnabled = false;
});
$(document).keydown(function(e) {
if (controlsEnabled) {
if (e.keyCode == 38) {
verticalSlideDown();
console.log("pressed key for Down : " + e.keyCode);
}
if (e.keyCode == 40) {
verticalSlideUp();
console.log("pressed key for Up: " + e.keyCode);
}
if (e.keyCode === 13) {
var div = $(".scroll-inner-container");
console.log("pressed key for stop : " + e.keyCode);
div.stop();
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="keyboard-btn">click here</button>