How to detect backspace key? - javascript

How can I also choose if e.which == 'backspace' ? at the moment, this function helps with showing a loader when info from a database is being retrieved, though i want the loader to show when the backspace is pressed.
field.focus().keypress(function(e) {
e.which !== 0 ? loader.show() : loader.hide();
});

field.focus().keyup(
function(e){
if(e.keyCode === 8)
alert('backspace trapped')
}
)

Related

Zoom in - Zoom out problem on the Javascript Background

I want to apply a javascript background on my website. I found a ready one, but I recognize a problem on it. When you scrool with mouse on browser, zoom in or zoom out, it starts to look so strange. You can try it on your own from that demo link;
Demo
Also the html and css code of this animational background is in this link;
Link of HTML-CSS-Javascript of this demo
Can you help me, how can I prevent it from being zoomed in or zoomed out on a browser ?
You can only do it using jquery with below code,
$(document).keydown(function(event) {
if (event.ctrlKey==true && (event.which == '61' || event.which == '107' || event.which == '173' || event.which == '109' || event.which == '187' || event.which == '189' ) ) {
event.preventDefault();
}
// 107 Num Key +
// 109 Num Key -
// 173 Min Key hyphen/underscor Hey
// 61 Plus key +/= key
});
$(window).bind('mousewheel DOMMouseScroll', function (event) {
if (event.ctrlKey == true) {
event.preventDefault();
}
});
Note: Make sure you have included jquery in your code before this script.

Ctrl+u double make not hide

The below code works fine, but if I click double Ctrl+u then it opens all. How can I disable all?
Ctrl+u, Ctrl+s, right-click, F12 key and more key for hide code?
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) { //Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
}
return false;
};
you have to put e.stopImmediatePropagation();
/*function check(e)
{
alert(e.keyCode);
}*/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) { //Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
e.stopImmediatePropagation();
}
return false;
};
Try to use the e.preventDefault() function. it will stop the browser to do the default actions when in this case a key combination has been pressed.
The key code for the F12 button is 123. To detect the 'contextmenu' event (user clicks right button), you also have to use the preventdefault function to avoid opening the contextmenu. Maybe this will help you:
Live preview: https://jsfiddle.net/cmLf34h3/1/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117) || e.keyCode === 123) { //Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
e.preventDefault();
}
return false;
};
window.oncontextmenu = function (e)
{
alert("You have tested if your right-mousebutton is still working. This alert confirms it's still working, have a nice day!")
e.preventDefault();
return false; // cancel default menu
}
Source for the right-click function: Is right click a Javascript event?
Note: You cannot 100% prevent these actions, there is always a backdoor to bypass this.
I hope this helps

How to focus a text box on pressing a key without entering that keyword into it

I want to focus into a text box when ever be user press 'S' keyword in keyboard. But the problem is that 'S' keyword also typed in text box (this only happens in chrome). How I can do this here is my code.
$(document).keypress(function(event){
if(document.getElementById('search')!==null)
{
if(event.which==115 && event.target.nodeName=='BODY') // WHEN USER PRESS 'S' KEYWORD IN KEYBOARD ONLY
{
$('#search').focus();
}
}
});
Use event.preventDefault()
$(document).keypress(function(event){
if($('#search').length)
{
console.log(1);
if(event.which==115 && event.target.nodeName=='BODY') // WHEN USER PRESS 'S' KEYWORD IN KEYBOARD ONLY
{
event.preventDefault();
$('#search').focus();
}
}
});
you can also try
(event.keyCode==83) // 83 is keyCode for "s"
By using the event.preventDefault() you can restrict the keypress to update the textbox value. i have added event.which == 83 in case of caps is on.
$(document).keypress(function (event) {
if (document.getElementById('search') !== null) {
if ((event.which == 115 || event.which == 83) && event.target.nodeName == 'BODY') // WHEN USER PRESS 'S' KEYWORD IN KEYBOARD ONLY
{
$('#search').focus();
event.preventDefault();
}
}
});
Check the Demo for JSFiddler DEMO

Hotkey to Button

Is there any way to make a keyboard shortcut that will click on a button on a webpage.
Like I would like to have a hotkey : Ctrl+S to automatically click on Search on Google.
I've tried this : Shortcut Manager plugin but I'm not sure how to assign it.
This might help:
See this jsFiddle
$(window).keypress(function(e) {
var keycode = (e.keyCode ? e.keyCode : e.which);
var rr = $('#report').html();
$('#report').html(rr + '<br />' + keycode);
if (e.ctrlKey) alert('Control pressed');
//if (!(keycode == 115 && e.ctrlKey) && !(keycode == 19)) return true;
if (!(keycode == 83 && e.ctrlKey) && !(keycode == 17)) return true;
alert("Ctrl-S pressed");
$('#gsearch').trigger('click'); //or just .click() also works
e.preventDefault();
return false;
});
Note that webkit browsers will not trap ctrl, alt, shift, etc keys. See this article for info

Know if input is in use and disable the hotkey (Jquery)

I use some hotkeys on my website, but when the user is inside the search form or inside comment. I want to disable them.
What the best for me to do it? Thanks
Example of my hotkey:
$(document).keydown(function(e)
{
if (e.which == 40 || e.which == 74) // next post
{
return scroll('next');
}
if (e.which == 38 || e.which == 75) // prev post
{
return scroll('prev');
}
});
You can check for the event.target element. If that element is from type INPUT you might want to omit the handler code. Could look like
$(document).keydown(function(e)
{
if( e.target.nodeName !== 'INPUT' ) {
if (e.which == 40 || e.which == 74) // next post
{
return scroll('next');
}
if (e.which == 38 || e.which == 75) // prev post
{
return scroll('prev');
}
}
});
You could check if e.target.nodeName === INPUT (the event is triggered inside an input field) and act accordingly

Categories

Resources