Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[VB] Keyboard hook not working

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
AwayTheWInd
Master Cheater
Reputation: 0

Joined: 11 Sep 2007
Posts: 450

PostPosted: Sat Feb 21, 2009 9:03 pm    Post subject: [VB] Keyboard hook not working Reply with quote

my keyboard hook isnt working and the function: KeyboardProc isnt being called.

Option Explicit On
Public Delegate Function KeyboardDelegate(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Class Scanner
Public Const WH_KEYBOARD_LL = 13&
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As KeyboardDelegate, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public hHook As Long

Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'if idHook is less than zero, no further processing is required
If idHook < 0 Then
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, lParam)
Else
If GetAsyncKeyState(16) Then
Button1.Text = "A"
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, lParam)
End If
End Function

Private Sub Scanner_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
UnhookWindowsHookEx(hHook)
End Sub

Private Sub Scanner_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardProc, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, AppDomain.GetCurrentThreadId())
End Sub
End Class

_________________
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Feb 22, 2009 12:03 am    Post subject: Reply with quote

WH_KEYBOARD, not WH_KEYBOARD_LL
Back to top
View user's profile Send private message
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Sun Feb 22, 2009 12:04 am    Post subject: Reply with quote

.NET Doesn't support global hooks, so you better just set a KeyDown even for all the controls:
Code:
foreach (Control c in Controls)
c.KeyDown += KeyEventHandler(Function);


If you still want a global hook, make a DLL and load it, then call SetWindowsHookEx with the hInstance of the DLL and the function within the DLL. (don't call SetWindowsHookEx from the DLL because the function mustn't be in the same instance of the calling instance)
Back to top
View user's profile Send private message MSN Messenger
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sun Feb 22, 2009 10:28 am    Post subject: Reply with quote

Zerith wrote:
.NET Doesn't support global hooks, so you better just set a KeyDown even for all the controls:
Code:
foreach (Control c in Controls)
c.KeyDown += KeyEventHandler(Function);


If you still want a global hook, make a DLL and load it, then call SetWindowsHookEx with the hInstance of the DLL and the function within the DLL. (don't call SetWindowsHookEx from the DLL because the function mustn't be in the same instance of the calling instance)


...

Yeah it does...
Look at my code in the GP+ section.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Sun Feb 22, 2009 10:58 am    Post subject: Reply with quote

Well, I read somewhere it's not possible, so I googled some and I saw this:
Quote:
global hooks are not possible in .NET using the SetWindowsHookEx API, except for MOUSE and KEYBOARD.

So I guess we both were partially correct.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites