gpt4 book ai didi

java - 如何在 SWT ListViewer 上捕获 Ctrl 键按下和释放

转载 作者:行者123 更新时间:2023-11-30 07:28:54 24 4
gpt4 key购买 nike

我正在尝试根据 Ctrl 按键在 SWT ListViewer 上对元素进行多重选择。我想区分使用 Ctrl 键的选择和 ListViewer 上的简单选择。我没有找到如何查看 Ctrl 键是否被按下或释放的方法。我没有看到任何有用的监听器补充。有没有办法通过 SWT ListViewer 上的按键和释放来了解选择。这是我尝试过的示例代码。

public class DummyListViewer {

private ListViewer dummyViewer;

public DummyListViewer (Composite parent) {
dummyViewer = createListViewer(parent);
}

private ListViewer createListViewer(Composite parent) {
GridData data = new GridData(SWT.LEFT, SWT.LEFT, false, false);
ScrolledComposite scrolledParent = (ScrolledComposite) createScrolledComposite(parent, SWT.NONE);
final ListViewer dummyViewer= new ListViewer(scrolledParent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL| SWT.MULTI);
dummyViewer.getList().addKeyListener(new CtrlKeyListener());
dummyViewer.setContentProvider(new ListViewerContentProvider());
dummyViewer.setLabelProvider(new ListViewerLabelProvider());
scrolledParent.setContent(dummyViewer.getControl());
scrolledParent.setMinSize(dummyViewer.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT));
dummyViewer.getControl().setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
return dummyViewer;
}

private Composite createScrolledComposite(Composite parent, int style) {
ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setLayout(new GridLayout(1, false));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData.widthHint = 400;
gridData.heightHint = 700;
scrolledComposite.setLayoutData(gridData);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setExpandHorizontal(true);
return scrolledComposite;
}

public void setInput(List<Object> input) {
filterViewer.setInput(input);
}

private class ListViewerContentProvider implements IStructuredContentProvider {

@Override
public void dispose() {

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

}

@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
return ((List)inputElement).toArray();
}
return null;
}

}

private ColumnViewerFilter filter;



private class ColumnViewerFilter extends ViewerFilter {

String value=""; //$NON-NLS-1$

/**
* Constructor for ColumnViewerFilter to set the String on which
* the viewer has to be filtered on.
* @param value - String on which
* the viewer has to be filtered on.
*/
public ColumnViewerFilter(String value) {
this.value = value;
}

@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (value != null && value instanceof String && element instanceof String) {
if (value.equals(element))
return true;
}
return false;
}

}

private class ListViewerLabelProvider extends ColumnLabelProvider {

@Override
public String getText(Object element) {
if(element instanceof String)
return (String)element;
return null;
}

}



private boolean CtrlkeyPressed = false;
private class CtrlKeyListener implements KeyListener{

@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CTRL)
CtrlkeyPressed = true;

}

@Override
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.CTRL)
CtrlkeyPressed = false;
}

}
}

最佳答案

您可以使用 ListViewer 中包含的 List 控件上的 addKeyListener 监听按键事件:

viewer.getList().addKeyListener(new KeyListener() {

@Override
public void keyPressed(final KeyEvent e)
{
if (e.keyCode == SWT.CTRL)
// TODO control pressed
}

@Override
public void keyReleased(final KeyEvent e)
{
if (e.keyCode == SWT.CTRL)
// TODO control release
}

});

请注意,在某些平台上,多重选择不使用 Ctrl 键。在 Mac 上,使用 键 (SWT.COMMAND)。

关于java - 如何在 SWT ListViewer 上捕获 Ctrl 键按下和释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com