Detect keycode ALT+L via Javascript - javascript

I try to detect if a user presses F12 or ALT + L.
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123 || (event.keyCode == 18 && event.keyCode == 76)) {
//do anything
return false;
}
}
What am I doing wrong?

event.keyCode contains only one value. You can use event.altKey do detect if the alt key is pressed.
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123 || (event.keyCode === 76 && event.altKey)) {
//do something
return false;
}
}

The standard way is to create a bool to detect if the 'alt' key is currently held down and then a separate if to detect if that is true and if the L key as just been pressed - see the fiddle:
http://jsfiddle.net/L4cb9/1
var held = false;
...
else if (event.keyCode == 18) {held = true;}
if (held == true && event.keyCode == 76) {
alert();
}
...
document.onkeyup = function(event) {
if (event.keyCode == 18) {held = false;}
}
This is applicable to holding any combination of keys - you can create an array for multiple key holds greater than two:
held = [];
...
if (event.keyCode == i) {held[i] = true;}
...
and so on

Related

Need help restricting a js function(that restricts invalid chars) to a particular input type

Hi i would like to restrict a function that allows only, numbers, back space and left & right arrow keys to inputs with number type, because when i implement it, it also affects my text inputs.
<script>
function chars(evt){
var key = window.event ? event.keyCode : event.which;
if (event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
return true;
}
else if ( key < 48 || key > 57 ) {
return false;
}
else return true;
}
</script>
Assign an id to your <input>. Add an event listener to it, like :
function getKeyCode() {
var key = window.event ? event.keyCode : event.which;
if(event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
console.log(true);
//return true;
} else if (key < 48 || key > 57) {
console.log(false);
// return false;
} else {
console.log(true);
// return true;
}
}
var el = document.getElementById("myInput");
el.addEventListener("keypress", getKeyCode);
<input type="text" id="myInput">

how to simulate keypress for unit testing in jasmine

I need to unit test function that is triggered when key pressed.
public onKeyDown(event: KeyboardEvent): void {
if (event.ctrlKey && event.keyCode === 38) {
console.log('increase');
}
if (event.ctrlKey && event.keyCode === 40) {
console.log('decrease');
}
/* Prevent entering characters */
if (event.keyCode >= 65 && event.keyCode <= 90) {
return;
}
}
How can I simulate keypress to satisfy the fist condition, for example?
The example code below shows how an event is created, triggered, and intercepted.
var keyPressed = null;
function keyPress(key) {
var event = document.createEvent('Event');
event.keyCode = key;
event.initEvent('keydown');
document.dispatchEvent(event);
}
document.addEventListener('keydown', function(e){
keyPressed = e.keyCode;
});
keyPress(37)
alert(keyPressed);

Disable enter key and space if iframe body is empty onload

How to check if iframe content is empty then then disable "enter key" and "space"
else if Enable.
DEMO HERE
var iframeContainerVal = $("#textEditor").contents().find("body").text();
var container = document.getElementById("textEditor").contentWindow;
function checkPress(){
alert('In Fn')
$(container).keypress(function(event){
if (event.keyCode == 10 || event.keyCode == 13 || event.keycode == 32 ) {
alert('aaa');
event.preventDefault();
}
});
}
if(iframeContainerVal == true) {
checkPress()
}
else {
alert('false')
}
Use return false;
function checkPress(){
alert('In Fn')
$(container).keypress(function(event){
if (event.keyCode == 10 || event.keyCode == 13 || event.keyCode == 32 ) {
return false;
}
});
}
you used event.keycode == 32 in your If statement But, it should capital C in keycode like
event.keyCode == 32
Check for iframe empty content , iframe id = 'if'
if ($('#iframe').contents().find('body').children().length == 0) {
alert('I frame is empty');
if (event.keyCode == 32 || event.keyCode == 13) {
return false;
}
}

Event is not defined error in javascript only Firefox

I use the following script to validate the text box to enter only numbers and (.) which means it is decimal textbox validation. It was work fine in Internet Explorer and Google Chrome. If I execute the function in FireFox I get the following Error:
Event Is not Defined.
How to solve this?
function abc(event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
I call this function like this:
$('.decimalValidate').live('keypress',function(){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(decimalval);
if(decimalvalidate == false)
return false;
});
I assign this validation for text box like this:
input type="text" id="Total" class="abc"
Try this
function abc(event) {
if(!event)
event= window.event;
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
and
$('.decimalValidate').live('keypress',function(e){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(evt); //keypress event
if(decimalvalidate == false)
return false;
});
decimalval is not an Event object, and you have to pass it to the abc function in ordert to find out which key you pressed:
$('.decimalValidate').live('keypress',function(ev){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(ev);
if(decimalvalidate == false)
return false;
});
$('.decimalValidate').live('keypress',function(e){
var decimalvalidate=abc(e); //this will point to the event of the keypress.
if(decimalvalidate == false)
return false;
});
I am not sure why you did all of the decimalid and decimalval operations, but if you want the event, do as I wrote in the edited code above.
Good luck.
$('.decimalValidate').on('keypress',function(event){
var decimalid = $(this).attr("id");
var decimalval = $('#'+decimalid).val();
var decimalvalidate = abc(event);
if(decimalvalidate == false)
return false;
});
function abc(event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
It helps you..

How to set combination of key codes in javascript

I use this code for replacing Enter key with TAB key :
function EnterTab() { if (event.keyCode == 13) event.keyCode = 9; return false; }
I want to know what can I do if I want replace right arrow key with SHIFT + TAB?
if(event.keyCode == 39){
event.shiftKey = true;
event.keyCode = 9;
return false;
}

Categories

Resources