Path: blob/master/src/java.desktop/share/classes/java/awt/Event.java
41152 views
/*1* Copyright (c) 1995, 2021, 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*/2425package java.awt;2627import java.awt.event.KeyEvent;28import java.io.Serial;2930/**31* <b>NOTE:</b> The {@code Event} class is obsolete and is32* available only for backwards compatibility. It has been replaced33* by the {@code AWTEvent} class and its subclasses.34* <p>35* {@code Event} is a platform-independent class that36* encapsulates events from the platform's Graphical User37* Interface in the Java 1.0 event model. In Java 1.138* and later versions, the {@code Event} class is maintained39* only for backwards compatibility. The information in this40* class description is provided to assist programmers in41* converting Java 1.0 programs to the new event model.42* <p>43* In the Java 1.0 event model, an event contains an44* {@link Event#id} field45* that indicates what type of event it is and which other46* {@code Event} variables are relevant for the event.47* <p>48* For keyboard events, {@link Event#key}49* contains a value indicating which key was activated, and50* {@link Event#modifiers} contains the51* modifiers for that event. For the KEY_PRESS and KEY_RELEASE52* event ids, the value of {@code key} is the unicode53* character code for the key. For KEY_ACTION and54* KEY_ACTION_RELEASE, the value of {@code key} is55* one of the defined action-key identifiers in the56* {@code Event} class ({@code PGUP},57* {@code PGDN}, {@code F1}, {@code F2}, etc).58*59* @deprecated It is recommended that {@code AWTEvent} and its subclasses be60* used instead61* @author Sami Shaio62* @since 1.063*/64@Deprecated(since = "9")65public class Event implements java.io.Serializable {66private transient long data;6768/* Modifier constants */6970/**71* This flag indicates that the Shift key was down when the event72* occurred.73*/74public static final int SHIFT_MASK = 1 << 0;7576/**77* This flag indicates that the Control key was down when the event78* occurred.79*/80public static final int CTRL_MASK = 1 << 1;8182/**83* This flag indicates that the Meta key was down when the event84* occurred. For mouse events, this flag indicates that the right85* button was pressed or released.86*/87public static final int META_MASK = 1 << 2;8889/**90* This flag indicates that the Alt key was down when91* the event occurred. For mouse events, this flag indicates that the92* middle mouse button was pressed or released.93*/94public static final int ALT_MASK = 1 << 3;9596/* Action keys */9798/**99* The Home key, a non-ASCII action key.100*/101public static final int HOME = 1000;102103/**104* The End key, a non-ASCII action key.105*/106public static final int END = 1001;107108/**109* The Page Up key, a non-ASCII action key.110*/111public static final int PGUP = 1002;112113/**114* The Page Down key, a non-ASCII action key.115*/116public static final int PGDN = 1003;117118/**119* The Up Arrow key, a non-ASCII action key.120*/121public static final int UP = 1004;122123/**124* The Down Arrow key, a non-ASCII action key.125*/126public static final int DOWN = 1005;127128/**129* The Left Arrow key, a non-ASCII action key.130*/131public static final int LEFT = 1006;132133/**134* The Right Arrow key, a non-ASCII action key.135*/136public static final int RIGHT = 1007;137138/**139* The F1 function key, a non-ASCII action key.140*/141public static final int F1 = 1008;142143/**144* The F2 function key, a non-ASCII action key.145*/146public static final int F2 = 1009;147148/**149* The F3 function key, a non-ASCII action key.150*/151public static final int F3 = 1010;152153/**154* The F4 function key, a non-ASCII action key.155*/156public static final int F4 = 1011;157158/**159* The F5 function key, a non-ASCII action key.160*/161public static final int F5 = 1012;162163/**164* The F6 function key, a non-ASCII action key.165*/166public static final int F6 = 1013;167168/**169* The F7 function key, a non-ASCII action key.170*/171public static final int F7 = 1014;172173/**174* The F8 function key, a non-ASCII action key.175*/176public static final int F8 = 1015;177178/**179* The F9 function key, a non-ASCII action key.180*/181public static final int F9 = 1016;182183/**184* The F10 function key, a non-ASCII action key.185*/186public static final int F10 = 1017;187188/**189* The F11 function key, a non-ASCII action key.190*/191public static final int F11 = 1018;192193/**194* The F12 function key, a non-ASCII action key.195*/196public static final int F12 = 1019;197198/**199* The Print Screen key, a non-ASCII action key.200*/201public static final int PRINT_SCREEN = 1020;202203/**204* The Scroll Lock key, a non-ASCII action key.205*/206public static final int SCROLL_LOCK = 1021;207208/**209* The Caps Lock key, a non-ASCII action key.210*/211public static final int CAPS_LOCK = 1022;212213/**214* The Num Lock key, a non-ASCII action key.215*/216public static final int NUM_LOCK = 1023;217218/**219* The Pause key, a non-ASCII action key.220*/221public static final int PAUSE = 1024;222223/**224* The Insert key, a non-ASCII action key.225*/226public static final int INSERT = 1025;227228/* Non-action keys */229230/**231* The Enter key.232*/233public static final int ENTER = '\n';234235/**236* The BackSpace key.237*/238public static final int BACK_SPACE = '\b';239240/**241* The Tab key.242*/243public static final int TAB = '\t';244245/**246* The Escape key.247*/248public static final int ESCAPE = 27;249250/**251* The Delete key.252*/253public static final int DELETE = 127;254255256/* Base for all window events. */257private static final int WINDOW_EVENT = 200;258259/**260* The user has asked the window manager to kill the window.261*/262public static final int WINDOW_DESTROY = 1 + WINDOW_EVENT;263264/**265* The user has asked the window manager to expose the window.266*/267public static final int WINDOW_EXPOSE = 2 + WINDOW_EVENT;268269/**270* The user has asked the window manager to iconify the window.271*/272public static final int WINDOW_ICONIFY = 3 + WINDOW_EVENT;273274/**275* The user has asked the window manager to de-iconify the window.276*/277public static final int WINDOW_DEICONIFY = 4 + WINDOW_EVENT;278279/**280* The user has asked the window manager to move the window.281*/282public static final int WINDOW_MOVED = 5 + WINDOW_EVENT;283284/* Base for all keyboard events. */285private static final int KEY_EVENT = 400;286287/**288* The user has pressed a normal key.289*/290public static final int KEY_PRESS = 1 + KEY_EVENT;291292/**293* The user has released a normal key.294*/295public static final int KEY_RELEASE = 2 + KEY_EVENT;296297/**298* The user has pressed a non-ASCII <em>action</em> key.299* The {@code key} field contains a value that indicates300* that the event occurred on one of the action keys, which301* comprise the 12 function keys, the arrow (cursor) keys,302* Page Up, Page Down, Home, End, Print Screen, Scroll Lock,303* Caps Lock, Num Lock, Pause, and Insert.304*/305public static final int KEY_ACTION = 3 + KEY_EVENT;306307/**308* The user has released a non-ASCII <em>action</em> key.309* The {@code key} field contains a value that indicates310* that the event occurred on one of the action keys, which311* comprise the 12 function keys, the arrow (cursor) keys,312* Page Up, Page Down, Home, End, Print Screen, Scroll Lock,313* Caps Lock, Num Lock, Pause, and Insert.314*/315public static final int KEY_ACTION_RELEASE = 4 + KEY_EVENT;316317/* Base for all mouse events. */318private static final int MOUSE_EVENT = 500;319320/**321* The user has pressed the mouse button. The {@code ALT_MASK}322* flag indicates that the middle button has been pressed.323* The {@code META_MASK} flag indicates that the324* right button has been pressed.325* @see java.awt.Event#ALT_MASK326* @see java.awt.Event#META_MASK327*/328public static final int MOUSE_DOWN = 1 + MOUSE_EVENT;329330/**331* The user has released the mouse button. The {@code ALT_MASK}332* flag indicates that the middle button has been released.333* The {@code META_MASK} flag indicates that the334* right button has been released.335* @see java.awt.Event#ALT_MASK336* @see java.awt.Event#META_MASK337*/338public static final int MOUSE_UP = 2 + MOUSE_EVENT;339340/**341* The mouse has moved with no button pressed.342*/343public static final int MOUSE_MOVE = 3 + MOUSE_EVENT;344345/**346* The mouse has entered a component.347*/348public static final int MOUSE_ENTER = 4 + MOUSE_EVENT;349350/**351* The mouse has exited a component.352*/353public static final int MOUSE_EXIT = 5 + MOUSE_EVENT;354355/**356* The user has moved the mouse with a button pressed. The357* {@code ALT_MASK} flag indicates that the middle358* button is being pressed. The {@code META_MASK} flag indicates359* that the right button is being pressed.360* @see java.awt.Event#ALT_MASK361* @see java.awt.Event#META_MASK362*/363public static final int MOUSE_DRAG = 6 + MOUSE_EVENT;364365366/* Scrolling events */367private static final int SCROLL_EVENT = 600;368369/**370* The user has activated the <em>line up</em>371* area of a scroll bar.372*/373public static final int SCROLL_LINE_UP = 1 + SCROLL_EVENT;374375/**376* The user has activated the <em>line down</em>377* area of a scroll bar.378*/379public static final int SCROLL_LINE_DOWN = 2 + SCROLL_EVENT;380381/**382* The user has activated the <em>page up</em>383* area of a scroll bar.384*/385public static final int SCROLL_PAGE_UP = 3 + SCROLL_EVENT;386387/**388* The user has activated the <em>page down</em>389* area of a scroll bar.390*/391public static final int SCROLL_PAGE_DOWN = 4 + SCROLL_EVENT;392393/**394* The user has moved the bubble (thumb) in a scroll bar,395* moving to an "absolute" position, rather than to396* an offset from the last position.397*/398public static final int SCROLL_ABSOLUTE = 5 + SCROLL_EVENT;399400/**401* The scroll begin event.402*/403public static final int SCROLL_BEGIN = 6 + SCROLL_EVENT;404405/**406* The scroll end event.407*/408public static final int SCROLL_END = 7 + SCROLL_EVENT;409410/* List Events */411private static final int LIST_EVENT = 700;412413/**414* An item in a list has been selected.415*/416public static final int LIST_SELECT = 1 + LIST_EVENT;417418/**419* An item in a list has been deselected.420*/421public static final int LIST_DESELECT = 2 + LIST_EVENT;422423/* Misc Event */424private static final int MISC_EVENT = 1000;425426/**427* This event indicates that the user wants some action to occur.428*/429public static final int ACTION_EVENT = 1 + MISC_EVENT;430431/**432* A file loading event.433*/434public static final int LOAD_FILE = 2 + MISC_EVENT;435436/**437* A file saving event.438*/439public static final int SAVE_FILE = 3 + MISC_EVENT;440441/**442* A component gained the focus.443*/444public static final int GOT_FOCUS = 4 + MISC_EVENT;445446/**447* A component lost the focus.448*/449public static final int LOST_FOCUS = 5 + MISC_EVENT;450451/**452* The target component. This indicates the component over which the453* event occurred or with which the event is associated.454* This object has been replaced by AWTEvent.getSource()455*456* @serial457* @see java.awt.AWTEvent#getSource()458*/459@SuppressWarnings("serial") // Not statically typed as Serializable460public Object target;461462/**463* The time stamp.464* Replaced by InputEvent.getWhen().465*466* @serial467* @see java.awt.event.InputEvent#getWhen()468*/469public long when;470471/**472* Indicates which type of event the event is, and which473* other {@code Event} variables are relevant for the event.474* This has been replaced by AWTEvent.getID()475*476* @serial477* @see java.awt.AWTEvent#getID()478*/479public int id;480481/**482* The <i>x</i> coordinate of the event.483* Replaced by MouseEvent.getX()484*485* @serial486* @see java.awt.event.MouseEvent#getX()487*/488public int x;489490/**491* The <i>y</i> coordinate of the event.492* Replaced by MouseEvent.getY()493*494* @serial495* @see java.awt.event.MouseEvent#getY()496*/497public int y;498499/**500* The key code of the key that was pressed in a keyboard event.501* This has been replaced by KeyEvent.getKeyCode()502*503* @serial504* @see java.awt.event.KeyEvent#getKeyCode()505*/506public int key;507508/**509* The key character that was pressed in a keyboard event.510*/511// public char keyChar;512513/**514* The state of the modifier keys.515* This is replaced with InputEvent.getModifiers()516* In java 1.1 MouseEvent and KeyEvent are subclasses517* of InputEvent.518*519* @serial520* @see java.awt.event.InputEvent#getModifiers()521*/522public int modifiers;523524/**525* For {@code MOUSE_DOWN} events, this field indicates the526* number of consecutive clicks. For other events, its value is527* {@code 0}.528* This field has been replaced by MouseEvent.getClickCount().529*530* @serial531* @see java.awt.event.MouseEvent#getClickCount()532*/533public int clickCount;534535/**536* An arbitrary argument of the event. The value of this field537* depends on the type of event.538* {@code arg} has been replaced by event specific property.539*540* @serial541*/542@SuppressWarnings("serial") // Not statically typed as Serializable543public Object arg;544545/**546* The next event. This field is set when putting events into a547* linked list.548* This has been replaced by EventQueue.549*550* @serial551* @see java.awt.EventQueue552*/553public Event evt;554555/* table for mapping old Event action keys to KeyEvent virtual keys. */556private static final int[][] actionKeyCodes = {557/* virtual key action key */558{ KeyEvent.VK_HOME, Event.HOME },559{ KeyEvent.VK_END, Event.END },560{ KeyEvent.VK_PAGE_UP, Event.PGUP },561{ KeyEvent.VK_PAGE_DOWN, Event.PGDN },562{ KeyEvent.VK_UP, Event.UP },563{ KeyEvent.VK_DOWN, Event.DOWN },564{ KeyEvent.VK_LEFT, Event.LEFT },565{ KeyEvent.VK_RIGHT, Event.RIGHT },566{ KeyEvent.VK_F1, Event.F1 },567{ KeyEvent.VK_F2, Event.F2 },568{ KeyEvent.VK_F3, Event.F3 },569{ KeyEvent.VK_F4, Event.F4 },570{ KeyEvent.VK_F5, Event.F5 },571{ KeyEvent.VK_F6, Event.F6 },572{ KeyEvent.VK_F7, Event.F7 },573{ KeyEvent.VK_F8, Event.F8 },574{ KeyEvent.VK_F9, Event.F9 },575{ KeyEvent.VK_F10, Event.F10 },576{ KeyEvent.VK_F11, Event.F11 },577{ KeyEvent.VK_F12, Event.F12 },578{ KeyEvent.VK_PRINTSCREEN, Event.PRINT_SCREEN },579{ KeyEvent.VK_SCROLL_LOCK, Event.SCROLL_LOCK },580{ KeyEvent.VK_CAPS_LOCK, Event.CAPS_LOCK },581{ KeyEvent.VK_NUM_LOCK, Event.NUM_LOCK },582{ KeyEvent.VK_PAUSE, Event.PAUSE },583{ KeyEvent.VK_INSERT, Event.INSERT }584};585586/**587* This field controls whether or not the event is sent back588* down to the peer once the target has processed it -589* false means it's sent to the peer, true means it's not.590*591* @serial592* @see #isConsumed()593*/594private boolean consumed = false;595596/**597* Use serialVersionUID from JDK 1.1 for interoperability.598*/599@Serial600private static final long serialVersionUID = 5488922509400504703L;601602static {603/* ensure that the necessary native libraries are loaded */604Toolkit.loadLibraries();605if (!GraphicsEnvironment.isHeadless()) {606initIDs();607}608}609610/**611* Initialize JNI field and method IDs for fields that may be612accessed from C.613*/614private static native void initIDs();615616/**617* <b>NOTE:</b> The {@code Event} class is obsolete and is618* available only for backwards compatibility. It has been replaced619* by the {@code AWTEvent} class and its subclasses.620* <p>621* Creates an instance of {@code Event} with the specified target622* component, time stamp, event type, <i>x</i> and <i>y</i>623* coordinates, keyboard key, state of the modifier keys, and624* argument.625* @param target the target component.626* @param when the time stamp.627* @param id the event type.628* @param x the <i>x</i> coordinate.629* @param y the <i>y</i> coordinate.630* @param key the key pressed in a keyboard event.631* @param modifiers the state of the modifier keys.632* @param arg the specified argument.633*/634public Event(Object target, long when, int id, int x, int y, int key,635int modifiers, Object arg) {636this.target = target;637this.when = when;638this.id = id;639this.x = x;640this.y = y;641this.key = key;642this.modifiers = modifiers;643this.arg = arg;644this.data = 0;645this.clickCount = 0;646switch(id) {647case ACTION_EVENT:648case WINDOW_DESTROY:649case WINDOW_ICONIFY:650case WINDOW_DEICONIFY:651case WINDOW_MOVED:652case SCROLL_LINE_UP:653case SCROLL_LINE_DOWN:654case SCROLL_PAGE_UP:655case SCROLL_PAGE_DOWN:656case SCROLL_ABSOLUTE:657case SCROLL_BEGIN:658case SCROLL_END:659case LIST_SELECT:660case LIST_DESELECT:661consumed = true; // these types are not passed back to peer662break;663default:664}665}666667/**668* <b>NOTE:</b> The {@code Event} class is obsolete and is669* available only for backwards compatibility. It has been replaced670* by the {@code AWTEvent} class and its subclasses.671* <p>672* Creates an instance of {@code Event}, with the specified target673* component, time stamp, event type, <i>x</i> and <i>y</i>674* coordinates, keyboard key, state of the modifier keys, and an675* argument set to {@code null}.676* @param target the target component.677* @param when the time stamp.678* @param id the event type.679* @param x the <i>x</i> coordinate.680* @param y the <i>y</i> coordinate.681* @param key the key pressed in a keyboard event.682* @param modifiers the state of the modifier keys.683*/684public Event(Object target, long when, int id, int x, int y, int key, int modifiers) {685this(target, when, id, x, y, key, modifiers, null);686}687688/**689* <b>NOTE:</b> The {@code Event} class is obsolete and is690* available only for backwards compatibility. It has been replaced691* by the {@code AWTEvent} class and its subclasses.692* <p>693* Creates an instance of {@code Event} with the specified694* target component, event type, and argument.695* @param target the target component.696* @param id the event type.697* @param arg the specified argument.698*/699public Event(Object target, int id, Object arg) {700this(target, 0, id, 0, 0, 0, 0, arg);701}702703/**704* <b>NOTE:</b> The {@code Event} class is obsolete and is705* available only for backwards compatibility. It has been replaced706* by the {@code AWTEvent} class and its subclasses.707* <p>708* Translates this event so that its <i>x</i> and <i>y</i>709* coordinates are increased by <i>dx</i> and <i>dy</i>,710* respectively.711* <p>712* This method translates an event relative to the given component.713* This involves, at a minimum, translating the coordinates into the714* local coordinate system of the given component. It may also involve715* translating a region in the case of an expose event.716* @param dx the distance to translate the <i>x</i> coordinate.717* @param dy the distance to translate the <i>y</i> coordinate.718*/719public void translate(int dx, int dy) {720this.x += dx;721this.y += dy;722}723724/**725* <b>NOTE:</b> The {@code Event} class is obsolete and is726* available only for backwards compatibility. It has been replaced727* by the {@code AWTEvent} class and its subclasses.728* <p>729* Checks if the Shift key is down.730* @return {@code true} if the key is down;731* {@code false} otherwise.732* @see java.awt.Event#modifiers733* @see java.awt.Event#controlDown734* @see java.awt.Event#metaDown735*/736public boolean shiftDown() {737return (modifiers & SHIFT_MASK) != 0;738}739740/**741* <b>NOTE:</b> The {@code Event} class is obsolete and is742* available only for backwards compatibility. It has been replaced743* by the {@code AWTEvent} class and its subclasses.744* <p>745* Checks if the Control key is down.746* @return {@code true} if the key is down;747* {@code false} otherwise.748* @see java.awt.Event#modifiers749* @see java.awt.Event#shiftDown750* @see java.awt.Event#metaDown751*/752public boolean controlDown() {753return (modifiers & CTRL_MASK) != 0;754}755756/**757* <b>NOTE:</b> The {@code Event} class is obsolete and is758* available only for backwards compatibility. It has been replaced759* by the {@code AWTEvent} class and its subclasses.760* <p>761* Checks if the Meta key is down.762*763* @return {@code true} if the key is down;764* {@code false} otherwise.765* @see java.awt.Event#modifiers766* @see java.awt.Event#shiftDown767* @see java.awt.Event#controlDown768*/769public boolean metaDown() {770return (modifiers & META_MASK) != 0;771}772773/**774* <b>NOTE:</b> The {@code Event} class is obsolete and is775* available only for backwards compatibility. It has been replaced776* by the {@code AWTEvent} class and its subclasses.777*/778void consume() {779switch(id) {780case KEY_PRESS:781case KEY_RELEASE:782case KEY_ACTION:783case KEY_ACTION_RELEASE:784consumed = true;785break;786default:787// event type cannot be consumed788}789}790791/**792* <b>NOTE:</b> The {@code Event} class is obsolete and is793* available only for backwards compatibility. It has been replaced794* by the {@code AWTEvent} class and its subclasses.795*/796boolean isConsumed() {797return consumed;798}799800/*801* <b>NOTE:</b> The {@code Event} class is obsolete and is802* available only for backwards compatibility. It has been replaced803* by the {@code AWTEvent} class and its subclasses.804* <p>805* Returns the integer key-code associated with the key in this event,806* as described in java.awt.Event.807*/808static int getOldEventKey(KeyEvent e) {809int keyCode = e.getKeyCode();810for (int i = 0; i < actionKeyCodes.length; i++) {811if (actionKeyCodes[i][0] == keyCode) {812return actionKeyCodes[i][1];813}814}815return (int)e.getKeyChar();816}817818/*819* <b>NOTE:</b> The {@code Event} class is obsolete and is820* available only for backwards compatibility. It has been replaced821* by the {@code AWTEvent} class and its subclasses.822* <p>823* Returns a new KeyEvent char which corresponds to the int key824* of this old event.825*/826char getKeyEventChar() {827for (int i = 0; i < actionKeyCodes.length; i++) {828if (actionKeyCodes[i][1] == key) {829return KeyEvent.CHAR_UNDEFINED;830}831}832return (char)key;833}834835/**836* <b>NOTE:</b> The {@code Event} class is obsolete and is837* available only for backwards compatibility. It has been replaced838* by the {@code AWTEvent} class and its subclasses.839* <p>840* Returns a string representing the state of this {@code Event}.841* This method is intended to be used only for debugging purposes, and the842* content and format of the returned string may vary between843* implementations. The returned string may be empty but may not be844* {@code null}.845*846* @return the parameter string of this event847*/848protected String paramString() {849String str = "id=" + id + ",x=" + x + ",y=" + y;850if (key != 0) {851str += ",key=" + key;852}853if (shiftDown()) {854str += ",shift";855}856if (controlDown()) {857str += ",control";858}859if (metaDown()) {860str += ",meta";861}862if (target != null) {863str += ",target=" + target;864}865if (arg != null) {866str += ",arg=" + arg;867}868return str;869}870871/**872* <b>NOTE:</b> The {@code Event} class is obsolete and is873* available only for backwards compatibility. It has been replaced874* by the {@code AWTEvent} class and its subclasses.875* <p>876* Returns a representation of this event's values as a string.877* @return a string that represents the event and the values878* of its member fields.879* @see java.awt.Event#paramString880* @since 1.1881*/882public String toString() {883return getClass().getName() + "[" + paramString() + "]";884}885}886887888