Click here to Skip to main content
15,888,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had two list boxes and some buttons.

I am trying to do the button click event by using keyboard shortcut.

That is, suppose if I press F1 key it should perform same as the functionality if I press button1.

Can any please help me how to write the code?
Posted
Updated 20-Apr-11 2:26am
v2

Firstly, don't use F1 - the standards say that is for "Help" and it is not a good idea to change key allocations like that - it confuses users.

Secondly, handle the KeyPress event[^] (alphanumeric keys), and check what key is passed to you in the e.KeyChar parameter.
For non-alphabetic keys, handle the KeyDown event[^] instead, and use the e.KeyCode parameter.

[edit]Forgot to add the KeyDown link to MSDN - OriginalGriff[/edit]
 
Share this answer
 
v2
<script type="text/javascript">
    document.onkeydown = CallClickEvent;    
    function CallClickEvent(e)
    {
        keynum = window.event ? event.keyCode : e.which
        
        if(keynum == 112) //F1
            document.getElementById('<%=Button1.ClientID %>').click()
        else if(keynum == 113)//F2
            document.getElementById('<%=Button2.ClientID %>').click()    
        else if(keynum == 114)//F3
            document.getElementById('<%=Button3.ClientID %>').click()    
        else if(keynum == 115)//F4
            document.getElementById('<%=Button4.ClientID %>').click()    
    }
    
    function buttonClicked(ctrl)
    {
        alert(ctrl.id + ' clicked');
    }
    </script>

 <input id="Button1" type="button" value="Button1"  runat="server"  önclick="buttonClicked(this)" /><input
        id="Button2" type="button" value="Button2" runat="server" onclick="buttonClicked(this)" /><input
            id="Button3" type="button" value="Button3" runat="server" onclick="buttonClicked(this)" /><input>
                id="Button4" type="button" value="Button4"  runat="server"  önclick="buttonClicked(this)" />
/xml></input>
 
Share this answer
 
v2
Hope this[^] might give you an idea.
 
Share this answer
 
Depending on the keys you wish to use, you might be able to do this without writing any code at all, by using Accelerator Keys.

e.g. If you have a 'Down' button then setting the Text property to &Down will cause the button text to display as Down and pressing Alt-D will cause the button to perform it's click method.
 
Share this answer
 

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