首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

Silverlight中键盘事件的js开发

        在之前的一篇文章中,介绍了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运行效果如下所示:       

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

【责编:michael】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航