捕获键盘键preSS背景 [英] Capture a keyboard keypress in the background

查看:170
本文介绍了捕获键盘键preSS背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在后台运行的应用程序一个。我不得不产生一些事件,每当用户preSS <大骨节病> F12 在任何时候。所以我需要一个捕获键盘preSS。在我的应用程序中,如果任何时候用户preSS <大骨节病> F10 将进行一些活动。我不明白该怎么做?

I have a application that runs in the background. I have to generate some event whenever a user press F12 at anytime. So what I need that to capture a key-press. In my application, if any time a user press F10 some event will be performed. I don't understand how to do that?

任何人有任何想法如何做到这一点?

Have anyone any idea how to do that?

N:A:
这是一个WinForms应用程序。它并不需要有关注我的形式。我的主窗口可能会保留在系统托盘中,但仍然要抓住关键preSS。

N:B: It is a winforms application. It doesn't need to have focus my form. My main window may remain in system tray but still it have to capture the keypress.

推荐答案

你想要的是一个全局热键


  1. 导入所需的库在你的类的顶部:

  1. Import needed libraries at the top of your class:

// DLL libraries used to manage hotkeys
[DllImport("user32.dll")] 
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


  • 添加在你的类中的字段,这将成为在code中的热键的引用:

  • Add a field in your class that will be a reference for the hotkey in your code:

    const int MYACTION_HOTKEY_ID = 1;
    


  • 注册热键(在Windows窗体的实例的构造函数):

  • Register the hotkey (in the constructor of your Windows Form for instance):

    // Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
    // Compute the addition of each combination of the keys you want to be pressed
    // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6...
    RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F12);
    


  • 在你的类中添加以下方法可以处理输入键:

  • Handle the typed keys by adding the following method in your class:

    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) {
            // My hotkey has been typed
    
            // Do what you want here
            // ...
        }
        base.WndProc(ref m);
    }
    


  • 这篇关于捕获键盘键preSS背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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