1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.webkit.event;
  27 
  28 import java.lang.annotation.Native;
  29 
  30 public final class WCKeyEvent {
  31 
  32     // Event types
  33     @Native public static final int KEY_TYPED       = 0;
  34     @Native public static final int KEY_PRESSED     = 1;
  35     @Native public static final int KEY_RELEASED    = 2;
  36 
  37     // Windows virtual key code constants used by WebKit Java port
  38     @Native public static final int VK_BACK         = 0x08;
  39     @Native public static final int VK_TAB          = 0x09;
  40     @Native public static final int VK_RETURN       = 0x0D;
  41     @Native public static final int VK_ESCAPE       = 0x1B;
  42     @Native public static final int VK_PRIOR        = 0x21;
  43     @Native public static final int VK_NEXT         = 0x22;
  44     @Native public static final int VK_END          = 0x23;
  45     @Native public static final int VK_HOME         = 0x24;
  46     @Native public static final int VK_LEFT         = 0x25;
  47     @Native public static final int VK_UP           = 0x26;
  48     @Native public static final int VK_RIGHT        = 0x27;
  49     @Native public static final int VK_DOWN         = 0x28;
  50     @Native public static final int VK_INSERT       = 0x2D;
  51     @Native public static final int VK_DELETE       = 0x2E;
  52     @Native public static final int VK_OEM_PERIOD   = 0xBE;
  53 
  54 
  55     private final int type;
  56     private final long when;
  57     private final String text;
  58     private final String keyIdentifier;
  59     private final int windowsVirtualKeyCode;
  60     private final boolean shift;
  61     private final boolean ctrl;
  62     private final boolean alt;
  63     private final boolean meta;
  64 
  65 
  66     public WCKeyEvent(int type, String text, String keyIdentifier,
  67                       int windowsVirtualKeyCode, boolean shift, boolean ctrl,
  68                       boolean alt, boolean meta, long when)
  69     {
  70         this.type = type;
  71         this.text = text;
  72         this.keyIdentifier = keyIdentifier;
  73         this.windowsVirtualKeyCode = windowsVirtualKeyCode;
  74         this.shift = shift;
  75         this.ctrl = ctrl;
  76         this.alt = alt;
  77         this.meta = meta;
  78         this.when = when;
  79     }
  80 
  81 
  82     public int getType() { return type; }
  83     public long getWhen() { return when; }
  84     public String getText() { return text; }
  85     public String getKeyIdentifier() { return keyIdentifier; }
  86     public int getWindowsVirtualKeyCode() { return windowsVirtualKeyCode; }
  87     public boolean isShiftDown() { return shift; }
  88     public boolean isCtrlDown() { return ctrl; }
  89     public boolean isAltDown() { return alt; }
  90     public boolean isMetaDown() { return meta; }
  91 
  92     public static boolean filterEvent(WCKeyEvent ke) {
  93         if (ke.getType() == KEY_TYPED) {
  94             String text = ke.getText();
  95             if (text == null || text.length() != 1) {
  96                 return true;
  97             }
  98             char kc = text.charAt(0);
  99             // don't send KEY_TYPED events to WebKit for some control keys
 100             // like DELETE, BACKSPACE, etc.
 101             if ((kc == '\b') || (kc == '\n') || (kc == '\t') ||
 102                 (kc == '\uffff') || (kc == '\u0018') || (kc == '\u001b') || (kc == '\u007f'))
 103             {
 104                 return true;
 105             }
 106         }
 107         return false;
 108     }
 109 }