PDA

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



Friendless
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.

Consumed
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.

laocoon
10-03-2005, 02:17 AM
HOLY CRAP DON'T DO THAT!

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.

Consumed
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?

laocoon
10-03-2005, 09:40 AM
http://www.mvps.org/vbvision/_samples/HotKey_Demo.zip

laocoon
10-03-2005, 09:42 AM
http://www.codeguru.com/code/legacy/vb_forms/1855_desktop3.zip

laocoon
10-03-2005, 09:44 AM
http://www.vbaccelerator.com/home/VB/Code/Libraries/Subclassing/Registered_Hotkeys/article.asp


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

Friendless
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.