Keyboard Shortcut in ASP.NET(转)

Keyboard shortcuts are well known facility in Windows desktop applications and make application user's life much easier. Although beginners like to use a mouse, that habit usually disappear after some time when user becomes more experienced, because keyboard shortcuts enables faster and more efficient work. Many Windows applications, including Visual Studio .NET have so called "Expert Mode" when all toolbars and other visuals aids become invisible and user uses keyboard only.

Keyboard shortcuts are not used that much on Web applications. One of the most popular shortcuts used on web pages is using an Enter keyboard key to submit a web form. If you try some search on Google, after you insert a search terms in search text box, you can press Enter key instead of taking a mouse to click on the Search button. Complete research of Enter key issue you can find in Enter Key in ASP.NET tutorial. One more common shortcut on web pages is using a TAB key to move focus between a controls on a web form.

You don't need to be limited to this two cases only. You can implement shortcuts in your ASP.NET Web application like it is classic desktop application. Your Web application will be more efficient for experts, more accessible for people with disabilities and generally looks more professional if all functions can be easily accessed through a keyboard and through a mouse click Of course, there are some problems, not known in desktop environment. For example, your shortcut can call server ASP.NET function, client javascript or even call server Ajax function with client code. Also, some shortcuts are already reserved by Web browser. If you press CTRL + O in Internet Explorer or Firefox, you will get browser's Open file dialog, so obviously you can't use CTRL + O shortcut in your Web application.

To use shortcuts in your web application, there are some simple solutions, already implemented in HTML. One of them is using of Access Keys.

Access Keys [Alt + Key]

Access keys are common way to enable keyboard shortcuts on your web page. In short, site visitor can press Alt + some key to get a button clicked or get focus to some textbox. Both ASP.NET 1.1 and ASP.NET 2.0 provide AccessKey property for buttons and text boxes. ASP.NET 2.0 has additional AssociatedControlID property, used in Label control to specify which control will be clicked or get focus.

Access Keys ASP.NET 1.1 example (also works in ASP.NET 2.0)

<asp:Label ID="WonderfulLabel" runat="server" 
  Text="My <u>W</u>onderful function [ALT + W]">
</asp:Label><br />
<asp:Button AccessKey="W" ID="btnWonderful" runat="server"></asp:Button>

Access Keys ASP.NET 2.0 example

<asp:Label ID="WonderfulLabel" runat="server"
  AccessKey="W"
  AssociatedControlID="btnWonderful"
  Text="My <u>W</u>onderful function [ALT + W]">
</asp:Label><br />
<asp:Button ID="btnWonderful" runat="server"></asp:Button>

So, in example above, button btnWonderful will be clicked when ALT + W is pressed.

You should avoid keys reserved by Web browser. Internet Explorer 6 uses F, E, V, T and H to open File, Edit, View, Tools and Help menu items. Firefox uses additional G and B to call its Go and Bookmark menus.

More advanced shortcuts, using Ctrl, Alt and Shift

As you saw, Alt + Key shortcuts are pretty simple to implement, but what is with other key combinations, which use Ctrl or Shift key, or even their combinations? To achieve that, we must use a little javascript client code. The first question is, how to detect what Web site visitor pressed on its keyboard?

I wrote small javascript listing, which detects Ctrl, Alt, Shift and character pressed. You can copy and paste it to .aspx file, but it is not limited to ASP.NET project. It works in any static HTML or even PHP page too, because uses client javascript only. See it online at Detect SHIFT, ALT, CTRL & character key example.

First, I added handlers for keydown and keyup events and declare variables for storing state of keys:

       document.onkeydown = KeyDownHandler;
       document.onkeyup = KeyUpHandler;

        var CTRL = false;      
        var SHIFT = false;     
        var ALT = false;
        var CHAR_CODE = -1;

Second important thing, is to add functions KeyDownHandler and KeyUpHandler:

        function KeyDownHandler(e)
        {
            var x = '';
            if (document.all)
            {
                var evnt = window.event;
                x = evnt.keyCode;
            }
            else
            {
                x = e.keyCode;
            }
            DetectKeys(x, true);
        }
       
        function KeyUpHandler(e)
        {
            var x = '';
            if (document.all)
            {
                var evnt = window.event;
                x = evnt.keyCode;
            }
            else
            {
                x = e.keyCode;
            }
            DetectKeys(x, false);
        }

Finnaly, place state of the keys to variables, using DetectKeys function:

        function DetectKeys(KeyCode, IsKeyDown)
        {           
            if (KeyCode == '16')
            {
                SHIFT = IsKeyDown;
            }
            else if (KeyCode == '17')
            {
                CTRL = IsKeyDown;
            }
            else if (KeyCode == '18')
            {
                ALT = IsKeyDown;
            }
            else
            {
                if(IsKeyDown)
                    CHAR_CODE = KeyCode;
                else
                    CHAR_CODE = -1;
            }
       }

Shortcut Enabled ASP.NET Custom Controls

To speed up implementation of javascript used above, it is good idea to build shorcut enabled ASP.NET custom controls, that inherits standard controls. That is subject of the our next tutorial

转载于:https://www.cnblogs.com/jeffery19830101/archive/2007/08/13/854020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
伯努利shortcut是指运用伯努利原理进行简化计算或问题求解的方法。伯努利原理是流体力学中的基本原理,它描述了流体在不同速度下的压力变化关系。伯努利原理可以用来解决多种与流体运动相关的问题,如流体静力学、管道流动、空气动力学等。 伯努利shortcut是指在求解流体流动问题时,通过应用伯努利原理以及相关的简化假设和公式,可以快速而精确地得出问题的答案,避免繁琐的数学计算。 例如,当需要求解水流通过管道时的流速或压力变化时,可以利用伯努利shortcut来简化计算。假设管道是理想的,流体是不可压缩的,并且没有能量损失。根据伯努利原理,可以将问题化为一个等效的高度差问题,通过比较管道两个位置的液体的高度差来确定流速或压力的变化。 伯努利shortcut的优点在于其简洁而高效的计算方法,通常能够得到良好的近似解。然而,由于伯努利shortcut是基于一些简化假设的,因此结果可能会受到一些限制。在实际应用中,需要根据具体情况判断是否适合使用伯努利shortcut,并对结果进行修正或验证。 总而言之,伯努利shortcut是一种运用伯努利原理进行流体流动问题求解的简化方法。通过合理地利用伯努利原理以及相关的简化假设和公式,可以快速、精确地得到流速或压力的变化,为实际问题的解答提供了方便和效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值