当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

Silverlight中键盘事件的js开发

欢迎进入.NET社区论坛,与200万技术人员互动交流 >>进入

        在之前的一篇文章中,介绍了Silverlight中鼠标事件的js应用,因为这篇文章内容摘选自SDK,所以就先暂放在新手区,算是给自己做了个备注吧:)

        下面是相应的xaml代码(KeyDown.xaml):          <Canvas xmlns="http://schemas.microsoft.com/client/2007"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Name="myTextCanvas">
          <TextBox Canvas.Left="30" Canvas.Top="30" Width="200"
           x:Name="myTextBox" Text="" />
          <TextBox Canvas.Left="30" Canvas.Top="80" Width="200"
          x:Name="myTextBox2" Text="" />
          <TextBlock Canvas.Left="30" Canvas.Top="130"
            x:Name="myTextBlock" Text="显示 KeyDown 事件参数" />
        </Canvas>

        相应的js代码, 内容详见注释(KeyDown.js):

         Silverlight_JsWeb.KeyDown = function()
        {
        }
        Silverlight_JsWeb.KeyDown.prototype =
        {
         handleLoad: function(plugIn, userContext, sender)
         {
          this.plugIn = plugIn;
          // 按钮事件挂钩示例: 查找按钮,然后附加事件处理程序
          sender.findName("myTextBox").addEventListener("KeyDown", Silverlight.createDelegate(this, this.handleKeyDown));
          sender.findName("myTextBox").addEventListener("GotFocus", Silverlight.createDelegate(this, this.handleGotFocus));
          sender.findName("myTextBox").addEventListener("LostFocus", Silverlight.createDelegate(this, this.handleLostFocus));
          sender.findName("myTextBox").addEventListener("KeyUp", Silverlight.createDelegate(this, this.handleKeyUp));
         },
        /*
        keyEventArgs 参数说明:
            Key: 键值,整型类型。
            PlatformKeyCode: 键盘按下的键值,整型类型。This value is the non-portable key code, which is operating system-specific.
            Shift: 是否按下了 SHIFT 键,Boolean类型.
            Ctrl: 是否按下了 Ctrl 键,Boolean类型.
        */
         // 键按下的事件处理方法.
            handleKeyDown: function(sender, keyEventArgs)
            {
                var textBlock = sender.findName("myTextBlock");
                var msg  = "key: " + keyEventArgs.key + "\r\n";
                    msg += "platformKeycode: " + keyEventArgs.platformKeyCode+ "\r\n";
                    msg += "shift: " + keyEventArgs.shift+ "\r\n";
                    msg += "ctrl: " + keyEventArgs.ctrl+ "\r\n";

                textBlock.Text = msg;
            },
            //得到输入焦点的处理方法
            handleGotFocus: function(sender, keyEventArgs)
            {
                sender.findName("myTextBlock").Text = "得到焦点";
            },
            //失去输入焦点的处理方法
            handleLostFocus: function(sender, keyEventArgs)
            {
                sender.findName("myTextBlock").Text = "失去焦点";
            },
            // 键弹起的事件处理方法
            handleKeyUp: function(sender, keyEventArgs)
            {
                // 判断是否按下 CTRL+V 组合键.
                if ((keyEventArgs.key == 51) && (keyEventArgs.ctrl == true))
                {
                    // 返回插件引用.
                    var plugin = sender.getHost();
                    // 检测是否是sivlerlight 2.0版.
                    sender.findName("myTextBlock").Text = "Silverlight 2.0: " + plugin.isVersionSupported("2.0");
                }
            }
        }


        其Demo运行效果如下所示:       

        好了,今天的内容就到这里了。

相关内容
赞助商链接