(VB or C #) Launching the application in the system tray and listening to keyboard input, even if the application is not in focus

I want to create an application that, when loaded, is in the system tray, and even after I open another program (for example, notepad or vlc or something else), i.e. even when the application is not in focus, and if I press G on my keyboard, the tray icon should show a hint - "G key pressed." I tried several codes, but nothing works when the application goes out of focus. I can use Register Hot Key , but it also needs a modifier (e.g. Ctrl or Alt , etc. Together with my G key). So, is there a way I can achieve this? what many tray icons like antivirus apps etc. and I don’t want to use the AutoHotkey app. I want to build one, but you need help.

+4
source share
2 answers

WindowsHookLib.dll saved me many times. This allows you to easily and simply connect a mouse and keyboard system.

http://www.vbforums.com/showthread.php?t=436321

to intercept the keyboard and act when you press the "G" button:

 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 
+3
source

You will need to use the Key for the keyboard. Here are a few implementations: Keyboard Interceptors

+1
source

Source: https://habr.com/ru/post/1305381/


All Articles