← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #250468]: How to instantiate a SikuliEvent object

 

Question #250468 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/250468

    Status: Open => Answered

RaiMan proposed the following answer:
If I understand you right:

since breakscript has a parameter self, I guess it is some method in a
Python class (since I do not know the class name, I call it SomeClass
here)

You might call this method from elsewhere by just using:
SomeClass(). breakscript(None)

Another possibility would be, to move the code out of breakscript into a global function 
def myTearDown(SomeClassInstance)
    App.focus("ArcMap")
    click(SomeClassInstance.inside_sitmap)
    wheel(SomeClassInstance.inside_sitmap, WHEEL_DOWN, 1)
    exit(1)

and reduce break script to
def breakscript(self, event):
    myTearDown(self)

So you now can call  myTearDown(SomeClassInstance) from anywhere.

--- Your initial question:
How to instantiate a SikuliEvent object

in the case of hotkeys, the event has no meaningful content and is not
of type SikuliEvent but of type HotkeyEvent.

this is how the addHotkey is setup in sikuli.Env.py:

from org.sikuli.basics import HotkeyListener
from org.sikuli.basics import HotkeyManager
from org.sikuli.script import Env as JEnv
class Env(JEnv):
    @classmethod
    def addHotkey(cls, key, modifiers, handler):
        class AnonyListener(HotkeyListener):
            def hotkeyPressed(self, event):
                handler(event)
        return HotkeyManager.getInstance().addHotkey(key, modifiers, AnonyListener())

and this is the HotkeyEvent as  defined on the Java level:

package org.sikuli.basics;
public class HotkeyEvent {
   public int keyCode;
   public int modifiers;
   public HotkeyEvent(int code_, int mod_){
      init(code_, mod_);
   }
   void init(int code_, int mod_){
      keyCode = code_;
      modifiers = mod_;
   }
}

... so it only contains the key information, for which it was defined
(allowing to use the the same hotkey handler for different hotkeys - if
that makes sense at all ;-)

This all is like it is, because including Java 7, functions are not first class objects like in Python and hence cannot passed around as callback functions (you always need some interface class, that defines a callback function, that you have to overwrite in an implementing class).
Only beginning with Java 8 you can have functions as parameters.

So with Python (and hence Sikuli scripting level) you should have a
global utility class or even a module, that you just import to access
your utility functions from where needed.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.