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 (http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx) but it needs a modifier also (like Ctrl or Alt etc. along 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. Thanks!
views:
351answers:
2
+1
Q:
(VB or C#) Run app in system tray and listen to keyboard input even when the app is not in focus
A:
You will need to use Keyboard hook. Here are several implementations: Keyboard hooks
Giorgi
2010-03-28 20:21:28
Hey, I already built my app using Hook yday itself. anyways Thanks for the reply. you can check out my app @ http://bit.ly/CapsLockStatus
Avinash Sonee
2010-03-30 10:04:55
That's nice. It would be good if you marked the reply that helped you as an answer so that others find it useful too.
Giorgi
2010-03-30 10:51:19
A:
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
Stefan
2010-03-28 20:22:25