实现键盘快捷键在Windows窗体应用程序的最佳方式? [英] Best way to implement keyboard shortcuts in a Windows Forms application?

查看:156
本文介绍了实现键盘快捷键在Windows窗体应用程序的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找实现常见的Windows快捷键(例如<大骨节病>控制 + <大骨节病>˚F,<大骨节病>控制 + <大骨节病>一个最好的方法ñ)在我的 Windows窗体在C#应用程序

I'm looking for a best way to implement common Windows keyboard shortcuts (for example Ctrl+F, Ctrl+N) in my Windows Forms application in C#.

该应用程序有它承载许多子窗体主要形式(一次)。当用户点击<大骨节病>控制 + <大骨节病>˚F,我想显示一个定制的搜索表单。搜索表单将取决于在应用程序中当前打开的子窗体。

The application has a main form which hosts many child forms (one at a time). When a user hits Ctrl+F, I'd like to show a custom search form. The search form would depend on the current open child form in the application.

我想在* ChildForm_KeyDown *事件中使用像这样的:

I was thinking of using something like this in the *ChildForm_KeyDown* event:

   if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
        // Show search form

但是,这并不正常工作。该事件甚至不开除你时preSS的关键。该解决方案是什么?

But this doesn't work. The event doesn't even fire when you press a key. What is the solution?

推荐答案

您可能忘记设置窗体的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.form.key$p$pview.aspx\">Key$p$pview属性为True。重写<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx\">ProcessCmdKey()方法是通用的解决方案:

You probably forgot to set the form's KeyPreview property to True. Overriding the ProcessCmdKey() method is the generic solution:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
  if (keyData == (Keys.Control | Keys.F)) {
    MessageBox.Show("What the Ctrl+F?");
    return true;
  }
  return base.ProcessCmdKey(ref msg, keyData);
}

这篇关于实现键盘快捷键在Windows窗体应用程序的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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