[RESOLVED] Raise event in a Class module through a module (Thanks to Merri).
Hi everybody , currently I want to make a keyboard hook but as an ActiveX Dll , I use Joacim Andersson's method for doing this , like here it work perfectly but it use AddressOf so I must use a module :( , my question is : If I have event in my class module say [ Public Event KeyPressed (VirutalKey As Long) ] how can I raise it through the module ?
I know that there is a way in the API Timer made by Wokawidget but I don't know how to use it in my case , can anyone help ?!
Btw : I made a search but nothing relevant :sick: if it was discussed be4 I want links please.
Thanks & Have a nice day :wave:
Re: Raise event in a Class module through a module.
One thing you could do is to make a public Collection where each class automatically adds itself and removes as well once done. Like:
Code:
' in the module
Public KbdCollection As New Collection
' in Initialize of a Class Module
KbdCollection.Add Me, CStr(ObjPtr(Me))
' in Terminate of a Class Module
KbdCollection.Remove CStr(ObjPtr(Me))
Now as you know all the classes that exist, you can easily call them so that events are raised.
As a notice, I've never actually tried if it is possible to do this. Hopefully it is.
Re: Raise event in a Class module through a module.
OK , Thanks for reply but I don't know how to finish this.
Say I made this :
In a Class Module (Class1) :
vb Code:
Option Explicit
Public Event DestroyMyPC()
Friend Sub RaiseMyEvent()
RaiseEvent DestroyMyPC
End Sub
Private Sub Class_Initialize()
KbdCollection.Add Me, CStr(ObjPtr(Me))
Goo
End Sub
Private Sub Class_Terminate()
KbdCollection.Remove CStr(ObjPtr(Me))
End Sub
And in a module :
vb Code:
Option Explicit
Public KbdCollection As New Collection
Public Sub Goo()
'What should be here
End Sub
And in the form :
vb Code:
Option Explicit
Private WithEvents MyClass As Class1
Private Sub Form_Load()
Set MyClass = New Class1
End Sub
Private Sub MyClass_DestroyMyPC()
MsgBox "Done"
End Sub
What should I do in the Goo Sub to call the RaiseMyEvent Sub
Thanks :D
Re: Raise event in a Class module through a module.
You can't call Goo on Initialize, because class isn't still fully ready at that point and it won't raise events. I moved Goo into a command button and it started to work.
Code:
' Form1.frm
Option Explicit
Private WithEvents MyClass As Class1
Private Sub Command1_Click()
Goo
End Sub
Private Sub Form_Load()
Set MyClass = New Class1
End Sub
Private Sub MyClass_DestroyMyPC()
Debug.Print "Goo!"
End Sub
Code:
' Module1.bas
Option Explicit
Public KbdCollection As New Collection
Public Sub Goo()
Dim Kbd As Class1
For Each Kbd In KbdCollection
Kbd.RaiseMyEvent
Next Kbd
End Sub
Code:
' Class1.cls
Option Explicit
Public Event DestroyMyPC()
Friend Sub RaiseMyEvent()
RaiseEvent DestroyMyPC
End Sub
Private Sub Class_Initialize()
KbdCollection.Add Me, CStr(ObjPtr(Me))
End Sub
Private Sub Class_Terminate()
KbdCollection.Remove CStr(ObjPtr(Me))
End Sub
:)
Re: Raise event in a Class module through a module.
Thanks but can I call it from the class later (by a public sub) ?
Thanks for your concern :)
Edit : About to say resolved , but I will try to complete the DLL fist before marking it as Resolved , Thanks Merri
Re: Raise event in a Class module through a module.
Yes, you can call the procedure from the class, just don't do it in Initialize :)
Re: Raise event in a Class module through a module.
Well thanks it is Resolved now , I made it ............. Oooops sorry Merri made it :)
Just want to know in addition to your name , your site & vbForums what should I add to the credits.
Thanks again :)
Edit : One more related question : should I use Virtual Keys list (that one @ MSDN) to convert the results to letters (characters) or there is a way to do this directly by any function (built in or API) ,Thanks.
Re: [RESOLVED] Raise event in a Class module through a module (Thanks to Merri).
This was so minor that I don't know if it is even worth crediting. My help was what, three to five lines of code depending on how compact you want to write it :D
As for your new question, see this solution at vbAccelerator.
Re: [RESOLVED] Raise event in a Class module through a module (Thanks to Merri).
Thanks again :) , I will take a look.
:wave: