为什么使用键盘进行选择时未触发选择事件?

huangapple go评论76阅读模式
英文:

Why Selection event not triggered when selection is done using keyboard?

问题

我有这个使用SWT的Eclipse RCP应用程序。以下是示范代码:

Combo combo = new Combo(shell, SWT.NONE);
combo.setItems(items); // items是一个String[]
combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

combo.addSelectionListener(new SelectionAdapter() {
     
    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
        System.out.println("在widgetDefaultSelected中");
    }

    @Override
    public void widgetSelected(SelectionEvent e) {
        System.out.println("在widgetSelected中");
    }
});

这段代码中,Combo 已经在代码中进行了自动完成的设置。选择事件应该在鼠标或键盘事件触发时被触发。使用鼠标进行选择会触发选择事件,但使用键盘进行选择则不会。我正在尝试找出原因。

我的Eclipse版本不是最新的,是3.6.2版本,并附带了相应的SWT JAR文件。如果有任何帮助,我将不胜感激。

英文:

I have this Eclipse RCP application which uses SWT. Here is a sample code.

    Combo combo = new Combo(shell, SWT.NONE);
    combo.setItems(items); // items is a String[]
    combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

    combo.addSelectionListener(new SelectionAdapter() {
         
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        	System.out.println("In widgetDefaultSelected");
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
        	System.out.println("In widgetSelected");
        }
    }); 

The combo has been set up in the code for auto complete. The selection event is supposed to get triggered for mouse or keyboard events. A selection using mouse triggers the selection event but one with keyboard does not. I am trying to see why.

My eclipse is not the latest, it is version is 3.6.2 and the swt JARs that come with it. I would appreciate any help.

答案1

得分: 0

由于选择事件不会通过键盘触发,我在组合小部件上添加了一个键盘监听器,然后检查用户是否按下了回车键。

combo.addKeyListener(new KeyListener() {

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.keyCode==SWT.CR || e.keyCode==SWT.KEYPAD_CR) { // 回车键
            Combo c = (Combo) e.getSource();
            System.out.println(c.getText());
            // 进行其余处理
        }
    }
});

看起来我可以从列表框中获取到所选的项目。到目前为止,似乎运行得还不错。

英文:

Since the selection event is not triggered with keyboard, I added a KeyListener to the combo widget and check to see if the user has pressed enter key.

    combo.addKeyListener(new KeyListener() {

		@Override
		public void keyPressed(KeyEvent e) {

		}

		@Override
		public void keyReleased(KeyEvent e) {
			if (e.keyCode==SWT.CR || e.keyCode==SWT.KEYPAD_CR) { // Enter key
				Combo c = (Combo) e.getSource();
				System.out.println(c.getText());
                // Do rest of processing
			}
		}
    });

Seems like I am getting the selected item out of the list box. So far it seems to be working OK.

答案2

得分: 0

选择事件不用于键盘事件,Combo#addSelectionListener 的 Javadoc 在这里解释得很清楚:

<p>

  • 当用户更改组合框的列表选择时,将调用<code>widgetSelected</code>。

  • 通常在按下回车键时会调用<code>widgetDefaultSelected</code>,即组合框的文本区域。
    </p>

英文:

Selection event is not used for keyboard events, the Javadoc of Combo#addSelectionListener is pretty clear here:

<p>

  • <code>widgetSelected</code> is called when the user changes the combo's list selection.

  • <code>widgetDefaultSelected</code> is typically called when ENTER is pressed the combo's text area.
    </p>

huangapple
  • 本文由 发表于 2020年10月24日 01:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64504895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定