我怎么知道用户按下键盘上的控制键+ z键? [英] How do I know that user press control + z key from keyboard?

查看:134
本文介绍了我怎么知道用户按下键盘上的控制键+ z键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是在后台,我想知道用户是否按下键盘上的控制和z按钮的组合?



我只能检测是否按下了Control但是我如何检测控制和z键的组合?



我尝试过:



My application is in background and i want to know if user press combination of control and z button from keyboard?

I can only detect if Control is pressed but how do i can detect combination of both control and z key?

What I have tried:

if (Control.ModifierKeys == Keys.Control)
                MessageBox.Show("Control pressed");

推荐答案

如果您的应用没有输入焦点,它将永远不会看到这些事件中的击键。



您需要实现全局热键或全局键盘钩。即使有一些全局热键的注册表并将代码打包在KeyboardHook类中,这两者也不是一回事。



Google for C#Global Hotkey例如。
If your app doesn't have the input focus it'll never see the keystrokes in those events.

You either need to implement a Global Hotkey or a Global Keyboard Hook. The two are NOT the same thing even though there are examples out there that registry for a Global Hotkey and pack the code inside a "KeyboardHook" class.

Google for "C# Global Hotkey" for examples.


vast25写道:



in这个代码e是EventArgs所以如果我的应用程序在后台也不行。


in this code e is EventArgs so it will also not work if my app in background.

这是一个不同的故事。但是,首先,你不应该知道钥匙是否被击中;你需要处理事件。解决方案1和2没有向你解释它并且是错误的,写在上下文之外。



现在,因为你的应用程序可能在后台(在某种意义上)你的意思是),也就是说,没有激活的窗口(它可能根本没有窗口或者没有激活它们),你可以为整个系统全局注册一个热键。这是如何完成的:

RegisterHotKey函数(Windows) [ ^ ],

UnregisterHotKey函数(Windows) [ ^ ]。



为此而做。 NET,你需要使用P / Invoke。这里已经完成了一切:

http://www.pinvoke.net/ default.aspx / user32.registerhotkey [ ^ ],

http://www.pinvoke.net/default.aspx/ user32.unregisterhotkey [ ^ ]。



如果你设置一个全局的Windows Hook,你可以获得更大的灵活性,但这要困难得多。对于全局钩子,您需要在本机DLL中设置钩子,而不是.NET程序集。钩子代码很难调试,一段时间内系统不稳定会有很多危险,等等。无论如何,阅读它:

挂钩(Windows) [ ^ ],

挂钩概述(Windows) [ ^ ],

SetWindowsHookEx函数(Windows) [ ^ ]。



-SA

This is a different story. But, first of all, you should not "know" if a key is hit; you need to handle events. Solution 1 and 2 did not explained it to you and are just wrong, written out of context.

Now, as your application may be in the background (in the sense you mean), that is, with no activated window (it may have no windows at all or none of them are activated), you can register a hot key globally for the whole system. This is how it is done:
RegisterHotKey function (Windows)[^],
UnregisterHotKey function (Windows)[^].

To do it for .NET, you need to use P/Invoke. Everything is already done for you here:
http://www.pinvoke.net/default.aspx/user32.registerhotkey[^],
http://www.pinvoke.net/default.aspx/user32.unregisterhotkey[^].

You can get more flexibility if you set up a global Windows Hook, but this is much harder. For a global hook, you would need to set up the hook in a native DLL, not your .NET assembly. Hook code is quite hard to debug, there is a lot of danger to destabilize your system for a while, and so on. Anyway, read about it:
Hooks (Windows)[^],
Hooks Overview (Windows)[^],
SetWindowsHookEx function (Windows)[^].

—SA


您必须检查OnKeyDown事件中的两个键,如:



You have to check for both keys in the OnKeyDown event like:

protected override void OnKeyDown(KeyEventArgs e)
 {
      if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control)
      {
            MessageBox.Show("Hello world");
      }
      base.OnKeyDown(e);
  }





als你可以看看这个 SO帖子


这篇关于我怎么知道用户按下键盘上的控制键+ z键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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