1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.events;
17 import org.eclipse.swt.widgets.Event;
20 * Instances of this class are sent as a result of
21 * keys being pressed and released on the keyboard.
23 * When a key listener is added to a control, the control
24 * will take part in widget traversal. By default, all
25 * traversal keys (such as the tab key and so on) are
26 * delivered to the control. In order for a control to take
27 * part in traversal, it should listen for traversal events.
28 * Otherwise, the user can traverse into a control but not
29 * out. Note that native controls such as table and tree
30 * implement key traversal in the operating system. It is
31 * not necessary to add traversal listeners for these controls,
32 * unless you want to override the default traversal.
36 * @see TraverseListener
37 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
40 public class KeyEvent extends TypedEvent {
43 * the character represented by the key that was typed.
44 * This is the final character that results after all modifiers have been
45 * applied. For example, when the user types Ctrl+A, the character value
46 * is 0x01. It is important that applications do not attempt to modify the
47 * character value based on a stateMask (such as SWT.CTRL) or the resulting
48 * character will not be correct.
50 public char character;
53 * the key code of the key that was typed,
54 * as defined by the key code constants in class <code>SWT</code>.
55 * When the character field of the event is ambiguous, this field
56 * contains the unicode value of the original character. For example,
57 * typing Ctrl+M or Return both result in the character '\r' but the
58 * keyCode field will also contain '\r' when Return was typed.
60 * @see org.eclipse.swt.SWT
65 * depending on the event, the location of key specified by the
66 * keyCode or character. The possible values for this field are
67 * <code>SWT.LEFT</code>, <code>SWT.RIGHT</code>, <code>SWT.KEYPAD</code>,
68 * or <code>SWT.NONE</code> representing the main keyboard area.
70 * The location field can be used to differentiate key events that have
71 * the same key code and character but are generated by different keys
72 * on the keyboard. For example, a key down event with the key code equal
73 * to SWT.SHIFT can be generated by the left and the right shift keys on
76 * The location field can only be used to determine the location of
77 * the key code or character in the current event. It does not include
78 * information about the location of modifiers in the state mask.
81 * @see org.eclipse.swt.SWT#LEFT
82 * @see org.eclipse.swt.SWT#RIGHT
83 * @see org.eclipse.swt.SWT#KEYPAD
87 public int keyLocation;
90 * the state of the keyboard modifier keys and mouse masks
91 * at the time the event was generated.
93 * @see org.eclipse.swt.SWT#MODIFIER_MASK
94 * @see org.eclipse.swt.SWT#BUTTON_MASK
99 * A flag indicating whether the operation should be allowed.
100 * Setting this field to <code>false</code> will cancel the operation.
104 static final long serialVersionUID = 3256442491011412789L;
107 * Constructs a new instance of this class based on the
108 * information in the given untyped event.
110 * @param e the untyped event containing the information
112 public KeyEvent(Event e) {
114 this.character = e.character;
115 this.keyCode = e.keyCode;
116 this.keyLocation = e.keyLocation;
117 this.stateMask = e.stateMask;
122 * Returns a string containing a concise, human-readable
123 * description of the receiver.
125 * @return a string representation of the event
128 public String toString() {
129 String string = super.toString ();
130 return string.substring (0, string.length() - 1) // remove trailing '}'
131 + " character='" + ((character == 0) ? "\\0" : String.valueOf(character)) + "'=0x" + Integer.toHexString(character)
132 + " keyCode=0x" + Integer.toHexString(keyCode)
133 + " keyLocation=0x" + Integer.toHexString(keyLocation)
134 + " stateMask=0x" + Integer.toHexString(stateMask)