know if hotkey hit when program not in focus?

ghostpally

Member
Joined
Jan 19, 2008
Messages
23
Programming Experience
Beginner
i made a AutoBuffer program for a game i play what it does is uses a timer and when the time get to the goes it cast my spell. Know what im trying to do is have a hotkey like F9 to Show Program F10 to Restart the timers. But the PRoblem is that my program has no focus and the game does. How can i make it so my program know that i hit the hotkey when it does not have Fouse. I try to use KeyPRess Event but that only works with the form have focus. I heard of APi and Keyboard Hook but I cant find a code that works. I would be realy nice if someone can help me Ty.
 
K

Ok Ty for u Class but i did not understand how to use it so i did some more looking up and i Found this is
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3056210&SiteID=1
It is simple all you do is Put it on the forum u want to control while its not active.
For one HotKey use this code
VB.NET:
Public Class Form2

Public Const MOD_ALT As Integer = &H1 'Alt key

Public Const VK_NUMPAD1 As Integer = &H61 'NumPad 1 key

Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer

Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

Public Const WM_HOTKEY As Integer = &H312

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

If m.Msg = WM_HOTKEY Then

Me.Label1.Text = DateTime.Now.Second.ToString

End If

MyBase.WndProc(m) 'Never Forget This

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

UnregisterHotKey(Me.Handle, 9) 'Remember to unregister the hotkey

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

RegisterHotKey(Me.Handle, 9, MOD_ALT, VK_NUMPAD1) 'Registers Alt + Numpad 1

End Sub

End Class

For More Hot keys use this code
VB.NET:
Public Class Form5

Const MYKEYID1 As Integer = 9

Const MYKEYID2 As Integer = 10

Public Const MOD_ALT As Integer = &H1 'Alt key

Public Const VK_NUMPAD1 As Integer = &H61 'NumPad 1 key

Public Const VK_NUMPAD2 As Integer = &H62 'NumPad 2 key

Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer

Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

Public Const WM_HOTKEY As Integer = &H312

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

If m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID1 Then

Me.Text = DateTime.Now.Second.ToString & "key 1"

ElseIf m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID2 Then

Me.Text = DateTime.Now.Second.ToString & "key 2"

End If

MyBase.WndProc(m) 'Never Forget This

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

UnregisterHotKey(Me.Handle, MYKEYID1) 'Remember to unregister the hotkey

UnregisterHotKey(Me.Handle, MYKEYID2) 'Remember to unregister the hotkey

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

RegisterHotKey(Me.Handle, MYKEYID1, MOD_ALT, VK_NUMPAD1) 'Registers Alt + Numpad 1

RegisterHotKey(Me.Handle, MYKEYID2, MOD_ALT, VK_NUMPAD2) 'Registers Alt + Numpad 2

End Sub

End Class
I dont take any credit for this code
Hope it helps
 
Back
Top