1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.gef;
|
12
|
|
13
|
import org.eclipse.swt.events.KeyEvent;
|
14
|
|
15
|
/**
|
16
|
* Encapsulates a Keyboard gesture (press or release) from the User. A KeyStroke
|
17
|
* is matched to a KeyEvent based the {@link KeyEvent#stateMask},
|
18
|
* {@link KeyEvent#keyCode} or {@link KeyEvent#character}, and whether that
|
19
|
* KeyEvent was dispatched as a result of a release or press by the User.
|
20
|
*/
|
21
|
public class KeyStroke {
|
22
|
|
23
|
/**
|
24
|
* Constructs a KeyStroke that will match the given
|
25
|
* {@link KeyEvent#character} and {@link KeyEvent#stateMask} during a press
|
26
|
* event.
|
27
|
*
|
28
|
* @param character
|
29
|
* the character to match
|
30
|
* @param stateMask
|
31
|
* the stateMask to match
|
32
|
* @return a new KeyStroke
|
33
|
*/
|
34
|
public static KeyStroke getPressed(char character, int stateMask) {
|
35
|
return new KeyStroke(character, stateMask, true);
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Constructs a KeyStroke that will match the given {@link KeyEvent#keyCode}
|
40
|
* and {@link KeyEvent#stateMask} during a press event.
|
41
|
*
|
42
|
* @param keyCode
|
43
|
* the keyCode to match
|
44
|
* @param stateMask
|
45
|
* the stateMask to match
|
46
|
* @return a new KeyStroke
|
47
|
*/
|
48
|
public static KeyStroke getPressed(int keyCode, int stateMask) {
|
49
|
return new KeyStroke(keyCode, stateMask, true);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Constructs a KeyStroke that will match the given
|
54
|
* {@link KeyEvent#character}, {@link KeyEvent#keyCode}, and
|
55
|
* {@link KeyEvent#stateMask} during a press event.
|
56
|
*
|
57
|
* @param character
|
58
|
* the character to match
|
59
|
* @param keyCode
|
60
|
* the keyCode to match
|
61
|
* @param stateMask
|
62
|
* the stateMask to match
|
63
|
* @return a new KeyStroke
|
64
|
*/
|
65
|
public static KeyStroke getPressed(char character, int keyCode,
|
66
|
int stateMask) {
|
67
|
return new KeyStroke(character, keyCode, stateMask, true);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Constructs a KeyStroke that will match the given
|
72
|
* {@link KeyEvent#character} and {@link KeyEvent#stateMask} during a
|
73
|
* release event.
|
74
|
*
|
75
|
* @param character
|
76
|
* the character to match
|
77
|
* @param stateMask
|
78
|
* the stateMask to match
|
79
|
* @return a new KeyStroke
|
80
|
*/
|
81
|
public static KeyStroke getReleased(char character, int stateMask) {
|
82
|
return new KeyStroke(character, stateMask, false);
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Constructs a KeyStroke that will match the given {@link KeyEvent#keyCode}
|
87
|
* and {@link KeyEvent#stateMask} during a release event.
|
88
|
*
|
89
|
* @param keyCode
|
90
|
* the keyCode to match
|
91
|
* @param stateMask
|
92
|
* the stateMask to match
|
93
|
* @return a new KeyStroke
|
94
|
*/
|
95
|
public static KeyStroke getReleased(int keyCode, int stateMask) {
|
96
|
return new KeyStroke(keyCode, stateMask, false);
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Constructs a KeyStroke that will match the given
|
101
|
* {@link KeyEvent#character}, {@link KeyEvent#keyCode}, and
|
102
|
* {@link KeyEvent#stateMask} during a release event.
|
103
|
*
|
104
|
* @param character
|
105
|
* the character to match
|
106
|
* @param keyCode
|
107
|
* the keyCode to match
|
108
|
* @param stateMask
|
109
|
* the stateMask to match
|
110
|
* @return a new KeyStroke
|
111
|
*/
|
112
|
public static KeyStroke getReleased(char character, int keyCode,
|
113
|
int stateMask) {
|
114
|
return new KeyStroke(character, keyCode, stateMask, false);
|
115
|
}
|
116
|
|
117
|
private int stateMask;
|
118
|
private char character;
|
119
|
private boolean onPressed;
|
120
|
private int keyCode;
|
121
|
|
122
|
/**
|
123
|
* Creates a KeyStroke for the specified KeyEvent and pressed value.
|
124
|
*
|
125
|
* @param event
|
126
|
* The KeyEvent
|
127
|
* @param pressed
|
128
|
* true if the KeyStroke is for a press event
|
129
|
*/
|
130
|
KeyStroke(KeyEvent event, boolean pressed) {
|
131
|
onPressed = pressed;
|
132
|
stateMask = event.stateMask;
|
133
|
character = event.character;
|
134
|
keyCode = event.keyCode;
|
135
|
}
|
136
|
|
137
|
KeyStroke(int keyCode, int stateMask, boolean onPressed) {
|
138
|
this.keyCode = keyCode;
|
139
|
this.stateMask = stateMask;
|
140
|
this.onPressed = onPressed;
|
141
|
}
|
142
|
|
143
|
KeyStroke(char character, int stateMask, boolean onPressed) {
|
144
|
this.character = character;
|
145
|
this.stateMask = stateMask;
|
146
|
this.onPressed = onPressed;
|
147
|
}
|
148
|
|
149
|
KeyStroke(char character, int keyCode, int stateMask, boolean onPressed) {
|
150
|
this.character = character;
|
151
|
this.keyCode = keyCode;
|
152
|
this.stateMask = stateMask;
|
153
|
this.onPressed = onPressed;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* @return true iff the Object is an equivalent KeyStroke
|
158
|
* @param obj
|
159
|
* the Object being compared
|
160
|
*/
|
161
|
public boolean equals(Object obj) {
|
162
|
if (obj instanceof KeyStroke) {
|
163
|
KeyStroke stroke = (KeyStroke) obj;
|
164
|
return stroke.character == character && stroke.keyCode == keyCode
|
165
|
&& stroke.onPressed == onPressed
|
166
|
&& stroke.stateMask == stateMask;
|
167
|
}
|
168
|
return false;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* @see java.lang.Object#hashCode()
|
173
|
*/
|
174
|
public int hashCode() {
|
175
|
return (stateMask + 1) * ((character ^ keyCode) + 1) // One of these is
|
176
|
// always Zero.
|
177
|
+ (onPressed ? 0 : 32);
|
178
|
}
|
179
|
|
180
|
}
|