how to simulate keypress for unit testing in jasmine - javascript

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);

Related

Javascript - Disable Key Down selectively

My application has a image palette. I am using key board shortcuts to move next and previous images. The following is the code:
window.addEventListener('keydown', function(e) {
if (e.which == 37 || e.which == 65) {
beforeAfterImages(1);
gtfval();
}
if(e.which == 39 || e.which == 68) {
beforeAfterImages(2);
gtfval();
}
})
In the same page, I have a button, that opens a modal window. The modal window has fields for text input. During text input When I press a or d the image palette behind the modal keeps moving. Can I disable the keydown functions, when I open the modal window. I tried the following:
$('#myModal').click(function() {
window.addEventListener('keydown', function(e) {
if (e.which == 37 || e.which == 65) {
return false;
}
if(e.which == 39 || e.which == 68) {
return false;
}
})
});
When you open the modal, you can edit a variable, like var isModalOpen. Then check that everytime the user presses the keys:
window.addEventListener('keydown', function(e) {
if(modalIsOpen) return; // this
if (e.which == 37 || e.which == 65) {
beforeAfterImages(1);
gtfval();
}
if(e.which == 39 || e.which == 68) {
beforeAfterImages(2);
gtfval();
}
})

Disable print screen option in IE,but the code works fine with google chrome

Print screen option has been disabled in Google Chrome with the below js code.
document.onkeydown = keydown;
document.onkeyup = keyup;
function keydown(e) {
console.log("key down triggered");
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (e.keyCode == 44 || e.keyCode == "44" || e.which == 44 || e.which == "44") {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.ctrlKey && (e.key == "P" || e.key == "C" || e.key == "A" || e.key == "p"||e.key == "c" || e.key == "a" || e.charCode == 16 || e.charCode == 112 ||e.keyCode == 80) || (e.keyCode == 44) || (e.keyCode == 123)) {
//alert("Inspect element & Print &cut/copy option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode > 111 && e.keyCode < 124) {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.key == "F11" || e.key == "f11") {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
}
function keyup(e) {
debugger;
console.log("key up triggered");
if (e.keyCode == 44 || e.keyCode == "44" || e.which == 44 || e.which =="44") {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode > 111 && e.keyCode < 124) {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.key == "F11" || e.key == "f11") {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
The above code works fine in Google Chrome.key code 44 for Print screen option is used.In IE all the function keys have been restricted,but the keyup and keydown function is not getting triggered,when in IE browser alone.
What is the alternate way to handle print screen option in IE Browser using jquery.?
And additionally the above code fails to prevent print screen when the user opens a bootbox alert pop up inside a web application.
Suggest a solution for the above 2 scenarios.
Don't.
No matter how clever your code, you can't stop me from focussing a different window on my second monitor and hitting Print Screen there to get a copy of whatever's on-screen that you're trying to stop me from screenshotting.
Or I could just open the browser console (even if you block F12 / Ctrl+Shift+I, I can still get there via the browser's menus) and type document.onkeydown = document.onkeyup = null; and break your entire guard.
It's not often that I "answer" a question by completely shutting it down, but you are wasting your time.

Space press change event

I would like to press the spacebar to cause the first event. Once that event happens, I would like to press the spacebar again and cause the second event to happen... repeat. -- like play/pause controls in video BUT NOT VIDEO --- I want to this to html id= events!!!
For example: the following codes works fine to 'click' the event 'play' using Spacebar, and 'pause' using (P). but I want to 'pause' using Spacebar as well.
document.onkeydown = function(evt) {
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if (evt.keyCode == 32) {
play.click();
}
if (evt.keyCode == 80) {
pause.click();
};
var playing = false;
document.onkeydown = function(evt) {
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if (evt.keyCode == 32) {
if (playing) { pause.click(); }
else { play.click(); }
playing = !playing;
}
if (evt.keyCode == 80) {
pause.click();
};

Disable keypress function when textarea has focus, re-enable function when textarea loses focus

I have a function which gets called on any keypress.
On textarea focus I disable the keypress function.
On textarea blur my keypress function call should be enabled again, but for some reason it is not.
This is the page: http://119.247.250.128/wasyoku/
function Kyprime()
{
document.getElementById('q').onfocus = function() {return false};
if (event.keyCode == 4) turn() // control d
if (event.keyCode == 126) hideframe() // ~
if (event.keyCode == 96) { // `
parent.C.location = (parent.C.location != wasyoku+"home/prime.html")
? "home/prime.html" : "n/FBwasyoku.html" }
if ( parent.C.location != wasyoku + "ascii.html"
&& parent.C.location != wasyoku + "masterpiece/pagelet.html"
&& parent.C.location != wasyoku + "masterpiece/artwork.html"
&& parent.C.location != wasyoku + "masterpiece/webwork.html")
{
if (event.keyCode == 32) // spacebar =
parent.C.location = "../masterpiece/pageletNews.html"
else if (event.keyCode == 3 | event.keyCode == 13) // enter return
parent.C.location = "../ascii.html"
}; document.onkeypress = Kyprime
How can I make my keypress function keep on working after textarea blur?
You could create two event listeners to define whether your keypress event should trigger the Kyprime function or not:
var textarea = document.getElementById('q');
textarea.addEventListener('blur', function() {
document.onkeypress = Kyprime;
});
textarea.addEventListener('focus', function() {
document.onkeypress = null;
});
Then on your Kyprime function you could remove this line:
document.getElementById('q').onfocus = function() {return false};
WORKING EXAMPLE: https://jsfiddle.net/czaj1a0s/1/

Detect keycode ALT+L via 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

Categories

Resources