简体   繁体   中英

How do I assign keyboard shortcuts to asp.net buttons without Shift, Control, or Alt requirements?

I have a menu of items that are preceded by a number to act as a keyboard shortcut for rapid navigation. Think nine buttons with text like:

1 Open
2 Close
3 End

and so on. Actual sample button:

<asp:Button ID="bOpen" runat="server" Font-Bold="True" Font-Size="Large" 
            Text="1 Open" Width="100%" onclick="bOpen_Click" AccessKey="1" />

I want the user to press a number on the numpad, on the top row of numbers, or on a handheld external input device to navigate to other pages. Currently, I'm using the AccessKey property, which works for the hand held device (so I don't need help with this part) but not either of the keyboard number sets. On the keyboard, Alt + Shift are required when using AccessKey. I don't want multi-key shortcuts.

How do I make it so that a single key press activates these menu items?

The solution I ended up using was Javascript/jQuery.

$(document).keyup(function (e) {
    if (e.keyCode == 49 || e.keyCode == 97) { $("#<%=bOpen.ClientID %>").click(); }
    if (e.keyCode == 50 || e.keyCode == 98) { $("#<%=bClose.ClientID %>").click(); }
    ...(etc)
} 

I included two keycodes per button to account for both the numpad and the top number row. I took the keycodes from here:

http://www.webonweboff.com/tips/js/event_key_codes.aspx

I also left the AccessKey property alone, so that the external input device will still work the same as before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM