捕捉界面中的按键

来源:互联网 发布:万能网络摄像机客户端 编辑:程序博客网 时间:2024/06/05 01:55

MSDN 介绍


https://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.processcmdkey(VS.80).aspx


处理命令键。

命名空间:   System.Windows.Forms
程序集:  System.Windows.Forms(位于 System.Windows.Forms.dll)

语法

C#
C++
F#
VB
复制
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)][SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]protected virtual bool ProcessCmdKey(ref Message msg,Keys keyData)

参数

msg
Type: System.Windows.Forms.Message

通过引用传递的 Message,表示要处理的窗口消息。

keyData
Type: System.Windows.Forms.Keys

Keys 值之一,表示要处理的键。

返回值

Type: System.Boolean

如果字符已由控件处理,则为 true;否则为false

备注

消息以处理命令键在预处理期间调用此方法。 命令键是始终优先于常规输入键的键。 命令键的示例包括加速器和菜单快捷方式。 该方法必须返回true 以指示它已处理命令键,或 false 以指示该密钥不是命令键。 当控件承载在 Windows 窗体应用程序中或作为 ActiveX 控件时,只能调用此方法。

ProcessCmdKey 方法首先确定控件是否有ContextMenu, ,如果是,则启用 ContextMenu 处理命令键。 如果命令键不是菜单快捷方式,且该控件有父级,该密钥传递给父级的ProcessCmdKey 方法。 实际效果是键"冒泡"控件层次结构中该命令。 除了用户按下的键,关键数据还指示哪些,如果有的话,同时按下修改键作为键的相同时间。 修改键包括 SHIFT、 CTRL 和 ALT 键。

继承函数说明:

当重写 ProcessCmdKey 方法在派生类中的,控件应返回true 以指示它已处理该键。 对于由该控件调用基类的结果未处理的键ProcessCmdKey 方法应返回。 控件很少,如果有,需要重写此方法。

安全性

SecurityPermission

for the immediate caller and inheriting classes to call this method. Associated enumeration: F:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode.

版本信息

.NET Framework
自 1.1 起可用



使用方法:
重写该方法即可
 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch(keyData)
            {
                case Keys.Escape:
                    this.WindowState = FormWindowState.Normal;
                    this.FormBorderStyle = FormBorderStyle.Sizable;
                    return true;
                case Keys.F2:
                     this.WindowState = FormWindowState.Maximized;
                     this.FormBorderStyle = FormBorderStyle.None;
                     return true;
                default:
                    return false;
            }
        }

原创粉丝点击