Reputation: 21
lets say I have a button like this:
<input id="a" type="button" value="A" />
and I want it so that if I press the "a" key on the keyboard, it visually acts as though I am clicking the button with the mouse. Is there a way to do that?
Upvotes: 0
Views: 71
Reputation: 2895
Have a look at HTML5's globalaccesskey
:
http://www.w3schools.com/tags/att_global_accesskey.asp
** EDIT **
Or if you are looking for a pure javascript way, you could try this:
document.onkeypress = textsizer;
function textsizer(e){
var evtobj=window.event? event : e;
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
var actualkey=String.fromCharCode(unicode);
if (actualkey=="e"){
document.getElementById('a').click();
}
}
Upvotes: 1
Reputation: 4262
document.onkeydown = function (evt) {
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if (keyCode == 65) {
document.getElementById("a").click();
// Your function here.
}
};
for other keycodes refer Keycodes
Upvotes: 2
Reputation: 9151
This should work: (Assumes JQuery)
$(document).keyup(function (event) {
if (event.which == 65) { // "A" key
$('#a').click();
}
});
Upvotes: -1