Redirect webpage on key event - javascript

What I want to do is, configure a keyboard key, and then, when I click it, will redirect to another webpage.
Can someone tell me what is wrong with my code or if there's another way to do this?
function redirectpagina () {
window.location="Pagina KITT Parque.html"
}
window.addEventListener("keyup", function(e){ if(e.keyCode == 8) redirectpagina (); }, false);

keyCode only works with printable keys. For the others (including backspace), you have to use which.
window.addEventListener("keyup", function(e){ if(e.which == 8) redirectpagina (); }, false);

This is a complete working page (at least on my browser).
<html>
<body>
<h3>type G for Google</h3>
<script>
function redirectpagina () {
window.location="http://www.google.com"
}
window.addEventListener(
"keyup",
function(e){
if (e.keyCode > 13)
window.alert(e.keyCode);
if(e.keyCode == 71)
redirectpagina ();
});
</script>

Related

Capture keyDown for current page only

I have forms on different pages of my applications. Upon pressing 'Enter' or 'Esc', the form on the current page must be 'Submitted' or 'Cancelled'. The keydown() function should be triggered anywhere on the page and not tied to a specific DOM element.
.js
$(document).keydown(function(e) {
if(e.which == 13) {
// enter pressed
$('#submitCreateAccountForm').click();
$('#submitForm').click();
$('#submitNewSubmissionForm').click();
}
if(e.which == 27) {
// esc pressed
$('#submitCreateAccountFormCancel').click();
$('#submitFormCancel').click();
$('#submitNewSubmissionFormCancel').click();
}
});
What should 'document' be replaced by? Thanks
try this
$(function(){
$('html').bind('keypress', function (e) {
if (e.keyCode == 13) {
//do somethings
}
else if (e.keyCode == 27) {
return false;
}
});
});
You can try this code:
$("body").keyup(function(event){ // bind keyup event to body
if(event.keyCode == 13){ // 13 - code of enter key (find for ESC)
$("#enter").click(); // bind enter press for clicking botton with id="enter" and corresponding actions
}
});

Continue loading when pressing 'esc' key in browser

Pressing esc in most browsers stops the request. However I want to override this and continue the request.
I have tried the following but no success.
$(document).on("keyup", function(e) {
if (e.keyCode == 27) { // escape key maps to keycode `27`
e.preventDefault();
}
});
Is this possible to achieve?
try this one
document.attachEvent("onkeydown", keydown);
function keydown() {
// esc
if (event.keyCode = 27) {
event.returnValue = false;
event.keyCode = 0;
}
}

How to trigger arrowkey presses in Chrome?

In chrome(windows), I can capture keypresses on characters, but not on the arrowkeys. See sample-code below:
$('body').on('keypress', function(e) {
console.log('Only works on charcters, in chrome')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
How can I capture arrow-key-presses?
Try changing keypress to keyup:
$('body').on('keyup', function(e) {
console.log('Works on everything :)')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I think keydown is working fine
$('body').on('keydown', function(e) {
console.log('Only works on charcters, in chrome')
});
Fiddle
I really like this module for key press triggers:
https://github.com/madrobby/keymaster
It really reduces the amount of boilerplate code you need to write when working with key presses.
// define short of 'down'
key('down', function(){ alert('you pressed down') });
How can I capture arrow-key-presses?
Use e.keyCode to detect which key is pressed.
Like this :
$('body').on('keyup', function(e) {
if (e.keyCode == '38') {
alert("up arrow");
}
else if (e.keyCode == '40') {
alert("down arrow");
}
else if (e.keyCode == '37') {
alert("left arrow");
}
else if (e.keyCode == '39') {
alert("right arrow");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Jquery KeyPress Doesn't work

Any idea why this doesn't work whatsoever on any browser?
If i try it in jsfiddle it works.
$('#input_area').keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
}
});
HTML
<script type="text/javascript" src="counts.js"></script>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
</head>
<body>
<input type="text" id="input_area"/>
</body>
JS
$(document).ready(function() {
$('#input_area').keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
}
});
});
I am ready to bet 5 bucks that you didn't wrap it in a document.ready handler in your actual application which jsfiddle does by default:
$(function() {
$('#input_area').keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
}
});
});
Another possibility is you forgot to reference jQuery or you referenced it from a wrong url.
Depending on the browser, the which property might not be implemented. You should also check for keyCode:
$(document).ready(function() {
$("#input_area").keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
alert('You pressed enter!');
}
});
});
If it works when running in jsfiddle, then it works. I suspect you're trying to register the keypress listener when the dom is not loaded yet, and input_area is not available yet. Wrap it inside $(document).ready :
$(document).ready(function() {
$('#input_area').keypress(function(e) {
if (e.which == 13) {
alert('You pressed enter!');
}
});
});
Try to use binding,
$('#input_area').bind('keypress', function(e){
alert(e.which);
});

Javascript: Escape key = browser back button

In a browser how can I make the keyboard's escape key go back in Javascript.
For example: if you visit this page and click the "Fullscreen" link I'd like to press the escape key and go back to the previous page.
What's the Javascript to make this magic happen?
You can add a Key-Listener:
window.addEventListener("keyup", function(e){ if(e.keyCode == 27) history.back(); }, false);
This will call history.back() if the Escape key (keycode 27) is pressed.
$(document).bind("keyup", null, function(event) {
if (event.keyCode == 27) { //handle escape key
//method to go back }
});
You can bind an onkeyup event handler to window and check if the keycode is 27 (keycode for Escape), then use the window.history.back() function.
window.onkeyup = function(e) {
if (e.keyCode == 27) window.history.back();
}
MDC docs on window.history, https://developer.mozilla.org/en/DOM/window.history
Just listen for key code 27 and call history.go(-1);
You need to listen for the 'ESC' keypress, and fire off the back action when it is pressed, like so:
document.onkeydown = function(e){
if (window.event.keyCode == 27) {
history.go(-1);
}
};

Categories

Resources