winforms中的快捷键实现 [英] Short cut key implementation in winforms

查看:142
本文介绍了winforms中的快捷键实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题。



我准备了一个Winforms应用程序,我想实现一些快捷键功能。



我在用户控件中放置了一个生成函数。但我无法实现快捷键功能。对我来说很困惑。



请帮我解决这个问题。



先谢谢

I have a problem like this.

I have prepared a Winforms Application where I want to implement some short-cut keys functionality.

I have a generate function placed inside a user control. but I'm not able to implement the short cut key functionality. its confusing for me.

kindly help me out to fix this please.

Thanks in Advance

推荐答案

快捷方式是PITA,大部分时间都是。

您可以设置菜单控件的ShortCutKeys属性,但如果没有t覆盖你想要的东西(而且它很少)然后你必须自己处理它,这通常是在Form的基础上(或者它们只在自定义控件具有焦点时才起作用,用户难以处理) 。



要通过Form处理它,请覆盖ProcessCmdKey方法:

Short cuts are a PITA, much of the time.
You can set the ShortCutKeys property of menu controls, but if that doesn't cover what you want (and it rarely does) then you have to pretty much handle it yourself, and that is normally on a Form basis (or they only work when the custom control has the focus, which is difficult for users to handle).

To handle it by Form, override the ProcessCmdKey method:
/// <summary>
/// Handle special keys that don't have shortcut operations.
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    Keys keyOnly = keyData & ~Keys.Modifiers;
    Keys modifiersOnly = Control.ModifierKeys & (Keys.Shift | Keys.Control | Keys.Alt);
    if (modifiersOnly == 0)
        {
        // Key alone
        switch (keyOnly)
            {
            case Keys.Enter:
                tsbvPlaySelected.PerformClick();
                return true;
            }
        }
    else if ((modifiersOnly & Keys.Control) != 0)
        {
        // Control and key
        switch (keyOnly)
            {
            case Keys.D:
                // Toolstrip buttons don't have shortcut keys.
                tsbvRename.PerformClick();
                return true;
            case Keys.Add:
                // "Numeric +" can't be added as a shortcut key in the actual menu
                tsbvAddSelectedToLastList.PerformClick();
                return true;
            case Keys.Subtract:
                // "Numeric -" can't be added as a shortcut key in the actual menu
                tsbvRemoveSelectedFromLastList.PerformClick();
                return true;
            }
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }


这篇关于winforms中的快捷键实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆