听键preSS当程序在后台 [英] Listen to key press when the program is in the background

查看:114
本文介绍了听键preSS当程序在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与这应该是在后台运行,而且检查MOD + O是pssed再做事$ P $程序工作。但我不能想出一个vb.net程序如何能听键presses当程序是不是选择 / 打开

I am currently working with a program which is supposed to run in the background but also check if "mod + o" is pressed then do something. But I cannot figure out how a vb.net program can listen to key presses when the program is not Selected / Opened.

推荐答案

您可以使用P /调用,以便能够使用WinAPI的的的 GetAsyncKeyState()函数 ,然后检查,在一个计时器。

You can use P/Invocation to be able to use WinAPI's GetAsyncKeyState() function, then check that in a timer.

<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function

Const KeyDown As Integer = &H8000

Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
    If (GetAsyncKeyState(Keys.O) And KeyDown) = KeyDown Then
        'Do whatever you want when 'O' is held down.
    End If
End Sub


我似乎无法找到mod关键是什么,这样你就可以做到这一点在你的表格(当它具有焦点):


I cannot seem to find what the Mod key is, so you can do this in your form (when it has focus):

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Form1.KeyDown
    MessageBox.Show(Convert.ToInt32(e.KeyCode))
End Sub

preSS的'mod'按钮ONLY,然后采取从消息框弹出窗口的数量,并把它在定时器:

Press the 'Mod' button ONLY, then take the number from the message box that popups and put it in the timer:

If (GetAsyncKeyState(<put number here>) And KeyDown) = KeyDown AndAlso (GetAsyncKeyState(Keys.O) And KeyDown) = KeyDown Then
        'Do whatever you want when 'Mod + O' is held down.
End If


编辑:

为了使code只执行每个键preSS一次,你可以加少许 -loop运行,直到该按钮是发布(添加它的你的如果语句来):

To make the code only execute one time per key press, you can add a little While-loop to run until either of the buttons are released (add it inside your If-statement):

While GetAsyncKeyState(ConsoleKey.LeftWindows) AndAlso GetAsyncKeyState(ConsoleKey.O)
End While

这将从执行多次停止code时,你只preSS组合一次。

This will stop your code from executing many times when you only press the combination once.

这篇关于听键preSS当程序在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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