在窗体最小化时检测按键。 [英] Detect key press while form is minimised.

查看:95
本文介绍了在窗体最小化时检测按键。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows窗体按键应用程序。我正在使用F3,F4,F5按键进行某些操作。但是当表单最小化时,按键不起作用。它应该在表单最小化时工作。如何在C#中解决这个问题。



I created one windows form keypress application.And I am using F3,F4,F5 keypress for some operation. But keypress is not working while form is minimised. It should work when form is minimised.How to fix this problem in C#.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
       {
               this.WindowState = FormWindowState.Minimized;

               oWord.Visible = true;
               if (keyData == Keys.F3)
               {
                   this.WindowState = FormWindowState.Minimized;
                   CaptureWithTaskBar();
                   this.WindowState = FormWindowState.Normal;
               }
               else if (keyData == Keys.F4)
               {
                   this.WindowState = FormWindowState.Minimized;
                   CaptureWithoutTaskBar();
                   this.WindowState = FormWindowState.Normal;
               }
               else if (keyData == Keys.F5)
               {
                   SaveDocument();
               }
}

推荐答案

问题是当表单最小化时,它没有焦点 - 所以它没有得到任何键盘活动。



如果你想在最小化时抓住击键 - 而且我建议反对它,它会使用户烦恼其他应用程序(如F3,F4和F5)经常使用密钥 - 然后您必须开始执行非常复杂的代码来安装Global Hook: Google [ ^ ] - 但不要期待它很容易,并且期望你试图让你的PC崩溃或让它处于非常不稳定的状态在开发它的过程中,有多少次。
The problem is that when the form is minimised, it doesn't have the focus - so it doesn;t get any keyboard activity.

If you want to catch keystrokes when minimized - and I'd advise against it, it annoys users if the keys are used by other apps like F3, F4, and F5 often are - then you have to start doing very complex code to install a Global Hook: Google[^] - but do not expect it to be easy, and do expect your attempts to crash your PC or leave it in a very unstable state a couple of times while you are developing it.


这篇关于在窗体最小化时检测按键。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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