FL_Up and FL_Down as shortcuts in the MacOS system menubar

10 views
Skip to first unread message

geert karman

unread,
Sep 29, 2022, 3:39:07 PM9/29/22
to fltk.general
Hello, 

I want to use the FL_Up and FL_Down keys in the Mac_OS system menubar as shortcuts. However, when I try that, these are shown as 'R' and 'S' respectively and the arrows do not work as shortcuts. If I do the same in a normal Fl_Menu_Bar everything works as expected. Do I have to do something different for the system menubar?

Working example code below:

#include <FL/Fl.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Sys_Menu_Bar.H>
#include <stdio.h>

Fl_Menu_Item menutable[] = {
  {"&File",0,0,0,FL_SUBMENU},
  {"shortcut up",FL_Up},
  {"shortcut down",FL_Down},
  {0},
  {0}
};

Fl_Menu_Item menu_location[] = {
  {"Fl_Menu_Bar",       0, 0, 0, FL_MENU_VALUE},
  {"Fl_Sys_Menu_Bar",   },
  {0}
};

Fl_Sys_Menu_Bar* smenubar;

void menu_location_cb(Fl_Widget* w, void* data)
{
  Fl_Menu_Bar *menubar = (Fl_Menu_Bar*)data;
  if (((Fl_Choice*)w)->value() == 1) { // switch to system menu bar
    menubar->hide();
    smenubar = new  Fl_Sys_Menu_Bar(0,0,0,30);
    smenubar->menu(menutable);
    }
  else { // switch to window menu bar
    menubar->menu(menutable);
    smenubar->clear();
    delete smenubar;
    menubar->show();
    }
}

void test_cb(Fl_Widget* w, void*) {
  puts("test callback called");  
}

int main(int argc, char **argv) {
  Fl_Double_Window window(250,150);
  Fl_Menu_Bar menubar(0,0,250,25);
  menubar.menu(menutable);
  menubar.callback(test_cb);
  Fl_Choice ch2(50,75,150,25,"Use:");
  ch2.menu(menu_location);
  ch2.callback(menu_location_cb, &menubar);
  ch2.value(1);
  menu_location_cb(&ch2, &menubar);
  window.end();
  Fl_Menu_Item custom[] = {
    {"Preferences…",  0, 0, NULL, FL_MENU_DIVIDER},
    {"Radio1",        0, 0, NULL, FL_MENU_RADIO | FL_MENU_VALUE},
    {"Radio2",        0, 0, NULL, FL_MENU_RADIO | FL_MENU_DIVIDER},
    {0}
  };
  window.show(argc, argv);
  return Fl::run();
}

Manolo

unread,
Sep 30, 2022, 2:01:18 AM9/30/22
to fltk.general
Hi Geert,

Support for using arrow keys as item shortcuts in Fl_Sys_Menu_Bar
has been added to the FLTK git repository (63dcdce).
Please, pull this last change for your app.

But, you also have to attach the callback function to each menu item
as shown in the modified example program copied below
(I have also added items using the left and right arrows ).
The program also shows the use of fl_shortcut_label() useful to get a
printable representation of menu item shortcuts.

#include <FL/Fl.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Sys_Menu_Bar.H>
#include <FL/fl_draw.H>
#include <stdio.h>

void item_cb(Fl_Widget* w, void *name) {
  Fl_Menu_* mw = (Fl_Menu_*)w;
  const Fl_Menu_Item* m = mw->mvalue();
  if (!m)
    printf("NULL\n");
  else if (m->shortcut())
    printf("%s - %s\n", m->label(), fl_shortcut_label(m->shortcut()));
  else
    printf("%s\n", m->label());

}

Fl_Menu_Item menutable[] = {
  {"&File",0,0,0,FL_SUBMENU},
  {"item-up",FL_Up, item_cb},
  {"item-down",FL_Down, item_cb},
  {"item-left",FL_Left, item_cb},
  {"item-right",FL_Right, item_cb},

  {0},
  {0}
};

Fl_Menu_Item menu_location[] = {
  {"Fl_Menu_Bar",       0, 0, 0, FL_MENU_VALUE},
  {"Fl_Sys_Menu_Bar",   },
  {0}
};

Fl_Sys_Menu_Bar* smenubar;

void menu_location_cb(Fl_Widget* w, void* data)
{
  Fl_Menu_Bar *menubar = (Fl_Menu_Bar*)data;
  if (((Fl_Choice*)w)->value() == 1) { // switch to system menu bar
    menubar->hide();
    smenubar = new  Fl_Sys_Menu_Bar(0,0,0,30);
    smenubar->menu(menutable);
    }
  else { // switch to window menu bar
    menubar->menu(menutable);
    smenubar->clear();
    delete smenubar;
    menubar->show();
    }
}

/*void test_cb(Fl_Widget* w, void*) {

  puts("test callback called");
}*/


int main(int argc, char **argv) {
  Fl_Double_Window window(250,150);
  Fl_Menu_Bar menubar(0,0,250,25);
  menubar.menu(menutable);
//  menubar.callback(test_cb);

geert karman

unread,
Sep 30, 2022, 3:15:35 AM9/30/22
to fltk.general


Op vrijdag 30 september 2022 om 07:01:18 UTC+1 schreef Manolo:
Hi Geert,

Support for using arrow keys as item shortcuts in Fl_Sys_Menu_Bar
has been added to the FLTK git repository (63dcdce).
Please, pull this last change for your app.
 
Great! Just checked it and it works fine.
 
But, you also have to attach the callback function to each menu item
as shown in the modified example program copied below
 
Understood.

Thanks a lot.
Reply all
Reply to author
Forward
0 new messages