@Override public void onKeyDown(final KyPressedEvent evt) { final Accelerator acc = evt.getAccelerator(); if (acc.getKeyCode() == KeyCode.UNDEFINED) return; ModuleInfo moduleInfo = null; // look up the module corresponding to this key press moduleInfo = moduleService.getModuleForAccelerator(acc); // TODO: ask options service whether the default modifier should be forced final boolean addModifierAutomatically = true; if (moduleInfo == null && addModifierAutomatically) { // look up the module corresponding to this key press, plus control final KeyCode keyCode = acc.getKeyCode(); final InputModifiers modifiers = forceDefaultModifier(acc.getModifiers()); final Accelerator modAcc = new Accelerator(keyCode, modifiers); if (!acc.equals(modAcc)) { moduleInfo = moduleService.getModuleForAccelerator(modAcc); } } if (moduleInfo == null) return; // no matching module found // run via command service, so that preprocessors are run moduleService.run(moduleInfo, true, new Object[0]); // FIXME // consume event, so that nothing else tries to handle it evt.consume(); }
@Override public ModuleInfo getModuleForAccelerator(final Accelerator acc) { for (final ModuleInfo info : getModules()) { final MenuPath menuPath = info.getMenuPath(); if (menuPath == null || menuPath.isEmpty()) continue; if (acc.equals(menuPath.getLeaf().getAccelerator())) return info; } return null; }
private InputModifiers forceDefaultModifier(final InputModifiers modifiers) { final boolean forceMeta = Accelerator.isCtrlReplacedWithMeta(); final boolean forceCtrl = !forceMeta; final boolean alt = modifiers.isAltDown(); final boolean altGr = modifiers.isAltGrDown(); final boolean ctrl = forceCtrl || modifiers.isCtrlDown(); final boolean meta = forceMeta || modifiers.isMetaDown(); final boolean shift = modifiers.isShiftDown(); return new InputModifiers(alt, altGr, ctrl, meta, shift, false, false, false); }
private void parseMenu( final MenuItem menuItem, final double weight, final MenuPath path, final Map<String, MenuPath> menuTable) { // build menu entry final String name = menuItem.getLabel(); final MenuEntry entry = new MenuEntry(name, weight); final MenuShortcut shortcut = menuItem.getShortcut(); if (shortcut != null) { // convert AWT MenuShortcut to ImageJ Accelerator final int code = shortcut.getKey(); final boolean meta = Accelerator.isCtrlReplacedWithMeta(); final boolean ctrl = !meta; final boolean shift = shortcut.usesShiftModifier(); final KeyCode keyCode = KeyCode.get(code); final InputModifiers modifiers = new InputModifiers(false, false, ctrl, meta, shift, false, false, false); final Accelerator acc = new Accelerator(keyCode, modifiers); entry.setAccelerator(acc); } path.add(entry); if (menuItem instanceof Menu) { // non-leaf // recursively process child menu items final Menu menu = (Menu) menuItem; final int itemCount = menu.getItemCount(); double w = -1; for (int i = 0; i < itemCount; i++) { final MenuItem item = menu.getItem(i); final boolean isSeparator = item.getLabel().equals("-"); if (isSeparator) w += 10; else w += 1; parseMenu(item, w, new MenuPath(path), menuTable); } } else { // leaf item // add menu item to table menuTable.put(menuItem.getLabel(), path); } }