Hello i'm making an app where i need to obtain keyboard access to use the left arrow key and the right arrow key of the keyboard, my app is html5 and javascript, it's pretty easy to do this with the robot class of java, now my question is... There is a way to do this with javascript ?
Asked
Active
Viewed 385 times
-2
-
The Java Robot class "Simulate" the event of the keydown, i don't need to know when a key is pressed i need to press the key with code – Daniel B Feb 06 '14 at 02:54
-
1I see - next time please consider adding a bit of desired behavior more than "same as XXXX" directly in your post (I've updated title, feel free to revert/improve). Side note: what you trying to do is somewhat strange - if it is your own site - refactor to allow direct calls, if it is external site you'll run into same-origin restrictions and not be able to trigger events... – Alexei Levenkov Feb 06 '14 at 03:12
-
Thank you Alexei, is a desktop html5 app with motion detect for help desk – Daniel B Feb 06 '14 at 03:20
2 Answers
1
You could do something like this:
$(document).keydown(function(event){
var key = event.which;
switch(key) {
case 37:
// Key left. Call function to do Key left work
break;
case 38:
// Key up. Call function to do Key up work
break;
case 39:
// Key right. Call function to do Key right work
break;
case 40:
// Key down. Call function to do Key down work
break;
}
});
these are the keycode arrow values:
left 37
up 38
right 39
down 40
EDIT: If you just want to programically press a certian key through javascript you can just do this:
jQuery.trigger({ type: 'keypress', which: keycode });

Jason Roell
- 6,679
- 4
- 21
- 28
-
The Java Robot class "Simulate" the event of the keydown, i don't need to know when a key is pressed i need to press the key with code – Daniel B Feb 06 '14 at 02:52
-
@user3215218 When do you want the key pressed? There needs to be an event to trigger the press of a key – Jason Roell Feb 06 '14 at 02:55
-
When i make a specific move, i got the event but don't know how to take control of keyboad and press the key, i don't know if this is possible, in java it's really easy, but really don't know how in javascript – Daniel B Feb 06 '14 at 02:57
-
@user3215218 Okay I edited my answer to show how to press the key through JavaScript. – Jason Roell Feb 06 '14 at 03:03
-
+1 Note that if code running on a web page it will not work cross-domain - you'll only be able to post events to your own page (or at most pages on the same domain) – Alexei Levenkov Feb 06 '14 at 03:13
0
You would probably use a combination of keyCode 37 and 39. See documentation here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
jQuery makes this very easy: http://api.jquery.com/event.which/

Harvey A. Ramer
- 763
- 7
- 13
-
The Java Robot class "Simulate" the event of the keydown, i don't need to know when a key is pressed i need to press the key with code – Daniel B Feb 06 '14 at 02:53
-
1Ah, then @user3215218 you will probably want to see this thread: http://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript – Harvey A. Ramer Feb 06 '14 at 03:12