Developer forums (C::B DEVELOPMENT STRICTLY!) > Plugins development

Help for a plugin newbie - reading keypresses inside the editor

<< < (3/3)

dmoore:
Turbo: You might find this useful. I made a little plugin for tweaking editor settings that illustrates handling keypresses (in this case, disabling the insert key)


--- Code: ---svn checkout http://svn.berlios.de/svnroot/repos/cbilplugin/branches/EditorTweaks
--- End code ---

Turbo:
Wow, thanks a lot, you got it! I don't think i'd have figured the connection specifics like you have, anytime soon.
Building plugins is a process too heavy for my computer (one compilation took 7 minutes, averaging to 3), i'm waiting for the new pc so i can dive back into this.

Anyway, for completion's sake, if anyone comes looking for the code, here's what i ended up with:
keys.h:

--- Code: ---/***************************************************************
 * Name:      keys
 * Purpose:   Code::Blocks plugin
 * Author:     (Turbo)
 * Created:   2009-12-06
 * Copyright:
 * License:   GPL
 **************************************************************/

#ifndef KEYS_H_INCLUDED
#define KEYS_H_INCLUDED

// For compilers that support precompilation, includes <wx/wx.h>
#include <wx/wxprec.h>

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <cbplugin.h>

#include <wx/wxscintilla.h> // for wxScintillaEvent

class CodeBlocksEvent;
class cbEditor;

class keys : public cbPlugin
{
    public:
        /** Constructor. */
        keys();
        /** Destructor. */
        virtual ~keys();

        virtual void BuildMenu(wxMenuBar* menuBar){}
        virtual void BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data = 0){}
        virtual bool BuildToolBar(wxToolBar* toolBar){ return false; }
    protected:
        virtual void OnAttach();
        virtual void OnRelease(bool appShutDown);


    private:
        void OnKeyUp(wxKeyEvent& event);

    private:
        int                                m_EditorHookId;
};
#endif // KEYS_H_INCLUDED

--- End code ---


keys.cpp:

--- Code: ---#include <sdk.h> // Code::Blocks SDK
#include <configurationpanel.h>
#include "keys.h"

#include <manager.h>
#include <sdk_events.h>
#include <editorbase.h>
#include <editor_hooks.h>
#include <wx/wxscintilla.h>
#include "cbstyledtextctrl.h"

#include <sstream>


// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
    PluginRegistrant<keys> reg(_T("keys"));
}


// constructor
keys::keys()
{
    // Make sure our resources are available.
    // In the generated boilerplate code we have no resources but when
    // we add some, it will be nice that this code is in place already ;)
    if(!Manager::LoadResource(_T("keys.zip")))
    {
        NotifyMissingFile(_T("keys.zip"));
    }
}

// destructor
keys::~keys()
{
}

void keys::OnAttach()
{
    // do whatever initialization you need for your plugin
    // NOTE: after this function, the inherited member variable
    // m_IsAttached will be TRUE...
    // You should check for it in other functions, because if it
    // is FALSE, it means that the application did *not* "load"
    // (see: does not need) this plugin...


    // hook "key up" event with member callback function
    EditorManager* em = Manager::Get()->GetEditorManager();
    for(int i=0;i<em->GetEditorsCount();i++)
    {
        cbEditor* ed=em->GetBuiltinEditor(i);
        if(ed && ed->GetControl())
        {
            ed->GetControl()->SetOvertype(false);
            ed->GetControl()->Connect(wxEVT_KEY_UP,(wxObjectEventFunction) (wxEventFunction) (wxCharEventFunction)&keys::OnKeyUp,NULL,this);
        }
    }
}

void keys::OnRelease(bool appShutDown)
{
    // do de-initialization for your plugin
    // if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
    // which means you must not use any of the SDK Managers
    // NOTE: after this function, the inherited member variable
    // m_IsAttached will be FALSE...

    // unregister hook
    EditorManager* em = Manager::Get()->GetEditorManager();
    for(int i=0;i<em->GetEditorsCount();i++)
    {
        cbEditor* ed=em->GetBuiltinEditor(i);
        if(ed && ed->GetControl())
            ed->GetControl()->Disconnect(wxEVT_NULL,(wxObjectEventFunction) (wxEventFunction) (wxCharEventFunction)&keys::OnKeyUp);
    }
}

void keys::OnKeyUp(wxKeyEvent& event)
{
    if( !IsAttached() )
        return;

    if(event.GetKeyCode() == WXK_CONTROL)
        Manager::Get()->GetLogManager()->Log(wxString::Format(_("CTRL DOWN")));
    else
    {
        Manager::Get()->GetLogManager()->Log(wxString::Format(wxT("%i"),event.GetKeyCode()));
    }

    event.Skip();  // allow event to propagate
}

--- End code ---
and remember to link with "wxscintilla".

Cookie goes to dmoore!

Pecan:

--- Quote ---void keys::OnAttach()
{
    // do whatever initialization you need for your plugin
    // NOTE: after this function, the inherited member variable
    // m_IsAttached will be TRUE...
    // You should check for it in other functions, because if it
    // is FALSE, it means that the application did *not* "load"
    // (see: does not need) this plugin...


    // hook "key up" event with member callback function
    EditorManager* em = Manager::Get()->GetEditorManager();
    for(int i=0;i<em->GetEditorsCount();i++)
    {
        cbEditor* ed=em->GetBuiltinEditor(i);
        if(ed && ed->GetControl())
        {
            ed->GetControl()->SetOvertype(false);
            ed->GetControl()->Connect(wxEVT_KEY_UP,(wxObjectEventFunction) (wxEventFunction) (wxCharEventFunction)&keys::OnKeyUp,NULL,this);
        }
    }
}
--- End quote ---

This will only work if you load the plugin *after* some editors are already open.

If CB loads this plugin at startup, there are no editors open.

To catch editors opened after startup, you will have to monitor a cbEvent such as cbEVT_EDITOR_ACTIVATED


--- Code: ---    Manager::Get()->RegisterEventSink(cbEVT_EDITOR_ACTIVATED, new cbEventFunctor<JumpTracker, CodeBlocksEvent>(this, &JumpTracker::OnEditorActivated));

--- End code ---

Then move your "hook "key up" event" loop to a function called OnEditorActivated.

Turbo:
Oh, thanks! That would've definitely come up to byte me back later on.

dmoore:
you don't need this:


--- Code: ---ed->GetControl()->SetOvertype(false);
--- End code ---

I only put it in my plugin code to make sure every editor I connected to was set to insert mode before blocking the insert key.

Navigation

[0] Message Index

[*] Previous page

Go to full version