Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/Event.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.awt;2526import java.awt.event.*;27import java.io.*;2829/**30* <b>NOTE:</b> The <code>Event</code> class is obsolete and is31* available only for backwards compatibility. It has been replaced32* by the <code>AWTEvent</code> class and its subclasses.33* <p>34* <code>Event</code> is a platform-independent class that35* encapsulates events from the platform's Graphical User36* Interface in the Java 1.0 event model. In Java 1.137* and later versions, the <code>Event</code> class is maintained38* only for backwards compatibility. The information in this39* class description is provided to assist programmers in40* converting Java 1.0 programs to the new event model.41* <p>42* In the Java 1.0 event model, an event contains an43* {@link Event#id} field44* that indicates what type of event it is and which other45* <code>Event</code> variables are relevant for the event.46* <p>47* For keyboard events, {@link Event#key}48* contains a value indicating which key was activated, and49* {@link Event#modifiers} contains the50* modifiers for that event. For the KEY_PRESS and KEY_RELEASE51* event ids, the value of <code>key</code> is the unicode52* character code for the key. For KEY_ACTION and53* KEY_ACTION_RELEASE, the value of <code>key</code> is54* one of the defined action-key identifiers in the55* <code>Event</code> class (<code>PGUP</code>,56* <code>PGDN</code>, <code>F1</code>, <code>F2</code>, etc).57*58* @author Sami Shaio59* @since JDK1.060*/61public class Event implements java.io.Serializable {62private transient long data;6364/* Modifier constants */6566/**67* This flag indicates that the Shift key was down when the event68* occurred.69*/70public static final int SHIFT_MASK = 1 << 0;7172/**73* This flag indicates that the Control key was down when the event74* occurred.75*/76public static final int CTRL_MASK = 1 << 1;7778/**79* This flag indicates that the Meta key was down when the event80* occurred. For mouse events, this flag indicates that the right81* button was pressed or released.82*/83public static final int META_MASK = 1 << 2;8485/**86* This flag indicates that the Alt key was down when87* the event occurred. For mouse events, this flag indicates that the88* middle mouse button was pressed or released.89*/90public static final int ALT_MASK = 1 << 3;9192/* Action keys */9394/**95* The Home key, a non-ASCII action key.96*/97public static final int HOME = 1000;9899/**100* The End key, a non-ASCII action key.101*/102public static final int END = 1001;103104/**105* The Page Up key, a non-ASCII action key.106*/107public static final int PGUP = 1002;108109/**110* The Page Down key, a non-ASCII action key.111*/112public static final int PGDN = 1003;113114/**115* The Up Arrow key, a non-ASCII action key.116*/117public static final int UP = 1004;118119/**120* The Down Arrow key, a non-ASCII action key.121*/122public static final int DOWN = 1005;123124/**125* The Left Arrow key, a non-ASCII action key.126*/127public static final int LEFT = 1006;128129/**130* The Right Arrow key, a non-ASCII action key.131*/132public static final int RIGHT = 1007;133134/**135* The F1 function key, a non-ASCII action key.136*/137public static final int F1 = 1008;138139/**140* The F2 function key, a non-ASCII action key.141*/142public static final int F2 = 1009;143144/**145* The F3 function key, a non-ASCII action key.146*/147public static final int F3 = 1010;148149/**150* The F4 function key, a non-ASCII action key.151*/152public static final int F4 = 1011;153154/**155* The F5 function key, a non-ASCII action key.156*/157public static final int F5 = 1012;158159/**160* The F6 function key, a non-ASCII action key.161*/162public static final int F6 = 1013;163164/**165* The F7 function key, a non-ASCII action key.166*/167public static final int F7 = 1014;168169/**170* The F8 function key, a non-ASCII action key.171*/172public static final int F8 = 1015;173174/**175* The F9 function key, a non-ASCII action key.176*/177public static final int F9 = 1016;178179/**180* The F10 function key, a non-ASCII action key.181*/182public static final int F10 = 1017;183184/**185* The F11 function key, a non-ASCII action key.186*/187public static final int F11 = 1018;188189/**190* The F12 function key, a non-ASCII action key.191*/192public static final int F12 = 1019;193194/**195* The Print Screen key, a non-ASCII action key.196*/197public static final int PRINT_SCREEN = 1020;198199/**200* The Scroll Lock key, a non-ASCII action key.201*/202public static final int SCROLL_LOCK = 1021;203204/**205* The Caps Lock key, a non-ASCII action key.206*/207public static final int CAPS_LOCK = 1022;208209/**210* The Num Lock key, a non-ASCII action key.211*/212public static final int NUM_LOCK = 1023;213214/**215* The Pause key, a non-ASCII action key.216*/217public static final int PAUSE = 1024;218219/**220* The Insert key, a non-ASCII action key.221*/222public static final int INSERT = 1025;223224/* Non-action keys */225226/**227* The Enter key.228*/229public static final int ENTER = '\n';230231/**232* The BackSpace key.233*/234public static final int BACK_SPACE = '\b';235236/**237* The Tab key.238*/239public static final int TAB = '\t';240241/**242* The Escape key.243*/244public static final int ESCAPE = 27;245246/**247* The Delete key.248*/249public static final int DELETE = 127;250251252/* Base for all window events. */253private static final int WINDOW_EVENT = 200;254255/**256* The user has asked the window manager to kill the window.257*/258public static final int WINDOW_DESTROY = 1 + WINDOW_EVENT;259260/**261* The user has asked the window manager to expose the window.262*/263public static final int WINDOW_EXPOSE = 2 + WINDOW_EVENT;264265/**266* The user has asked the window manager to iconify the window.267*/268public static final int WINDOW_ICONIFY = 3 + WINDOW_EVENT;269270/**271* The user has asked the window manager to de-iconify the window.272*/273public static final int WINDOW_DEICONIFY = 4 + WINDOW_EVENT;274275/**276* The user has asked the window manager to move the window.277*/278public static final int WINDOW_MOVED = 5 + WINDOW_EVENT;279280/* Base for all keyboard events. */281private static final int KEY_EVENT = 400;282283/**284* The user has pressed a normal key.285*/286public static final int KEY_PRESS = 1 + KEY_EVENT;287288/**289* The user has released a normal key.290*/291public static final int KEY_RELEASE = 2 + KEY_EVENT;292293/**294* The user has pressed a non-ASCII <em>action</em> key.295* The <code>key</code> field contains a value that indicates296* that the event occurred on one of the action keys, which297* comprise the 12 function keys, the arrow (cursor) keys,298* Page Up, Page Down, Home, End, Print Screen, Scroll Lock,299* Caps Lock, Num Lock, Pause, and Insert.300*/301public static final int KEY_ACTION = 3 + KEY_EVENT;302303/**304* The user has released a non-ASCII <em>action</em> key.305* The <code>key</code> field contains a value that indicates306* that the event occurred on one of the action keys, which307* comprise the 12 function keys, the arrow (cursor) keys,308* Page Up, Page Down, Home, End, Print Screen, Scroll Lock,309* Caps Lock, Num Lock, Pause, and Insert.310*/311public static final int KEY_ACTION_RELEASE = 4 + KEY_EVENT;312313/* Base for all mouse events. */314private static final int MOUSE_EVENT = 500;315316/**317* The user has pressed the mouse button. The <code>ALT_MASK</code>318* flag indicates that the middle button has been pressed.319* The <code>META_MASK</code>flag indicates that the320* right button has been pressed.321* @see java.awt.Event#ALT_MASK322* @see java.awt.Event#META_MASK323*/324public static final int MOUSE_DOWN = 1 + MOUSE_EVENT;325326/**327* The user has released the mouse button. The <code>ALT_MASK</code>328* flag indicates that the middle button has been released.329* The <code>META_MASK</code>flag indicates that the330* right button has been released.331* @see java.awt.Event#ALT_MASK332* @see java.awt.Event#META_MASK333*/334public static final int MOUSE_UP = 2 + MOUSE_EVENT;335336/**337* The mouse has moved with no button pressed.338*/339public static final int MOUSE_MOVE = 3 + MOUSE_EVENT;340341/**342* The mouse has entered a component.343*/344public static final int MOUSE_ENTER = 4 + MOUSE_EVENT;345346/**347* The mouse has exited a component.348*/349public static final int MOUSE_EXIT = 5 + MOUSE_EVENT;350351/**352* The user has moved the mouse with a button pressed. The353* <code>ALT_MASK</code> flag indicates that the middle354* button is being pressed. The <code>META_MASK</code> flag indicates355* that the right button is being pressed.356* @see java.awt.Event#ALT_MASK357* @see java.awt.Event#META_MASK358*/359public static final int MOUSE_DRAG = 6 + MOUSE_EVENT;360361362/* Scrolling events */363private static final int SCROLL_EVENT = 600;364365/**366* The user has activated the <em>line up</em>367* area of a scroll bar.368*/369public static final int SCROLL_LINE_UP = 1 + SCROLL_EVENT;370371/**372* The user has activated the <em>line down</em>373* area of a scroll bar.374*/375public static final int SCROLL_LINE_DOWN = 2 + SCROLL_EVENT;376377/**378* The user has activated the <em>page up</em>379* area of a scroll bar.380*/381public static final int SCROLL_PAGE_UP = 3 + SCROLL_EVENT;382383/**384* The user has activated the <em>page down</em>385* area of a scroll bar.386*/387public static final int SCROLL_PAGE_DOWN = 4 + SCROLL_EVENT;388389/**390* The user has moved the bubble (thumb) in a scroll bar,391* moving to an "absolute" position, rather than to392* an offset from the last position.393*/394public static final int SCROLL_ABSOLUTE = 5 + SCROLL_EVENT;395396/**397* The scroll begin event.398*/399public static final int SCROLL_BEGIN = 6 + SCROLL_EVENT;400401/**402* The scroll end event.403*/404public static final int SCROLL_END = 7 + SCROLL_EVENT;405406/* List Events */407private static final int LIST_EVENT = 700;408409/**410* An item in a list has been selected.411*/412public static final int LIST_SELECT = 1 + LIST_EVENT;413414/**415* An item in a list has been deselected.416*/417public static final int LIST_DESELECT = 2 + LIST_EVENT;418419/* Misc Event */420private static final int MISC_EVENT = 1000;421422/**423* This event indicates that the user wants some action to occur.424*/425public static final int ACTION_EVENT = 1 + MISC_EVENT;426427/**428* A file loading event.429*/430public static final int LOAD_FILE = 2 + MISC_EVENT;431432/**433* A file saving event.434*/435public static final int SAVE_FILE = 3 + MISC_EVENT;436437/**438* A component gained the focus.439*/440public static final int GOT_FOCUS = 4 + MISC_EVENT;441442/**443* A component lost the focus.444*/445public static final int LOST_FOCUS = 5 + MISC_EVENT;446447/**448* The target component. This indicates the component over which the449* event occurred or with which the event is associated.450* This object has been replaced by AWTEvent.getSource()451*452* @serial453* @see java.awt.AWTEvent#getSource()454*/455public Object target;456457/**458* The time stamp.459* Replaced by InputEvent.getWhen().460*461* @serial462* @see java.awt.event.InputEvent#getWhen()463*/464public long when;465466/**467* Indicates which type of event the event is, and which468* other <code>Event</code> variables are relevant for the event.469* This has been replaced by AWTEvent.getID()470*471* @serial472* @see java.awt.AWTEvent#getID()473*/474public int id;475476/**477* The <i>x</i> coordinate of the event.478* Replaced by MouseEvent.getX()479*480* @serial481* @see java.awt.event.MouseEvent#getX()482*/483public int x;484485/**486* The <i>y</i> coordinate of the event.487* Replaced by MouseEvent.getY()488*489* @serial490* @see java.awt.event.MouseEvent#getY()491*/492public int y;493494/**495* The key code of the key that was pressed in a keyboard event.496* This has been replaced by KeyEvent.getKeyCode()497*498* @serial499* @see java.awt.event.KeyEvent#getKeyCode()500*/501public int key;502503/**504* The key character that was pressed in a keyboard event.505*/506// public char keyChar;507508/**509* The state of the modifier keys.510* This is replaced with InputEvent.getModifiers()511* In java 1.1 MouseEvent and KeyEvent are subclasses512* of InputEvent.513*514* @serial515* @see java.awt.event.InputEvent#getModifiers()516*/517public int modifiers;518519/**520* For <code>MOUSE_DOWN</code> events, this field indicates the521* number of consecutive clicks. For other events, its value is522* <code>0</code>.523* This field has been replaced by MouseEvent.getClickCount().524*525* @serial526* @see java.awt.event.MouseEvent#getClickCount()527*/528public int clickCount;529530/**531* An arbitrary argument of the event. The value of this field532* depends on the type of event.533* <code>arg</code> has been replaced by event specific property.534*535* @serial536*/537public Object arg;538539/**540* The next event. This field is set when putting events into a541* linked list.542* This has been replaced by EventQueue.543*544* @serial545* @see java.awt.EventQueue546*/547public Event evt;548549/* table for mapping old Event action keys to KeyEvent virtual keys. */550private static final int actionKeyCodes[][] = {551/* virtual key action key */552{ KeyEvent.VK_HOME, Event.HOME },553{ KeyEvent.VK_END, Event.END },554{ KeyEvent.VK_PAGE_UP, Event.PGUP },555{ KeyEvent.VK_PAGE_DOWN, Event.PGDN },556{ KeyEvent.VK_UP, Event.UP },557{ KeyEvent.VK_DOWN, Event.DOWN },558{ KeyEvent.VK_LEFT, Event.LEFT },559{ KeyEvent.VK_RIGHT, Event.RIGHT },560{ KeyEvent.VK_F1, Event.F1 },561{ KeyEvent.VK_F2, Event.F2 },562{ KeyEvent.VK_F3, Event.F3 },563{ KeyEvent.VK_F4, Event.F4 },564{ KeyEvent.VK_F5, Event.F5 },565{ KeyEvent.VK_F6, Event.F6 },566{ KeyEvent.VK_F7, Event.F7 },567{ KeyEvent.VK_F8, Event.F8 },568{ KeyEvent.VK_F9, Event.F9 },569{ KeyEvent.VK_F10, Event.F10 },570{ KeyEvent.VK_F11, Event.F11 },571{ KeyEvent.VK_F12, Event.F12 },572{ KeyEvent.VK_PRINTSCREEN, Event.PRINT_SCREEN },573{ KeyEvent.VK_SCROLL_LOCK, Event.SCROLL_LOCK },574{ KeyEvent.VK_CAPS_LOCK, Event.CAPS_LOCK },575{ KeyEvent.VK_NUM_LOCK, Event.NUM_LOCK },576{ KeyEvent.VK_PAUSE, Event.PAUSE },577{ KeyEvent.VK_INSERT, Event.INSERT }578};579580/**581* This field controls whether or not the event is sent back582* down to the peer once the target has processed it -583* false means it's sent to the peer, true means it's not.584*585* @serial586* @see #isConsumed()587*/588private boolean consumed = false;589590/*591* JDK 1.1 serialVersionUID592*/593private static final long serialVersionUID = 5488922509400504703L;594595static {596/* ensure that the necessary native libraries are loaded */597Toolkit.loadLibraries();598if (!GraphicsEnvironment.isHeadless()) {599initIDs();600}601}602603/**604* Initialize JNI field and method IDs for fields that may be605accessed from C.606*/607private static native void initIDs();608609/**610* <b>NOTE:</b> The <code>Event</code> class is obsolete and is611* available only for backwards compatibility. It has been replaced612* by the <code>AWTEvent</code> class and its subclasses.613* <p>614* Creates an instance of <code>Event</code> with the specified target615* component, time stamp, event type, <i>x</i> and <i>y</i>616* coordinates, keyboard key, state of the modifier keys, and617* argument.618* @param target the target component.619* @param when the time stamp.620* @param id the event type.621* @param x the <i>x</i> coordinate.622* @param y the <i>y</i> coordinate.623* @param key the key pressed in a keyboard event.624* @param modifiers the state of the modifier keys.625* @param arg the specified argument.626*/627public Event(Object target, long when, int id, int x, int y, int key,628int modifiers, Object arg) {629this.target = target;630this.when = when;631this.id = id;632this.x = x;633this.y = y;634this.key = key;635this.modifiers = modifiers;636this.arg = arg;637this.data = 0;638this.clickCount = 0;639switch(id) {640case ACTION_EVENT:641case WINDOW_DESTROY:642case WINDOW_ICONIFY:643case WINDOW_DEICONIFY:644case WINDOW_MOVED:645case SCROLL_LINE_UP:646case SCROLL_LINE_DOWN:647case SCROLL_PAGE_UP:648case SCROLL_PAGE_DOWN:649case SCROLL_ABSOLUTE:650case SCROLL_BEGIN:651case SCROLL_END:652case LIST_SELECT:653case LIST_DESELECT:654consumed = true; // these types are not passed back to peer655break;656default:657}658}659660/**661* <b>NOTE:</b> The <code>Event</code> class is obsolete and is662* available only for backwards compatibility. It has been replaced663* by the <code>AWTEvent</code> class and its subclasses.664* <p>665* Creates an instance of <code>Event</code>, with the specified target666* component, time stamp, event type, <i>x</i> and <i>y</i>667* coordinates, keyboard key, state of the modifier keys, and an668* argument set to <code>null</code>.669* @param target the target component.670* @param when the time stamp.671* @param id the event type.672* @param x the <i>x</i> coordinate.673* @param y the <i>y</i> coordinate.674* @param key the key pressed in a keyboard event.675* @param modifiers the state of the modifier keys.676*/677public Event(Object target, long when, int id, int x, int y, int key, int modifiers) {678this(target, when, id, x, y, key, modifiers, null);679}680681/**682* <b>NOTE:</b> The <code>Event</code> class is obsolete and is683* available only for backwards compatibility. It has been replaced684* by the <code>AWTEvent</code> class and its subclasses.685* <p>686* Creates an instance of <code>Event</code> with the specified687* target component, event type, and argument.688* @param target the target component.689* @param id the event type.690* @param arg the specified argument.691*/692public Event(Object target, int id, Object arg) {693this(target, 0, id, 0, 0, 0, 0, arg);694}695696/**697* <b>NOTE:</b> The <code>Event</code> class is obsolete and is698* available only for backwards compatibility. It has been replaced699* by the <code>AWTEvent</code> class and its subclasses.700* <p>701* Translates this event so that its <i>x</i> and <i>y</i>702* coordinates are increased by <i>dx</i> and <i>dy</i>,703* respectively.704* <p>705* This method translates an event relative to the given component.706* This involves, at a minimum, translating the coordinates into the707* local coordinate system of the given component. It may also involve708* translating a region in the case of an expose event.709* @param dx the distance to translate the <i>x</i> coordinate.710* @param dy the distance to translate the <i>y</i> coordinate.711*/712public void translate(int dx, int dy) {713this.x += dx;714this.y += dy;715}716717/**718* <b>NOTE:</b> The <code>Event</code> class is obsolete and is719* available only for backwards compatibility. It has been replaced720* by the <code>AWTEvent</code> class and its subclasses.721* <p>722* Checks if the Shift key is down.723* @return <code>true</code> if the key is down;724* <code>false</code> otherwise.725* @see java.awt.Event#modifiers726* @see java.awt.Event#controlDown727* @see java.awt.Event#metaDown728*/729public boolean shiftDown() {730return (modifiers & SHIFT_MASK) != 0;731}732733/**734* <b>NOTE:</b> The <code>Event</code> class is obsolete and is735* available only for backwards compatibility. It has been replaced736* by the <code>AWTEvent</code> class and its subclasses.737* <p>738* Checks if the Control key is down.739* @return <code>true</code> if the key is down;740* <code>false</code> otherwise.741* @see java.awt.Event#modifiers742* @see java.awt.Event#shiftDown743* @see java.awt.Event#metaDown744*/745public boolean controlDown() {746return (modifiers & CTRL_MASK) != 0;747}748749/**750* <b>NOTE:</b> The <code>Event</code> class is obsolete and is751* available only for backwards compatibility. It has been replaced752* by the <code>AWTEvent</code> class and its subclasses.753* <p>754* Checks if the Meta key is down.755*756* @return <code>true</code> if the key is down;757* <code>false</code> otherwise.758* @see java.awt.Event#modifiers759* @see java.awt.Event#shiftDown760* @see java.awt.Event#controlDown761*/762public boolean metaDown() {763return (modifiers & META_MASK) != 0;764}765766/**767* <b>NOTE:</b> The <code>Event</code> class is obsolete and is768* available only for backwards compatibility. It has been replaced769* by the <code>AWTEvent</code> class and its subclasses.770*/771void consume() {772switch(id) {773case KEY_PRESS:774case KEY_RELEASE:775case KEY_ACTION:776case KEY_ACTION_RELEASE:777consumed = true;778break;779default:780// event type cannot be consumed781}782}783784/**785* <b>NOTE:</b> The <code>Event</code> class is obsolete and is786* available only for backwards compatibility. It has been replaced787* by the <code>AWTEvent</code> class and its subclasses.788*/789boolean isConsumed() {790return consumed;791}792793/*794* <b>NOTE:</b> The <code>Event</code> class is obsolete and is795* available only for backwards compatibility. It has been replaced796* by the <code>AWTEvent</code> class and its subclasses.797* <p>798* Returns the integer key-code associated with the key in this event,799* as described in java.awt.Event.800*/801static int getOldEventKey(KeyEvent e) {802int keyCode = e.getKeyCode();803for (int i = 0; i < actionKeyCodes.length; i++) {804if (actionKeyCodes[i][0] == keyCode) {805return actionKeyCodes[i][1];806}807}808return (int)e.getKeyChar();809}810811/*812* <b>NOTE:</b> The <code>Event</code> class is obsolete and is813* available only for backwards compatibility. It has been replaced814* by the <code>AWTEvent</code> class and its subclasses.815* <p>816* Returns a new KeyEvent char which corresponds to the int key817* of this old event.818*/819char getKeyEventChar() {820for (int i = 0; i < actionKeyCodes.length; i++) {821if (actionKeyCodes[i][1] == key) {822return KeyEvent.CHAR_UNDEFINED;823}824}825return (char)key;826}827828/**829* <b>NOTE:</b> The <code>Event</code> class is obsolete and is830* available only for backwards compatibility. It has been replaced831* by the <code>AWTEvent</code> class and its subclasses.832* <p>833* Returns a string representing the state of this <code>Event</code>.834* This method is intended to be used only for debugging purposes, and the835* content and format of the returned string may vary between836* implementations. The returned string may be empty but may not be837* <code>null</code>.838*839* @return the parameter string of this event840*/841protected String paramString() {842String str = "id=" + id + ",x=" + x + ",y=" + y;843if (key != 0) {844str += ",key=" + key;845}846if (shiftDown()) {847str += ",shift";848}849if (controlDown()) {850str += ",control";851}852if (metaDown()) {853str += ",meta";854}855if (target != null) {856str += ",target=" + target;857}858if (arg != null) {859str += ",arg=" + arg;860}861return str;862}863864/**865* <b>NOTE:</b> The <code>Event</code> class is obsolete and is866* available only for backwards compatibility. It has been replaced867* by the <code>AWTEvent</code> class and its subclasses.868* <p>869* Returns a representation of this event's values as a string.870* @return a string that represents the event and the values871* of its member fields.872* @see java.awt.Event#paramString873* @since JDK1.1874*/875public String toString() {876return getClass().getName() + "[" + paramString() + "]";877}878}879880881