[gtkmm] Keystroke interception



I have an application where the main window is a text window for entering
commands and expressions to be evaluated.  I want to intercept all incoming
keystrokes for two reasons.  First, I will eventually want some control key
combinations to trigger operations, such as switching to another window, as an
alternative to selecting menu entries.  Second, and more importantly, I want
certain combinations of Alt or Control+Alt with other keys to insert special
Unicode characters into the text window, e.g. Alt+G becomes 0x2207, Alt+L
becomes 0x2395, and so on.  I would like to be able to use a simple text file
to define these keystroke mappings so that they can be easily customized.

I have tried to do this by following some examples in the online documentation
and in messages that I have found through Google, but I can't get the connect()
call to work; I get a compiler error.  I don't know what widget to call
connect() from.  I have tried doing this from the text view, the text buffer,
the scrolled window, and the main window itself and none of these seem to work.
I also tried creating a Gtk::Entry and connecting from that, which didn't work
either.  This is very frustrating.  Intercepting and translating keystrokes is
something that many programs would have to do, so I would expect this sort of
thing to be documented somewhere.

I would appreciate any help you can give me.  My code so far, and a sample of 
the error messages I am getting, are included below.  

Thanks.

--- Brian


/*------------------------------------------------------------------------------
  apl.h - Main program class for the APL interpreter

  Brian B. McGuinness     July, 2004
------------------------------------------------------------------------------*/
#ifndef apl_h
#define apl_h

#include <gdk/gdk.h>

// --- this includes everything; not the best procedure (fix later):
#include <gtkmm.h>

class APLInterpreter : public Gtk::Window {
  public:
    APLInterpreter();
    virtual ~APLInterpreter();

  protected:
    // Signal handlers:
    bool OnKeystroke (GdkEventKey *Event);

    // Member widgets:  
    Gtk::Entry                   *MainWindowEntry;
    Gtk::ScrolledWindow           MainScrolledWindow;
    Gtk::TextView                 MainView;
    Glib::RefPtr<Gtk::TextBuffer> MainTextBuffer;
};

#endif // !apl_h


/*------------------------------------------------------------------------------
  apl.cpp - Main application object for the APL interpreter
------------------------------------------------------------------------------*/
#include <iostream> // --- for testing only

#include "apl.h"
#include "basedefs.h"

/*------------------------------------------------------------------------------
  APLInterpreter::APLInterpreter - Initialize the APL interpreter
------------------------------------------------------------------------------*/
APLInterpreter::APLInterpreter() {
  set_title (_("APL Interpreter"));
  set_border_width (5);
  set_default_size (600, 400);

  // Attach the scrolled window to the main window
  add (MainScrolledWindow);

  MainScrolledWindow.add (MainView);

  // Only show the scrollbars when they are necessary
  MainScrolledWindow.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

  // Intercept keystrokes to perform character translations
  //******************** this doesn't work ********************
  MainWindowEntry = manage (new Gtk::Entry);
  MainWindowEntry->signal_key_press_event().connect (SigC::slot (&APLInterpreter::OnKeystroke), false);
  //***********************************************************

  MainTextBuffer = Gtk::TextBuffer::create();
  //... not done yet: construct the intro string and insert it below
  MainTextBuffer->set_text ("uid) yyyy-mm-dd hh:mm:ss username\n\n program ID & version\n\n");

  MainView.set_buffer (MainTextBuffer);

  show_all_children();
}

/*------------------------------------------------------------------------------
  APLInterpreter::~APLInterpreter - Clean up in preparation to terminate
------------------------------------------------------------------------------*/
APLInterpreter::~APLInterpreter() {
  //... disconnect from signal
}

/*------------------------------------------------------------------------------
  APLInterpreter::OnKeystroke - Perform keyboard mapping

  We translate incoming keystrokes into the appropriate APL characters.
------------------------------------------------------------------------------*/
bool APLInterpreter::OnKeystroke (GdkEventKey *Keystroke) {
  if (Keystroke->type == GDK_KEY_PRESS) {
    std::cout << (Keystroke->keyval) << ' ' << (Keystroke->state) << '\n';
    return 0;
  }
  else return 0;
}

/*------------------------------------------------------------------------------
  Main Program 
------------------------------------------------------------------------------*/
int main (int argc, char *argv[]) {
  Gtk::Main kit (argc, argv);

  APLInterpreter Application;
  Gtk::Main::run (Application);
    
  return 0;
}



# make
g++ apl.cpp -o apl `pkg-config gtkmm-2.0 --cflags --libs`
apl.cpp: In constructor `APLInterpreter::APLInterpreter()':
apl.cpp:27: error: no matching function for call to `Glib::SignalProxy1<bool,
   GdkEventKey*>::connect(SigC::Slot2<bool, APLInterpreter&, GdkEventKey*>,
   bool)'
/usr/include/gtkmm-2.0/glibmm/signalproxy.h:136: error: candidates are:
   SigC::Connection Glib::SignalProxy1<R, P1>::connect(const SigC::Slot1<R,
   P1>&, bool) [with R = bool, P1 = GdkEventKey*]
make: *** [apl] Error 1




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]