Advertisement
Guest User

Multipress Keyboard

a guest
May 15th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. import java.awt.event.KeyEvent;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4.  
  5. import org.havi.ui.HSinglelineEntry;
  6. import org.havi.ui.event.HRcEvent;
  7.  
  8. public class KeyboardController {
  9.    
  10.     private final static int timeInSeconds = 2;
  11.     private KeyEvent event;
  12.     private HSinglelineEntry in;
  13.     private static int lastKey = -1;
  14.     private static int pressCounter = 0;
  15.     private static Timer timer = new Timer();
  16.     private static int sync = 1;
  17.    
  18.     private int selected;
  19.    
  20.     public KeyboardController(KeyEvent e, HSinglelineEntry input) {
  21.         event=e;
  22.         in=input;
  23.     }
  24.  
  25.     public void selectKeyboard() {
  26.         if(lastKey == event.getKeyCode() && sync > 0) {
  27.             sync++;
  28.         }
  29.         else {
  30.             pressCounter = 0;
  31.             sync = 1;
  32.             lastKey = event.getKeyCode();
  33.         }
  34.         selected=pressCounter++ % 3;
  35.         timer.schedule(new ResetButton(in), timeInSeconds * 1000);
  36.        
  37.         switch(event.getKeyCode()){
  38.         case HRcEvent.VK_ENTER:
  39.             in.setEditMode(false);
  40.             break;
  41.         case HRcEvent.VK_RIGHT:
  42.             in.setCaretCharPosition(in.getCaretCharPosition()+1);
  43.             break;
  44.         case HRcEvent.VK_LEFT:
  45.             if(in.getCaretCharPosition()>0)
  46.                 in.setCaretCharPosition(in.getCaretCharPosition()-1);  
  47.             break;
  48.         case HRcEvent.VK_2:
  49.             if(selected==0)
  50.                 in.insertChar('a');
  51.             else if(selected==1)
  52.                 in.insertChar('b');
  53.             else if(selected==2)
  54.                 in.insertChar('c');
  55.             break;
  56. ...
  57.  
  58.     }
  59. }
  60.    
  61.     static class ResetButton extends TimerTask {
  62.         HSinglelineEntry in;
  63.         public ResetButton(HSinglelineEntry input) {
  64.             in=input;
  65.         }
  66.                
  67.         @Override
  68.         public void run() {
  69.             // This timer is finished
  70.             if(sync > 0)
  71.                 sync--;
  72.             in.setCaretCharPosition(in.getCaretCharPosition()+1);
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement