(VB or C#) Run app in system tray and listen to keyboard input even when the app is not in focus
I want to make an app which on loading sits in the system tray and even after I open another program (say notepad or vlc or anything) i.e. even when the app is not in focus and if I press G on my keyboard, the tray icon should show a tool tip - "key G is pressed".
I have tried several codes but nothing works when the app goes out of focus. I can use Register Hot Key
but it needs a modifier also (like Ctrl or Alt etc. alon开发者_JAVA百科g with my key G). So, is there any way I can achieve this?
something which many tray icons do like antivirus apps, etc.
and I do not want to use AutoHotkey
application. I want to build one but need some help.
WindowsHookLib.dll have saved me many times. It lets you hook both mouse and keyboard system wide very easy.
http://www.vbforums.com/showthread.php?t=436321
to hook the keyboard and act when 'G' is pressed:
Imports WindowsHookLib
Public Class frmMain
Dim WithEvents gkh As New LLKeyboardHook
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
gkh.RemoveHook()
ghk.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gkh.InstallHook()
End Sub
Private Sub gkh_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyEventArgs) Handles gkh.KeyDown
If e.KeyCode = Keys.G Then
REM G is pressed!
End If
End Sub
Private Sub gkh_KeyUp(ByVal sender As Object, ByVal e As WindowsHookLib.KeyEventArgs) Handles gkh.KeyUp
If e.KeyCode = Keys.G Then
REM G was pressed and now released
End If
End Sub
End Class
You will need to use Keyboard hook. Here are several implementations: Keyboard hooks
精彩评论