Reputation: 14187
I'm working on a terminal-like modal, using bootstrap's and I want to clear it when the user presses Ctrl+L like it does when he writes clear
in it.
But the problem is, when I press Ctrl+L, it focuses the URL bar, and I don't have anymore focus on my page.
In this part, alert is showing, but it still focuses URL bar after :
if(event.key == "l" && event.ctrlKey)
alert("clear it");
I have tried several things like focusing my terminal after a sec, or things like that. But nothing worked.
Have you got any ideas ? Thanks you !
Upvotes: 3
Views: 639
Reputation: 701
This might help. But if you press L two times without leaving Ctrl it will not work.
sorry but I am using jQuery, but I hope you got the logic.
var key = '';
$(document).on('keydown', function(e) {
key += e.keyCode;
console.log(key);
if (key === '9176' || key === '1776'){
key = '';
console.log('stopped');
return false;
}
});
$(document).on('keyup', function(e) {
key = '';
});
an example http://jsbin.com/xicegu/3/edit
Upvotes: 0
Reputation: 408
I agree with Barmar, but here you go:
document.addEventListener("keydown", function(e) {
if ((navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey) && e.keyCode === 76) {
e.preventDefault();
return false;
}
return true;
});
Upvotes: 0
Reputation: 14187
Well, it wasn't hard as I thought it would be.
The only thing which worked was e.preventDefault()
, I tried return false
but it didn't.
if(event.key == "l" && event.ctrlKey)
{
event.preventDefault();
// ...
// what I wanted to do
}
Thank you all for your quick answer and Pawan for yours in particular.
Upvotes: 1