简体   繁体   中英

How to disable left and right arrow keys for navigation in SWT List?

The SWT List lets both left and up arrow keys to go up the list items and both right and down arrow keys to go down the list items by default. But I want to use left and right arrow keys for another propose. How can I remove the left and right arrow keys listener that is in SWT List by default? And then replace my own. Here is the code I want to use:

Listener listListener = new Listener() {
        public void handleEvent(Event e) {
            int RowIndex = listData.getSelectionIndex();

            if(RowIndex == 0){
                if (e.keyCode == SWT.ARROW_RIGHT) { //right arrow
                    //do something
                }else if (e.keyCode == SWT.ARROW_LEFT) { //left arrow
                    //do something
                }
             }
         }
    };
    list.addListener(SWT.KeyDown, listListener);

Thank you!

You can suppress the default behavior of key strokes by setting the doit flag to false like this:

if( event.keyCode == SWT.ARROW_RIGHT ) {
  // do something    
  event.doit = false;
}

The default behavior of arrows keys is very likely platform dependant. Windows behaves as you describe, but other platforms might behave differently. Setting the doit flag to false will work in any case, whether the key has a default behavior or not.

I would rather not override platform behavior because your applications will then behave inconsistent with respect to the platforms general usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM