Keystroke Trapping: Python or C

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
bill
Posts: 376
Joined: Fri Jan 09, 2015 9:25 pm

Keystroke Trapping: Python or C

Post by bill »

Are the keyboard strokes trapped by Python or C.

Where in codebase would this appear if I wanted to extend to a Custom View? Lets say keystroke V,C would provide a side view tilted 5 degrees towards the viewer (which would intentionally change the shading gradients on the model).
abdullah
Veteran
Posts: 4935
Joined: Sun May 04, 2014 3:16 pm
Contact:

Re: Keystroke Trapping: Python or C

Post by abdullah »

I do not know about Python, at c++ level the view provider (src/Gui/ViewProvider.cpp) provides you with key presses. Then this is inherited, for example at sketcher level it is (src/Mod/Sketcher/Gui/ViewProviderSketch.cpp):

Code: Select all

bool ViewProviderSketch::keyPressed(bool pressed, int key)
I never use Python, but searching in Draft WB, I found this:

Code: Select all

DraftGui.py:    def keyPressEvent(self, event):
It may be or not the real deal...
abdullah
Veteran
Posts: 4935
Joined: Sun May 04, 2014 3:16 pm
Contact:

Re: Keystroke Trapping: Python or C

Post by abdullah »

bill wrote: Thu May 24, 2018 12:16 pm Are the keyboard strokes trapped by Python or C.

Where in codebase would this appear if I wanted to extend to a Custom View? Lets say keystroke V,C would provide a side view tilted 5 degrees towards the viewer (which would intentionally change the shading gradients on the model).
that is for the view provider. I am not sure if what you want is to make a new command and assign such a shortcut. Because those are part of the command then. Example (CommandCreateGeo.cpp):

Code: Select all

CmdSketcherCreateRectangle::CmdSketcherCreateRectangle()
  : Command("Sketcher_CreateRectangle")
{
    sAppModule      = "Sketcher";
    sGroup          = QT_TR_NOOP("Sketcher");
    sMenuText       = QT_TR_NOOP("Create rectangle");
    sToolTipText    = QT_TR_NOOP("Create a rectangle in the sketch");
    sWhatsThis      = "Sketcher_CreateRectangle";
    sStatusTip      = sToolTipText;
    sPixmap         = "Sketcher_CreateRectangle";
    sAccel          = "R";
    eType           = ForEdit;
}
In Python, you may want to take a look to "DraftTools.py", there are a lot of shortcuts in there:

Code: Select all

class Draft_Label(Creator):
    "The Draft_Label command definition"

    def GetResources(self):
        return {'Pixmap'  : 'Draft_Label',
                'Accel' : "D, L",
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_Label", "Label"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_Label", "Creates a label, optionally attached to a selected object or element")}

    def Activated(self):
        self.name = translate("draft","Label", utf8_decode=True)
Post Reply