Saturday 20 December 2014

Detecting key events in Console application Using JLine


JLine is nothing but a java library for handling console inputs.It can detects various keys which System class can not detect.

For ex:you can not detect ESC key or ENTER key pressed in java.

JLine ConsoleReader object can detects special key using its readVirtualKey() function.
For more information about JLine.

You can download the jar from http://www.java2s.com/Code/Jar/j/Downloadjlinejar.htm.

For maven dependency refer this http://mvnrepository.com/artifact/jline/jline

Using Jline you can ROTATE the list using UP and DOWN arrow key.
For ex: If you want to rotate list of country names. using UP and DOWN arrow key

public String ArrowKeysForSelctingCountryName() throws IOException {  
           List<String> listCountry = new ArrayList<String>();  
           listCountry.add("India");  
           listCountry.add("Australia");  
           listCountry.add("UK");  
           int i = 0;  
           boolean isValueSelected = false;  
           String strCountryName = null;  
           ListIterator<String> itr = listCountry.listIterator();  
           int key;  
           ConsoleReader consoleReader = new ConsoleReader();  
           while ((key = consoleReader.readVirtualKey()) != 10) {  
                // to clear previous list name ,you can customise it according to you.  
                for (int clear = 0; clear < 100; clear++) {  
                     consoleReader.delete();  
                }  
                switch (key) {  
                // UP arrow key  
                case 65539:  
                     isValueSelected = true;  
                     for (; i < listCountry.size(); i++) {  
                          if (itr.hasNext()) {  
                               strCountryName = itr.next();  
                               consoleReader.putString(strCountryName);  
                               Collections.rotate(listCountry, i);  
                               consoleReader.moveCursor(-(strCountryName.length()));  
                               break;  
                          } else {  
                               while (itr.hasPrevious()) {  
                                    itr.previous();  
                               }  
                          }  
                     }  
                     if (i == listCountry.size() - 1) {  
                          while (itr.hasPrevious()) {  
                               itr.previous();  
                          }  
                     }  
                     break;  
                // Down Arrow Key  
                case 65540:  
                     isValueSelected = true;  
                     for (; i < listCountry.size(); i++) {  
                          if (itr.hasNext()) {  
                               strCountryName = itr.next();  
                               consoleReader.putString(strCountryName);  
                               Collections.rotate(listCountry, i);  
                               consoleReader.moveCursor(-(strCountryName.length()));  
                               break;  
                          } else {  
                               while (itr.hasPrevious()) {  
                                    itr.previous();  
                               }  
                          }  
                     }  
                     if (i == listCountry.size() - 1) {  
                          while (itr.hasPrevious()) {  
                               itr.previous();  
                          }  
                     }  
                     break;  
                }  
           }  
           if (key == 10 && strCountryName.length() > 0 && isValueSelected == true) {  
                return strCountryName;  
           }  
           return strCountryName;  
      }  


You can also implement AUTO-COMPLETE functionality using TAB key.
Here is the sample code : to implement auto completion of students names.

 public static void AutoCompleteSelectingStudentsName() throws IOException {  
         List<String> lStudentsName = new ArrayList<String>();  
         lStudentsName.add("Ramesh");  
         lStudentsName.add("John");  
         lStudentsName.add("Bob");  
         // convert into string array  
         String[] arrStudentsNames = lStudentsName.toArray(new String[lStudentsName.size()]);  
         ConsoleReader consoleReader= new ConsoleReader();  
         consoleReader.addCompletor(new SimpleCompletor(arrStudentsNames));  
        }  





No comments:

Post a Comment