Hi Chip:
I know you are a VBS guy and not a VB.net guy but you mentioned it a good idea to post my code so, well, I did. First, your idea about the scope of the instTester variable was a good thought and made me ReThink the scope of the Handlers themselves which use the instTester variable in their AddressOf parameters.
So, 2 quick answers and one thought:
1) I am not sure how VBS uses classes nor what syntax is used to document them so I will put a little blurb about how vb.net handles them. In most programming languages these days you have to define variable types before you use them. There are instance variables like Class and SQLConnection related variables. Then there are non-instance variables like Integer, decimal, string and Boolean. But, you only have to instantiate "Instance" variables before you can use them.
So to use a decimal variable:
Dim myVar As Decimal = 15
myVar = myVar + 10
When you use a Instance variable you need to Instantiate it, in most cases, before you can use the non-static (non-Shared in VB.net) methods, properties or variables inside it.
Tester Class
(stuff like subs, variables or properties etc go here)
End Class
This defines the types you use inside the class but does not allocate storage or pointers to objects within the class. So before you can use this "Outline" of the defined Tester class we need to Instantiate it using the "New" KeyWord. The VB.net runtime will then allocate storage and set pointers and the like so it will be an active copy as it were, of the original "Outline" of the defined tester class and assign a reference pointer to the new class to a variable you make up. I just made up the name instTester to tell me this variable is the instance reference for this class.
To instantiate it for use:
The New KeyWord tells the Runtime engine to instantiate the specified class (Tester).
Dim instTester As New Tester()
2) When a programmer Instantiates a class there is a default method which automatically gets executed once. Programmers can define this method, sub, manyually if they want to say set up default values for a new instance of a class, perhaps for things like counters etc...
To do this they add a sub to the class definition named "New()"
Class Tester
Dim MyCounter As Integer
Private Sub New()
myCounter = 0
End Sub
End Class
Now the ClassLevel myCounter will be set to 0 when the class is first allocated storage.
Dim instTester As New Tester()
After this the instTester class instance will have a variable named myCounter with the value of 0.
To use it after the Tester Class has been instantiated as instTester:
instTester.myCounter = instTester.myCounter + 1
3) In my script I define a variable called myKeyBoard in the global Module with the Public scope keyword. Any variables placed in a module with the Public KeyWord can be used anytime and from anyplace within my script. I assign a value to this global variable when I initialize other WE Objects in that sub.
myKeyboard = weApplication.Keyboard
Since the AddHandler AddressOf values (addresses) are associated with the global variable myKeyBoard they should not be released until that global variable goes out of scope which, in my case, is when the script is terminated. This should be the case even though the instTester variable will go out of scope since the values have already been assigned relative to the global myKeyboard variable. That said, I will try a couple of things to validate the code and my understanding as described above. I will move the Functions into the same class as all the WindowEyes stuff which will mean I dont need to use the instTester variable at all. I might also set up global variables to hold the 2 AddHandler AddressOf values and use them in the AddHandler and RemoveHandler statements - although I dont think this should make a diference I will check it out. That was a very good thought and caused me to walk through the above senario in my mind so I posted it as an answer to your post.
Later and I'll post up the results when I get some time to run the tests.
Rick USA

----- Original Message ----- From: "Chip Orange" <[email protected]>
To: <[email protected]>
Sent: Wednesday, June 20, 2012 1:50 PM
Subject: RE: Followup Threads WE, ExternalScript and vb.net 2010 Express


Rick,

I've mentioned vb.net isn't something I'm familiar with; but could you
explain to me: in sub New() of class ScriptContext, you declare what looks like a local variable named instTester, then you assign two onKey handlers to methods of the class you've made instTester. It looks to me however like
this variable will then immediately go out of scope because it's declared
inside of sub New, and so the handlers won't function as handlers, or will
yield an error?

Chip


-----Original Message-----
From: RicksPlace [mailto:[email protected]]
Sent: Wednesday, June 20, 2012 9:43 AM
To: [email protected]
Subject: Re: Followup Threads WE, ExternalScript and vb.net
2010 Express

Hi Guys:
I stripped out everything from error handling to the
streamwriter log file and a few other objects and methods
used for support and other operations.
Here is the barebones logic I use to implement the
Keyboard.OnKeyDown and Keyboard.OnKeyUp events as described
in the WE Docs.
If you see something wrong with the way I am handling the
delegation of the Events or how I set up the return values of
the Handler functions (OnKeyDownHandler and OnKeyUpHandler)
let me know or let me know if you think of anything to try diferently.
If you want the project I will attach it to a message but it
is created in vb.net 2008 and it requires both vb.net 2008
Express and vb.net 2010 express to test.
Public Module LaunchApp
' All variables in module are global
Public myKeyboard As WindowEyes.Keyboard Public weApplication
As WindowEyes.Application
    Public WithEvents weClientInformation As
WindowEyes.ClientInformation Public Sub Main()
Application.Run(New ScriptContext) End Sub End Module Public
Class ScriptContext Inherits ApplicationContext Public Sub
New() Dim instTester As New Tester() weApplication = New
WindowEyes.Application
weApplication.ClientIdentify(System.Diagnostics.Process.GetCur
rentProcess().Id)
weClientInformation = weApplication.ClientInformation
AddHandler weClientInformation.OnShutdown, AddressOf
weClientInformation_OnShutdown myKeyBoard =
weApplication.KeyBoard AddHandler myKeyboard.OnKeyDown,
AddressOf instTester.OnKeyDownHandler AddHandler
myKeyboard.OnKeyUp, AddressOf instTester.OnKeyUpHandler End
Sub Public  Sub weClientInformation_OnShutdown() Dim
instTester As New Tester RemoveHandler myKeyboard.OnKeyDown,
AddressOf instTester.OnKeyDownHandler RemoveHandler
myKeyboard.OnKeyUp, AddressOf instTester.OnKeyUpHandler
Application.Exit()
System.Environment.Exit(0)
End Sub
End Class
Public Class Tester
Public Function OnKeyDownHandler( _
ByVal ReturnedKey As Integer, ByVal ReturnedModifiers As
WindowEyes.KeyModifiers) _
As WindowEyes.KeyDisposition
Dim AppropriateDisposition As WindowEyes.KeyDisposition
AppropriateDisposition = WindowEyes.KeyDisposition.kdProcess
Return AppropriateDisposition
End Function
Public Function OnKeyUpHandler( _
ByVal ReturnedKey As Integer, ByVal ReturnedModifiers As
WindowEyes.KeyModifiers) _
As WindowEyes.KeyDisposition
Dim AppropriateDisposition As WindowEyes.KeyDisposition
AppropriateDisposition = WindowEyes.KeyDisposition.kdProcess
Return AppropriateDisposition
End Function
End Class



Reply via email to