I'm using the Camera jQuery slideshow on my site, but it lacks being able to control with keyboard which I need. In their google group someone posted the previous/next commands which work, however I still need the pause/resume keyboard button to work (in my case I'm using the spacebar). What would I need to enter to have .camera_play also controlled by the spacebar? Is that even possible?
Here is the code that pertains to the problem:
$(document.documentElement).keyup(function (event) {
// handle cursor keys
if (event.keyCode == 37) {//go left
$('.camera_prev').click();
} else if (event.keyCode == 39) { //go right
$('.camera_next').click();
} else if(event.keyCode == 32) { // spacebar to stop
$('.camera_stop').click();
After seeing how camera plugin works, you can use the :visible selector to click on the currently active action (which may be stop or resume), like this:
$(document.documentElement).keyup(function (event) {
// handle cursor keys
if (event.keyCode == 37) {//go left
$('.camera_prev').click();
} else if (event.keyCode == 39) { //go right
$('.camera_next').click();
} else if(event.keyCode == 32) { // spacebar to stop
$('.camera_stop , .camera_play').filter(':visible').click();
}
});
The documentation doesn't show this, but I dug into the code and it looks like when you click the stop button, it adds a class to the main wrapper called 'paused'. Here's how you can update the your code to handle both pausing and starting with the space bar.
$(document.documentElement).keyup(function (event) {
// handle cursor keys
if (event.keyCode == 37) {//go left
$('.camera_prev').click();
} else if (event.keyCode == 39) { //go right
$('.camera_next').click();
} else if(event.keyCode == 32) { // spacebar to stop
if($('.camera_wrap').hasClass('paused')){
$('.camera_play').click();
}
else{
$('.camera_stop').click();
}
}
}
Related
Let's assume the simple pong game for two players with one keyboard.
The movement handler appears to be easy:
document.addEventListener('keydown', function (event) {
if (event.keyCode === 87) { //w
movePaddle(leftPaddle, 0);
} else if (event.keyCode === 83) { //s
movePaddle(leftPaddle, 1);
} else if (event.keyCode === 38) { //arrow_up
movePaddle(rightPaddle, 0);
} else if (event.keyCode === 40) { //arrow_down
movePaddle(rightPaddle, 1);
}
});
The second argument in movePaddle function just means the direction (up or down).
The problem: if W and Up Arrow are pressed together, then only one paddle moves. It depends on which key were pressed last.
How to handle keydown events correctly?
I need two paddles moving together according pressed keys. If W and Up Arrow, then both go up. If W and Down Arrow, then left goes up and right goes down. Of course, only one paddle should move if only one key are pressed.
https://gist.github.com/RepComm/52690eadaa9f4a3e7a7819b3267feca4
Use it like this: (In a loop of any kind, this one just uses anim frame)
Input.init();
var test = function () {
window.requestAnimationFrame(test);
if (Input.isKeyDown("w")) {
console.log("Forward March!");
}
}
window.requestAnimationFrame(test);
Try separating the keys by saying
document.addEventListener('keydown', function (event) {
if (event.keyCode === 87) { //w
movePaddle(leftPaddle, 0);
} else if (event.keyCode === 83) { //s
movePaddle(leftPaddle, 1);
}
// Separate them here
if (event.keyCode === 38) { //arrow_up
movePaddle(rightPaddle, 0);
} else if (event.keyCode === 40) { //arrow_down
movePaddle(rightPaddle, 1);
}
});
This will work because you're saying else if for every single one, that means that only one of them can be true at the same time.
What I'm saying is that when using else if, the else if is paired to the if, and much like just else, only one of the blocks can be true at the same time
I have a chat area, where messages go, and a chat input below. As you can see in this structure:
After load I run this function:
function activate_key_detection()
{
$(document).keydown(function(e)
{
code = (e.keyCode ? e.keyCode : e.which);
if(e.ctrlKey)
{
if(window.getSelection().toString() != "")
{
return;
}
}
$('#chat_input').focus();
if(code == 13)
{
send_to_chat();
e.preventDefault();
return false;
}
if(code == 38)
{
$('#chat_input').val(last_input);
e.preventDefault();
return false;
}
if(code == 27)
{
if($('#overlay').css('display') == 'block')
{
hide_boxes();
e.preventDefault();
return false;
}
else
{
clear_input();
}
}
});
}
As you can see it first focuses the input, and when the key is the up arrow (38) it catches it, modifies the input and tries to prevent the default behavior of they key.
The problem is, when I first click inside the chat area, and then press the up arrow, it will still scroll the chat area a line. Subsequent presses of the up arrow don't scroll the chat area further.
I tried adding e.stopPropagation() there too, to no avail.
Any help is welcome.
I am using the JQuery UI Dialog to create a pop-up. The pop-up has two buttons. The first button is automaticly selected by JQuery. I can change the selection between the buttons and the exit button with 'tab'.
I want to change the selection (only between the two buttons) also with the left and right arrow keys on the keyboard.
Where do I have to catch the arrow key down events and how can I change the focus of the buttons?
Thanks for your help!
I would do it something like that :)
$('body').on('keydown', '#terug', function(e) {
if (e.keyCode === 39) { //right arrow
$('#ok').focus();
}
});
$('body').on('keydown', '#ok', function(e) {
if (e.keyCode === 37) { //left arrow
$('#terug').focus();
}
});
Try it :) And if this won't work then go global without specifying selector in event definition:
$('body').on('keydown', function(e) {
if (e.keyCode === 39 && $('#terug').is(':focus')) { //right arrow
$('#ok').focus();
}
});
Hope this will help! :) If not give me a comment and i will try to fix this. :)
Thanks for your help! It worked. I added my solution to complete this question.
I bind the keydown event only on the ui-buttons:
$(document).on('keydown', '.ui-button', handleUIButtonKeyDown);
After that I handle the left and right arrow keys
function handleUIButtonKeyDown(event) {
if (event.keyCode == 37) {
//left arrow key pressed, select the previous button
makeButtonFocus($(this).prev(".ui-button"));
} else if (event.keyCode == 39) {
//right arrow key pressed, select the next button
makeButtonFocus($(this).next(".ui-button"));
}
}
function makeButtonFocus($button) {
$button.addClass("ui-state-focus");
$button.focus();
}
Here's a more generic answer
works on any number of buttons irregardless of DOM structure (btns need not be siblings)
$('body').on('keydown', function(e) {
if (e.keyCode === 37) { //left arrow
modalKeyboardNav("prev")
} else if (e.keyCode === 39) { //right arrow
modalKeyboardNav("next");
}
});
function modalKeyboardNav(dir) {
if (!$("body").hasClass("modal-open")) {
// no modal open
return;
}
var $curModal = $(".modal.show"),
$curFocus = $(document.activeElement),
$focusable = $curModal.find(".btn"),
curFocusIdx = $focusable.index($curFocus);
if (curFocusIdx < 0) {
// nothing currently focused
// "next" will focus first $focusable, "prev" will focus last $focusable
curFocusIdx = dir == "next" ? -1 : 0;
}
if (dir == "prev") {
// eq() accepts negative index
$focusable.eq(curFocusIdx - 1).focus();
} else {
if (curFocusIdx == $focusable.length - 1) {
// last btn is focused, wrap back to first
$focusable.eq(0).focus();
} else {
$focusable.eq(curFocusIdx + 1).focus();
}
}
}
I have an iframe which contains slider. The slider allows navigating between pictures with left/right key arrows but if there is horizontal scroll on the page then the page is scrolled as well with left/right key arrows. Is there any way to prevent page scrolling but not the navigation between slides in the iframe.
The code looks like this:
if (e.keyCode === 39) {
next();
}
if (e.keyCode === 37) {
prev();
}
Thanks.
you should apply e.preventDefault(); in order to terminate the default behavior of an event.
some kind of
$(document).on("keypress", function () {
if (e.keyCode === 39 || e.keyCode === 37) {
return
}
});
Simply I have a js script that change the page with left and right arrows, but how to stop that if a specific textarea is selected ?
This is my js to change the page
$(document).keydown(function(event) {
if(event.keyCode === 37) {
window.location = "http://site.com/pics/5";
}
else if(event.keyCode === 39) {
window.location = "http://site.com/pics/7";
}
});
$('textarea').on('keypress', function(evt) {
if ((evt.keyCode === 37) || (evt.keyCode === 39)) {
console.log('stop propagation');
evt.stopPropagation();
}
});
See example fiddle: http://jsfiddle.net/GUDqV/1
Update: after OP clarification this works even on jQuery 1.2.6 on Chrome: http://jsfiddle.net/GUDqV/2/
$('textarea').bind('keyup', function(evt) {
if ((evt.keyCode === 37) || (evt.keyCode === 39)) {
console.log('stop propagation');
evt.stopPropagation();
}
});
see screenshot of this code on Chrome and jQ1.2.6
Probably the simplest approach is to factor event.target into your code, checking to see if it is the textarea:
$(document).keydown(function(event) {
if (event.target.id == "myTextArea") {
return true;
}
else if(event.keyCode === 37) {
window.location = "http://site.com/pics/5";
}
else if(event.keyCode === 39) {
window.location = "http://site.com/pics/7";
}
});
Any key events that originate from a textarea element with an id of myTextArea will then be ignored.
You can check if the textarea is in focus by doing something like:
if (document.activeElement == myTextArea) {
// Don't change the page
}
$("#mytextarea").is(":focus") This will let you know if the element is focused.
Also $(document.activeElement) will get the currently focused element.
You can check to see if your text area is focused, and disable the script that navigates when using left and right arrow keys.
A little bit of code showing what you've tried might bring in more specific responses.
Hope this helps.