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

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

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

问题

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

  1. Combo combo = new Combo(shell, SWT.NONE);
  2. combo.setItems(items); // items是一个String[]
  3. combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  4. combo.addSelectionListener(new SelectionAdapter() {
  5. @Override
  6. public void widgetDefaultSelected(SelectionEvent e) {
  7. System.out.println("在widgetDefaultSelected中");
  8. }
  9. @Override
  10. public void widgetSelected(SelectionEvent e) {
  11. System.out.println("在widgetSelected中");
  12. }
  13. });

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

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

英文:

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

  1. Combo combo = new Combo(shell, SWT.NONE);
  2. combo.setItems(items); // items is a String[]
  3. combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  4. combo.addSelectionListener(new SelectionAdapter() {
  5. @Override
  6. public void widgetDefaultSelected(SelectionEvent e) {
  7. System.out.println("In widgetDefaultSelected");
  8. }
  9. @Override
  10. public void widgetSelected(SelectionEvent e) {
  11. System.out.println("In widgetSelected");
  12. }
  13. });

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

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

  1. combo.addKeyListener(new KeyListener() {
  2. @Override
  3. public void keyPressed(KeyEvent e) {
  4. }
  5. @Override
  6. public void keyReleased(KeyEvent e) {
  7. if (e.keyCode==SWT.CR || e.keyCode==SWT.KEYPAD_CR) { // 回车键
  8. Combo c = (Combo) e.getSource();
  9. System.out.println(c.getText());
  10. // 进行其余处理
  11. }
  12. }
  13. });

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

英文:

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.

  1. combo.addKeyListener(new KeyListener() {
  2. @Override
  3. public void keyPressed(KeyEvent e) {
  4. }
  5. @Override
  6. public void keyReleased(KeyEvent e) {
  7. if (e.keyCode==SWT.CR || e.keyCode==SWT.KEYPAD_CR) { // Enter key
  8. Combo c = (Combo) e.getSource();
  9. System.out.println(c.getText());
  10. // Do rest of processing
  11. }
  12. }
  13. });

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:

确定