捕获键preSS活动在VB.NETpreSS、VB、NET

2023-09-04 23:38:27 作者:弱智儿童欢乐多

我感觉沮丧。我花了几个小时寻找一个很好的一块code捕捉到关键的preSS活动在任何窗口不管我的应用程序集中与否。我需要建立在后台工作,以捕捉F5键的应用程序。没有人有任何code?

I'm feeling frustrated. I have spent hours looking for a good piece of code to capture the keypress events in any windows no matter if my application is focused or not. I need to create an application to work in the background to capture the F5 key. Does anyone have any code?

推荐答案

我知道这是所有的C#和OP问VB.Net,但概念是相同的......

I know this is all C# and OP asked about VB.Net, but the concepts are the same...

我写的处理使用这是一个简单的工具项目。下载库,并添加对它的引用您的项目,并导入 Gma.UserActivityMonitor 命名空间

I wrote a simple utility that handled global key presses using this project. Download the library, and add a reference to it in your project, and import the Gma.UserActivityMonitor namespace

在表单Load事件,添加处理程序。

In your form load event, add the handlers.

HookManager.KeyDown += KeyDown;
HookManager.KeyUp += KeyUp;

在这些方法中,我在寻找任何控制键是pressed。

In these methods, I'm looking for either control key to be pressed.

void KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey)
    {
        //Do Stuff
    }
}
void KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey)
    {
        //Do Stuff
    }
}

通过这样写的方式,关键preSS仍然会提供给其他应用程序来处理。如果你想捕获键和其他应用程序停止其处理,在事件处理函数中设置Handled属性为true。

With the way this is written, the key press will still be available to other apps to process. If you want to "capture" the key and halt its processing by other apps, set the Handled property to true in your event handler.

e.Handled = true;

编辑:以上code转换到VB.Net

Conversion of above code to VB.Net

要在VB.Net添加处理程序,您可以使用的AddHandler AddressOf

To add handlers in VB.Net, you use AddHandler and AddressOf.

AddHandler HookManager.KeyDown AddressOf KeyDown

功能的KeyDown 的KeyUp 是这样的。

Sub KeyDown(ByVal sender as Object, ByVal e as KeyEventArgs)
    If e.KeyCode = Keys.LControlKey Or e.KeyCode = Keys.RControlKey Then
        'Do Stuff
    End If
End Sub
 
精彩推荐
图片推荐