Back to index

wims  3.65+svn20090927
KeyboardController.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.awt.Component;
00004 import java.awt.event.KeyEvent;
00005 import java.awt.event.KeyListener;
00006 
00007 public class KeyboardController
00008        implements KeyListener
00009 {      
00010        boolean Escape=false;
00012        boolean IgnoreTyped=false;
00014        int CommandType=0;
00016        Component C=null;
00018        KeyboardInterface Primary=null,Secondary=null;
00019        
00020        public synchronized void keyPressed (KeyEvent e)
00021        {      if (e.getKeyCode()==KeyEvent.VK_SHIFT) return;
00022               if (e.getKeyCode()==KeyEvent.VK_CONTROL) return;
00023               if (e.getKeyCode()==KeyEvent.VK_ALT) return;
00024               if (old(e)) return;
00025               String s=Keyboard.findKey(e,CommandType);
00026               // System.out.println(Escape+" "+CommandType+" "+s);
00027               IgnoreTyped=false;
00028               if (s.startsWith("command."))
00029               {      if (s.equals("command.escape"))
00030                      {      Escape=!Escape;
00031                      }
00032                      else try
00033                      {      CommandType=Integer.parseInt(s.substring(8));
00034                             Escape=false;
00035                      }
00036                      catch (Exception ex) { CommandType=0; }
00037                      IgnoreTyped=true;
00038               }
00039               else if (s.startsWith("charkey."))
00040               {      keyboardCommand(e,s);
00041                      IgnoreTyped=true;
00042                      Escape=false;
00043                      CommandType=0;
00044               }
00045               else if (Escape)
00046               {      char c=e.getKeyChar();
00047                      IgnoreTyped=true;
00048                      keyboardEscape(e,c);
00049                      Escape=false;
00050               }
00051               else if (!s.equals(""))
00052               {      keyboardCommand(e,s);
00053                      IgnoreTyped=false;
00054                      Escape=false;
00055                      CommandType=0;
00056               }
00057               else if (!e.isActionKey())
00058               {      if (!Global.getParameter("keyboard.compose",true))
00059                      {      keyboardChar(e,e.getKeyChar());
00060                             Escape=false;
00061                             CommandType=0;
00062                      }
00063                      else
00064                      {      Escape=false;
00065                             CommandType=0;
00066                      }
00067               }
00068        }
00069        
00070        public void keyTyped (KeyEvent e)
00071        {      if (!Global.getParameter("keyboard.compose",true)) return;
00072               // System.out.println("key typed "+IgnoreTyped+" "+e);
00073               if (IgnoreTyped) return;
00074               IgnoreTyped=false;
00075               keyboardChar(e,e.getKeyChar());
00076               Escape=false;
00077               CommandType=0;
00078        }      
00079 
00080        public void keyReleased (KeyEvent e) {}
00081        
00082        public void keyboardCommand (KeyEvent e, String command)
00083        {      // System.out.println(command);
00084               if (Primary==null || !Primary.keyboardCommand(e,command))
00085                      if (Secondary!=null) Secondary.keyboardCommand(e,command);
00086        }
00087        
00088        public void keyboardEscape (KeyEvent e, char c)
00089        {      //System.out.println("escape "+c);
00090               if (Primary==null || !Primary.keyboardEscape(e,c))
00091                      if (Secondary!=null) Secondary.keyboardEscape(e,c);
00092        }
00093        
00094        public void keyboardChar (KeyEvent e, char c)
00095        {      //System.out.println(""+c);
00096               if (Primary==null || !Primary.keyboardChar(e,c))
00097                      if (Secondary!=null) Secondary.keyboardChar(e,c);
00098        }
00099        
00100        boolean scaled=false; // scaled already
00101        long scale; // the scaling in milliseconds
00102        
00108        boolean old (KeyEvent e)
00109        {      if (!scaled)
00110               {      scaled=true; scale=System.currentTimeMillis()-e.getWhen();
00111                      return false;
00112               }
00113               long delay=System.currentTimeMillis()-e.getWhen()-scale;
00114               if (delay>10000) return false; // function does not work!
00115               return (delay>200);
00116        }
00117        
00118        public void listenTo (Component c)
00119        {      if (C!=null) C.removeKeyListener(this);
00120               C=c;
00121               if (C!=null) C.addKeyListener(this);
00122        }
00123        
00124        public void setPrimary (KeyboardInterface i)
00125        {      Primary=i;
00126        }
00127        
00128        public void setSecondary (KeyboardInterface i)
00129        {      Secondary=i;
00130        }
00131 }