Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  131] [ 1]  / answers: 1 / hits: 15470  / 14 Years ago, fri, april 9, 2010, 12:00:00

I know this exact question was asked here, but the answer didn't work for what I needed to do so I figured I'd give some example code and explain a bit...



$(document).keypress(
function (event) {
// Pressing Up or Right: Advance to next video
if (event.keyCode == 40 || event.keyCode == 39) {
event.preventDefault();
$(.current).next().click();
}
// Pressing Down or Left: Back to previous video
else if (event.keyCode == 38 || event.keyCode == 37) {
event.preventDefault();
$(.current).prev().click();
}
}
);


It basically disables the arrow keys to use them for something else, but doing:



$(document).keypress(function () { });


doesn't enable the default function again... I need it to scroll the page without having to create a scroll function for it...



Any ideas?



Thanks,

Matt


More From » jquery

 Answers
6

I'm not sure this is the right way to handle it.



A better way to approach this problem would be to put some kind of check inside your document.keypress instructions.. like..



var enableKeys = false;

$(document).keypress(
function (event) {
// Pressing Up or Right: Advance to next video
if (event.keyCode == 40 || event.keyCode == 39 && enableKeys) {
event.preventDefault();

$(.current).next().click();
}
// Pressing Down or Left: Back to previous video
else if (event.keyCode == 38 || event.keyCode == 37 && enableKeys) {
event.preventDefault();
$(.current).prev().click();
}
}
);


Then control the enablekeys wherever you feel necessary, either with a hover, or something along those lines.


[#97121] Tuesday, April 6, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zaynerogerb

Total Points: 454
Total Questions: 109
Total Answers: 97

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
zaynerogerb questions
;