View Full Version : Using 'KeyDown' While in a different application?

09-30-2005, 12:37 AM
I currently have:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim scHwnd As Long
Dim scPid As Long
Dim scHp As Long
Dim scMin As Long
Dim iU As Integer
Dim iD As Integer
Dim Gold As Integer
scHwnd = FindWindow("FFwinClass", vbNullString)
GetWindowThreadProcessId scHwnd, scPid
scHp = OpenProcess(PROCESS_ALL_ACCESS, 0, scPid)
ReadProcessMemory scHp, &HD64830, Gold, 4&, 0&
Select Case KeyCode
Case 33 'Page Up
iU = Gold + 500
WriteProcessMemory scHp, &HD64830, iU, 4&, 4&
Case 34 'Page Down
iD = Gold - 500
WriteProcessMemory scHp, &HD64830, iD, 4&, 4&
End Select
End Sub

The problem is, I want the shortcut keys (PageUP and PageDOWN) to work during the current game which is a whole different application. The game would have to have the focus and be above everything else while the program would still obtain the information the keys being pressed... is there any simple code or method for doing this?

Thank you for your time.

10-02-2005, 09:58 AM
Use the GetAsyncKeystate() Windows API function to check if it has been pressed with a timer. Look around for some examples if you don't know how to use it. There are tons of them around.

10-03-2005, 02:17 AM

That is one of the worst programming techniques you could EVER develop. Please refrain from polling and make your code interrupt based. If you want a systemwide hotkey, you could use RegisterHotkey function. If you wanted to hook a specific application, you could subclass the window (win32asm.cjb.net has a tutorial on subclassing), or you could reverse the program first and hook the WM_CHAR message.

The only time you would ever use a timer in a loop like that is in multimedia/gaming.

10-03-2005, 03:09 AM
Hmmm... From MSDN:
RegisterHotkey function...WM_HOTKEY messages are posted to the message queue and must be processed in the message loop...

Is this (and if so, how) possible in VB?

10-03-2005, 09:40 AM

10-03-2005, 09:42 AM

10-03-2005, 09:44 AM

quite strange how much comes up with a quick google for "global hotkey visual basic"

10-03-2005, 01:28 PM
I seemed to have ran across some problems using the GetAsyncKeystate() due to the timer method cause it didn't seem effecient. Currently looking into the method posted by Jakor.

Thank you both for making posts on this subject.