Page 1 of 1

Read/call a variable from another Plugin?

Posted: Thu Dec 19, 2013 5:12 pm
by woggy
Is there a way to read the variable "codes" from this plugin (the Keyboard Plugin) from another plugin?
something like:

Code: Select all

print eg.plugins.keyboard.codes

Code: Select all

class Keyboard(eg.PluginBase):

    text = Text

    def __init__(self):
        self.AddEvents()


    def __start__(self, universalMods = False, *dummyArgs):
        SetKeyboardCallback(self.KeyboardCallback, int(universalMods))


    def __stop__(self):
        SetKeyboardCallback(None, 0)


    def Configure(self, universalMods = True):
        panel = eg.ConfigPanel()
        universalModsCtrl = panel.CheckBox(universalMods, self.text.label)
        panel.sizer.Add(universalModsCtrl, 0, wx.ALL, 20)
        while panel.Affirmed():
            panel.SetResult(universalModsCtrl.GetValue())


    def KeyboardCallback(self, codes, num, lastNum):
        if codes == "":
            self.EndLastEvent()
        else:
            if num >= lastNum:
                self.TriggerEnduringEvent(codes)
            else:
                self.EndLastEvent()
            return HasActiveHandler("Keyboard." + codes)


Re: Read/call a variable from another Plugin?

Posted: Thu Dec 19, 2013 5:36 pm
by Pako
woggy wrote:Is there a way to read the variable "codes" from this plugin (the Keyboard Plugin) from another plugin?
something like:

Code: Select all

print eg.plugins.keyboard.codes
No.
"codes" is not a Keyboard plugin variable.
It is only KeyboardCallback function variable.
However, if it were really a "plugin variable", then it is of course possible.

So without a plugin modification is not possible to read the variable "codes".

Pako

Re: Read/call a variable from another Plugin?

Posted: Fri Dec 20, 2013 1:55 pm
by woggy
many of my scripts and plugins use eg.globals.tgb to watch for key presses.
Is there a more correct way to do this than adding the line " eg.globals.tgb = (codes)"

Code: Select all

    def KeyboardCallback(self, codes):
        eg.globals.tgb = (codes)
        if codes == "":
            self.EndLastEvent()
        else:
            shouldBlock = HasActiveHandler("Keyboard." + codes)
            self.TriggerEnduringEvent(codes)
            return shouldBlock
I know I shouldn't use eg.globals in plugins and I have to add the line every time I install Eventghost, but it works =)

Re: Read/call a variable from another Plugin?

Posted: Fri Dec 20, 2013 7:24 pm
by Pako
woggy wrote:I know I shouldn't use eg.globals in plugins ...
Yes it is true.
This is a rule, written by Bitmonster:
Bitmonster wrote:No plugin should ever assign something to eg.globals or change anything in it. Never!

The eg.globals namespace is exclusive for the user's Python scripts and commands.
If your plugin wants to publish variables to the user, the preferred way is to implement getter actions, that simply return the value. This way the user can also easily see all informations your plugin can provide.
And I vote for respect.
So modified plugin should look something like this:

Code: Select all

class Keyboard(eg.PluginBase):

    text = Text

    def __init__(self):
        self.AddEvents()
        self.AddAction(GetCodes)


    def __start__(self, universalMods = False, *dummyArgs):
        self.codes = ""
        SetKeyboardCallback(self.KeyboardCallback, int(universalMods))


    def __stop__(self):
        SetKeyboardCallback(None, 0)


    def Configure(self, universalMods = True):
        panel = eg.ConfigPanel()
        universalModsCtrl = panel.CheckBox(universalMods, self.text.label)
        panel.sizer.Add(universalModsCtrl, 0, wx.ALL, 20)
        while panel.Affirmed():
            panel.SetResult(universalModsCtrl.GetValue())


    def KeyboardCallback(self, codes, num, lastNum):
        self.codes = codes
        if codes == "":
            self.EndLastEvent()
        else:
            if num >= lastNum:
                self.TriggerEnduringEvent(codes)
            else:
                self.EndLastEvent()
            return HasActiveHandler("Keyboard." + codes)


class GetCodes(eg.ActionBase):
    def __call__(self):
        return self.plugin.codes
Pako

Re: Read/call a variable from another Plugin?

Posted: Tue Dec 24, 2013 9:57 am
by woggy
Merry Christmas
Is there some chance this can be in the official release?

Code: Select all

# -*- coding: utf-8 -*-
#
# This file is part of EventGhost.
# Copyright (C) 2005-2009 Lars-Peter Voss <[email protected]>
#
# EventGhost is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2 as published by the
# Free Software Foundation;
#
# EventGhost is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""<rst>
This plugin generates events on keypresses (hotkeys).

**Notice:** If such a keyboard event is assigned to a macro, the plugin will
block the key, so Windows or another application will not see it anymore. This
is needed to permit remapping of keys as otherwise the old key would reach the
target in conjunction of another action you might want to do and this is
mostly not what you intend.

But this blocking only happens, if a macro would actually execute in
succession of the event. So if the macro or any of its parents is disabled,
the keypress will pass through.
"""


import eg

eg.RegisterPlugin(
    name = "Keyboard",
    author = "Bitmonster & blackwind",
    version = "1.1.2",
    kind = "remote",
    guid = "{59CBD10F-C1D8-4ADB-999B-9B76BA360F1F}",
    description = __doc__,
    icon = (
        "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeT"
        "AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQIDBMjdIFglwAAADV0RVh0Q29t"
        "bWVudAAoYykgMjAwNCBKYWt1YiBTdGVpbmVyCgpDcmVhdGVkIHdpdGggVGhlIEdJTVCQ"
        "2YtvAAABfklEQVQ4y6WSv0sCYRjHP2+IGlI0GOeN4WQJQnd/gENbRHtTji2NIkhTROLa"
        "EATh0hpE0OTmKNzgQUpDFEhdmosNVw3d2yB3+HYXRX3h5Xnf7/v8fh7BL1CrHywASSAN"
        "uMAAeK2Uq56IUFwFtoAH4BG4AZ6BJ+AF+KiUq55vE/sS7Ai4Ag6Bh0q5+vZTdqJWP1gD"
        "mj/oWYARwe/EgObG+qZMpxcFgBACKSVCKNUZ/t80ThsnxzEATcuIfr//bXjbtslmlxQu"
        "lZpTe2DbtqKgaRqDwUDhEokG7+8lEokGsKs6KBQKOI6Drus4jkMmk0FKGbxvb+/IZkt4"
        "nsR93WZ+kgAzvoNpY13XGY/HigSIx5Mkk7Pc390TGuO0cZQE6PV6of4EDjqdDgDD4TBS"
        "AuRyOSzLYnllOeCCEkzTxHVdTNNUDkA+n8d13ck8DYPudTecQavVUuQ02u32ZJssK7qE"
        "0WhEsVhESomUEiB09zwvWDKf9x3sX1ye7/E3nPFffAJVOqjtMbQazAAAAABJRU5ErkJg"
        "gg=="
    ),
)


from eg import HasActiveHandler
from eg.cFunctions import SetKeyboardCallback


class Text:
    label = "Universal modifiers"



class Keyboard(eg.PluginBase):

    text = Text

    def __init__(self):
        self.AddEvents()
        self.AddAction(GetCodes)


    def __start__(self, universalMods = False, *dummyArgs):
        self.codes = ""
        SetKeyboardCallback(self.KeyboardCallback, int(universalMods))


    def __stop__(self):
        SetKeyboardCallback(None, 0)


    def Configure(self, universalMods = True):
        panel = eg.ConfigPanel()
        universalModsCtrl = panel.CheckBox(universalMods, self.text.label)
        panel.sizer.Add(universalModsCtrl, 0, wx.ALL, 20)
        while panel.Affirmed():
            panel.SetResult(universalModsCtrl.GetValue())


    def KeyboardCallback(self, codes, num, lastNum):
        self.codes = codes
        if codes == "":
            self.EndLastEvent()
        else:
            if num >= lastNum:
                self.TriggerEnduringEvent(codes)
            else:
                self.EndLastEvent()
            return HasActiveHandler("Keyboard." + codes)


class GetCodes(eg.ActionBase):
    def __call__(self):
        return self.plugin.codes