ckeyboard-shortcutsmotif

How to map Esc to an action in a Motif widget


This minimal example creates a Motif window with a push button widget.

#include <Xm/PushB.h>

int main(int argc, char **argv) {
  XtAppContext app;
  Widget       toplevel, b;
  char         *translations="<Key>Escape: ArmAndActivate()\n\
                              <Key>a:      ArmAndActivate()";

  toplevel = XtVaOpenApplication(&app, "tl", NULL, 0, &argc, argv, NULL,
                                 sessionShellWidgetClass, NULL);
  
  b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass, toplevel, NULL);
  XtOverrideTranslations(b, XtParseTranslationTable(translations));

  XtRealizeWidget(toplevel);
  XtAppMainLoop(app);
  return(0);
}

Both Esc and a are mapped to the same action, ArmAndActivate, but the widget only responds to a keypress, not to Esc. How can I make it respond to Esc? Note that Escape is indeed the correct keysym according to xev.


Solution

  • Escape is the osfCancel Motif virtual binding; in that sense it is already "occupied". So use

    <Key>osfCancel: ArmAndActivate()
    

    The virtual bindings can be modified via the ~/.motifbind file, see The Motif Programming Model and man VirtualBindings.