Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. import java.awt.Component;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6.  
  7. public class KeyboardController
  8.         implements KeyListener
  9. {       /** Macro and programs key pressed */
  10.         boolean Escape=false;
  11.         /** Note, if the next entry should be ignored or not */
  12.         boolean IgnoreTyped=false;
  13.         /** The type of the recent command key (1..5) */
  14.         int CommandType=0;
  15.         /** The component, which we are listening to. */
  16.         Component C=null;
  17.         /** The primary and secondary KeyboardInterface */
  18.         KeyboardInterface Primary=null,Secondary=null;
  19.        
  20.         public synchronized void keyPressed (KeyEvent e)
  21.         {       if (e.getKeyCode()==KeyEvent.VK_SHIFT) return;
  22.                 if (e.getKeyCode()==KeyEvent.VK_CONTROL) return;
  23.                 if (e.getKeyCode()==KeyEvent.VK_ALT) return;
  24.                 if (old(e)) return;
  25.                 String s=Keyboard.findKey(e,CommandType);
  26.                 // System.out.println(Escape+" "+CommandType+" "+s);
  27.                 IgnoreTyped=false;
  28.                 if (s.startsWith("command."))
  29.                 {       if (s.equals("command.escape"))
  30.                         {       Escape=!Escape;
  31.                         }
  32.                         else try
  33.                         {       CommandType=Integer.parseInt(s.substring(8));
  34.                                 Escape=false;
  35.                         }
  36.                         catch (Exception ex) { CommandType=0; }
  37.                         IgnoreTyped=true;
  38.                 }
  39.                 else if (s.startsWith("charkey."))
  40.                 {       keyboardCommand(e,s);
  41.                         IgnoreTyped=true;
  42.                         Escape=false;
  43.                         CommandType=0;
  44.                 }
  45.                 else if (Escape)
  46.                 {       char c=e.getKeyChar();
  47.                         IgnoreTyped=true;
  48.                         keyboardEscape(e,c);
  49.                         Escape=false;
  50.                 }
  51.                 else if (!s.equals(""))
  52.                 {       keyboardCommand(e,s);
  53.                         IgnoreTyped=false;
  54.                         Escape=false;
  55.                         CommandType=0;
  56.                 }
  57.                 else if (!e.isActionKey())
  58.                 {       if (!Global.getParameter("keyboard.compose",true))
  59.                         {       keyboardChar(e,e.getKeyChar());
  60.                                 Escape=false;
  61.                                 CommandType=0;
  62.                         }
  63.                         else
  64.                         {       Escape=false;
  65.                                 CommandType=0;
  66.                         }
  67.                 }
  68.         }
  69.        
  70.         public void keyTyped (KeyEvent e)
  71.         {       if (!Global.getParameter("keyboard.compose",true)) return;
  72.                 // System.out.println("key typed "+IgnoreTyped+" "+e);
  73.                 if (IgnoreTyped) return;
  74.                 IgnoreTyped=false;
  75.                 keyboardChar(e,e.getKeyChar());
  76.                 Escape=false;
  77.                 CommandType=0;
  78.         }      
  79.  
  80.         public void keyReleased (KeyEvent e) {}
  81.        
  82.         public void keyboardCommand (KeyEvent e, String command)
  83.         {       // System.out.println(command);
  84.                 if (Primary==null || !Primary.keyboardCommand(e,command))
  85.                         if (Secondary!=null) Secondary.keyboardCommand(e,command);
  86.         }
  87.        
  88.         public void keyboardEscape (KeyEvent e, char c)
  89.         {       //System.out.println("escape "+c);
  90.                 if (Primary==null || !Primary.keyboardEscape(e,c))
  91.                         if (Secondary!=null) Secondary.keyboardEscape(e,c);
  92.         }
  93.        
  94.         public void keyboardChar (KeyEvent e, char c)
  95.         {       //System.out.println(""+c);
  96.                 if (Primary==null || !Primary.keyboardChar(e,c))
  97.                         if (Secondary!=null) Secondary.keyboardChar(e,c);
  98.         }
  99.        
  100.         boolean scaled=false; // scaled already
  101.         long scale; // the scaling in milliseconds
  102.        
  103.         /**
  104.         Test for old keys. This algorithm uses the difference between
  105.         event time and system time. However, one needs to scale this
  106.         first, since in Linux both timers do not agree.
  107.         */
  108.         boolean old (KeyEvent e)
  109.         {       if (!scaled)
  110.                 {       scaled=true; scale=System.currentTimeMillis()-e.getWhen();
  111.                         return false;
  112.                 }
  113.                 long delay=System.currentTimeMillis()-e.getWhen()-scale;
  114.                 if (delay>10000) return false; // function does not work!
  115.                 return (delay>200);
  116.         }
  117.        
  118.         public void listenTo (Component c)
  119.         {       if (C!=null) C.removeKeyListener(this);
  120.                 C=c;
  121.                 if (C!=null) C.addKeyListener(this);
  122.         }
  123.        
  124.         public void setPrimary (KeyboardInterface i)
  125.         {       Primary=i;
  126.         }
  127.        
  128.         public void setSecondary (KeyboardInterface i)
  129.         {       Secondary=i;
  130.         }
  131. }
  132.