19

我试图Print Screen在表单不是当前活动的应用程序时检测按钮按下。

如果可能,该怎么做?

4

3 回答 3

28

好吧,如果您遇到系统挂钩问题,这里有现成的解决方案(基于http://www.dreamincode.net/forums/topic/180436-global-hotkeys/):

在项目中定义静态类:

public static class Constants
{
    //windows message id for hotkey
    public const int WM_HOTKEY_MSG_ID = 0x0312;
}

在项目中定义类:

public class KeyHandler
{
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private int key;
    private IntPtr hWnd;
    private int id;

    public KeyHandler(Keys key, Form form)
    {
        this.key = (int)key;
        this.hWnd = form.Handle;
        id = this.GetHashCode();
    }

    public override int GetHashCode()
    {
        return key ^ hWnd.ToInt32();
    }

    public bool Register()
    {
        return RegisterHotKey(hWnd, id, 0, key);
    }

    public bool Unregiser()
    {
        return UnregisterHotKey(hWnd, id);
    }
}

添加用途:

using System.Windows.Forms;
using System.Runtime.InteropServices;

现在,在您的表单中,添加字段:

private KeyHandler ghk;

在 Form 构造函数中:

ghk = new KeyHandler(Keys.PrintScreen, this);
ghk.Register();

将这两种方法添加到您的表单中:

private void HandleHotkey()
{
        // Do stuff...
}

protected override void WndProc(ref Message m)
{
    if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
        HandleHotkey();
    base.WndProc(ref m);
}

HandleHotkey 是您的按钮按下处理程序。您可以通过在此处传递不同的参数来更改按钮:ghk = new KeyHandler(Keys.PrintScreen, this);

现在你的程序对按钮输入做出反应,即使没有集中注意力。

于 2013-08-17T18:06:36.660 回答
15

是的,您可以,它被称为“系统挂钩”,请查看.NET 中的全局系统挂钩

于 2013-08-17T17:48:20.490 回答
10

APIGetAsyncKeyState()可能是设置 Windows Hook 的完全可接受的替代方案。

这取决于您希望如何接收输入。如果您更喜欢事件驱动的通知,那么钩子就是要走的路;但是,如果您更喜欢轮询键盘以获取状态更改,则可以使用上面的 API。

以下是如何使用的简单演示GetAsyncKeyState
源自Pinvoke.NET

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(int vKey);

private static readonly int VK_SNAPSHOT = 0x2C; //This is the print-screen key.

//Assume the timer is setup with Interval = 16 (corresponds to ~60FPS).
private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();

private void timer1_Tick(object sender, EventArgs e)
{
    short keyState = GetAsyncKeyState(VK_SNAPSHOT);

    //Check if the MSB is set. If so, then the key is pressed.
    bool prntScrnIsPressed = ((keyState >> 15) & 0x0001) == 0x0001;

    //Check if the LSB is set. If so, then the key was pressed since
    //the last call to GetAsyncKeyState
    bool unprocessedPress = ((keyState >> 0)  & 0x0001) == 0x0001;

    if (prntScrnIsPressed)
    {
        //TODO Execute client code...
    }

    if (unprocessedPress)
    {
        //TODO Execute client code...
    }
}
于 2018-01-18T16:24:45.663 回答