CaiNiaoCoder
CaiNiaoCoder

Reputation: 3319

eclipse rcp: key binding with toolitem

I have a toolbar in my eclipse rcp application, it contains three buttons,

  1. home button: go to home page
  2. back button: go to previous page
  3. forward button: go to next page

the code create them :

ToolBar tb = new ToolBar(toolBarComp, SWT.NONE);
        fd = new FormData();
        fd.right = new FormAttachment(100, -10);
        tb.setLayoutData(fd);

        homeBtn = new ToolItem(tb, SWT.PUSH);
        homeBtn.setImage(ResourceManager.getImage(ResourceClass.class, "home.png"));
        homeBtn.setToolTipText("返回主页");

        backBtn = new ToolItem(tb, SWT.PUSH);
        backBtn.setImage(ResourceManager.getImage(ResourceClass.class, "back.png"));
        backBtn.setToolTipText("返回至上一页");
        backBtn.setEnabled(false);
        backBtn.addSelectionListener(new SelectionAdapter(){

            public void widgetSelected(SelectionEvent e) {
                CompositePageRender.this.controler.goBack();
            }

        });

        forwardBtn = new ToolItem(tb, SWT.PUSH);
        forwardBtn.setImage(ResourceManager.getImage(ResourceClass.class, "forward.png"));
        forwardBtn.setToolTipText("前进至下一页");
        forwardBtn.setEnabled(false);
        forwardBtn.addSelectionListener(new SelectionAdapter(){

            public void widgetSelected(SelectionEvent e) {
                CompositePageRender.this.controler.goFroward();
            }

        });

I wan to bind ALT+H with the home button, is that possible ? how to do?

Upvotes: 0

Views: 887

Answers (1)

Paul Webster
Paul Webster

Reputation: 10654

Within an SWT application you can use mnemonics to simulate pressing Buttons.

But if you mean an RCP application with ToolItems in a toolbar, you need to use the command framework to use keybindings.

You define a command, and then use a handle to implement the behaviour. Then you have both your ToolItem and keybinding ALT+H point to the same command.

See Workbench extension points using Commands for more information.

Upvotes: 2

Related Questions