Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Every One,

I am selecting radio button in grid view on key press from keyboard. I am selecting radio button on selection of 'a' and 'd' key press.
But now i want to make combination of 'alt' key combination with keys 'a' and 'd'.
So give me any idea how can i do this. following is code which i am using to perform key events. But i am not understanding that how can i make combination of alt key with this.

please help me...

<script type="text/javascript">

       document.onkeypress = clickRadio;

       function clickRadio(e) {
           if (window.event)
               var keyCode = window.event.keyCode;       // IE
           else
               var keyCode = e.which;

               if (keyCode == 65 || keyCode == 97 || keyCode == 68 || keyCode == 100) {
                   setRadioButton(keyCode);
               }
       }
       function setRadioButton(whichKey) {}</script>
Posted
Updated 26-Jun-12 19:38pm
v2

Try This:

Put This In The Body:

JavaScript
<BODY onload="document.body.focus();" onkeydown="AltDown();">


This Is The Function To Detect The ALT:

JavaScript
function AltDown() {
if (window.event.altLeft) {
alert("altLeft Pressed");
}
else {
if (window.event.altKey) {
    alert("altRight Pressed");
}
}

//To Cancel The Event
window.event.returnValue = false;

document.body.focus();
}
 
Share this answer
 
v2
Comments
krumia 27-Jun-12 1:44am    
WARNING: shiftLeft, shiftRight, ctrlLeft, ctrlRight, altLeft, altRight properties of window.event are IE only. Consider that if you're using other browsers.
This is an extract from http://www.w3schools.com/jsref/event_onkeypress.asp[^]

Quote:
The onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers. To detect only whether the user has pressed a key, use the onkeydown[^] event instead, because it works for all keys.


You can use event.altKey, event.ctrlKey, event.shiftKey predecates to check if respective modifiers are pressed.
 
Share this answer
 
Comments
coolnavjot31 27-Jun-12 2:05am    
Hello

I have solved this. you are right i Have solve this by using onkeydown event.
following is my solved code.

<script type="text/javascript">
document.onkeydown = clickRadio;

function clickRadio(e) {

if (window.event)
{
var keyCode = window.event.keyCode; // IE
}
else
{
var keyCode = e.which;
}

if (e.altKey && (keyCode == 65 || keyCode == 97 || keyCode == 78 || keyCode == 110))
{
setRadioButton(keyCode);
}
}
</script>
krumia 27-Jun-12 5:56am    
Great to hear that. You can enter your code as a solution here. Please do so. It will be useful for others. And you get some reputation points too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900