下面列出了怎么用java.awt.event.InputEvent的API类实例代码及写法,或者点击链接到github查看源代码。
private static int mapNewModifiers(int modifiers) {
if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
modifiers |= InputEvent.SHIFT_MASK;
}
if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
modifiers |= InputEvent.ALT_MASK;
}
if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
modifiers |= InputEvent.ALT_GRAPH_MASK;
}
if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
modifiers |= InputEvent.CTRL_MASK;
}
if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
modifiers |= InputEvent.META_MASK;
}
return modifiers;
}
/**
* Converts XEMBED modifiers mask into AWT InputEvent mask
*/
int getModifiers(int state) {
int mods = 0;
if ((state & XEMBED_MODIFIER_SHIFT) != 0) {
mods |= InputEvent.SHIFT_DOWN_MASK;
}
if ((state & XEMBED_MODIFIER_CONTROL) != 0) {
mods |= InputEvent.CTRL_DOWN_MASK;
}
if ((state & XEMBED_MODIFIER_ALT) != 0) {
mods |= InputEvent.ALT_DOWN_MASK;
}
// FIXME: What is super/hyper?
// FIXME: Experiments show that SUPER is ALT. So what is Alt then?
if ((state & XEMBED_MODIFIER_SUPER) != 0) {
mods |= InputEvent.ALT_DOWN_MASK;
}
// if ((state & XEMBED_MODIFIER_HYPER) != 0) {
// mods |= InputEvent.DOWN_MASK;
// }
return mods;
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
new RemovedComponentMouseListener();
});
Robot r = Util.createRobot();
r.setAutoDelay(100);
r.waitForIdle();
Util.pointOnComp(button, r);
r.waitForIdle();
r.mousePress(InputEvent.BUTTON1_MASK);
r.waitForIdle();
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.waitForIdle();
if (!mouseReleasedReceived) {
throw new RuntimeException("mouseReleased event was not received");
}
}
public void testBeansCompletionOpenBraces() {
startTest();
EditorOperator eo = new EditorOperator("index.jsp");
eo.setCaretPositionToLine(18);
type(eo, "${");
eo.typeKey(' ', InputEvent.CTRL_MASK);
evt.waitNoEvent(1000);
CompletionInfo completion = getCompletion();
CompletionJListOperator cjo = completion.listItself;
checkCompletionItems(cjo, new String[]{"simplebean", "innerBean", "header"});
eo.pressKey(KeyEvent.VK_ESCAPE);
type(eo, "s");
eo.typeKey(' ', InputEvent.CTRL_MASK);
evt.waitNoEvent(1000);
completion = getCompletion();
cjo = completion.listItself;
checkCompletionItems(cjo, new String[]{"simplebean", "Short"});
this.clearLine(eo);
endTest();
}
/**
* Converts XEMBED modifiers mask into AWT InputEvent mask
*/
int getModifiers(int state) {
int mods = 0;
if ((state & XEMBED_MODIFIER_SHIFT) != 0) {
mods |= InputEvent.SHIFT_DOWN_MASK;
}
if ((state & XEMBED_MODIFIER_CONTROL) != 0) {
mods |= InputEvent.CTRL_DOWN_MASK;
}
if ((state & XEMBED_MODIFIER_ALT) != 0) {
mods |= InputEvent.ALT_DOWN_MASK;
}
// FIXME: What is super/hyper?
// FIXME: Experiments show that SUPER is ALT. So what is Alt then?
if ((state & XEMBED_MODIFIER_SUPER) != 0) {
mods |= InputEvent.ALT_DOWN_MASK;
}
// if ((state & XEMBED_MODIFIER_HYPER) != 0) {
// mods |= InputEvent.DOWN_MASK;
// }
return mods;
}
/**
* Sets new modifiers by the old ones.
* The mouse modifiers have higher priority than overlaying key
* modifiers.
*/
private void setNewModifiers() {
if ((gestureModifiers & InputEvent.BUTTON1_MASK) != 0) {
gestureModifiers |= InputEvent.BUTTON1_DOWN_MASK;
}
if ((gestureModifiers & InputEvent.BUTTON2_MASK) != 0) {
gestureModifiers |= InputEvent.BUTTON2_DOWN_MASK;
}
if ((gestureModifiers & InputEvent.BUTTON3_MASK) != 0) {
gestureModifiers |= InputEvent.BUTTON3_DOWN_MASK;
}
if ((gestureModifiers & InputEvent.SHIFT_MASK) != 0) {
gestureModifiers |= InputEvent.SHIFT_DOWN_MASK;
}
if ((gestureModifiers & InputEvent.CTRL_MASK) != 0) {
gestureModifiers |= InputEvent.CTRL_DOWN_MASK;
}
if ((gestureModifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
gestureModifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
}
}
public void testCCMethod() {
startTest();
EditorOperator eo = new EditorOperator("test.js");
cleanFile(eo);
type(eo, "\n\n");
eo.setCaretPositionToLine(1);
type(eo, "$(\"\").b");
eo.typeKey(' ', InputEvent.CTRL_MASK);
evt.waitNoEvent(100);
CompletionInfo completion = getCompletion();
String[] res = {"before", "bind", "blur"};
CompletionJListOperator cjo = completion.listItself;
checkCompletionItems(cjo, res);
String[] res2 = {"add"};
checkCompletionDoesntContainItems(cjo, res2);
completion.listItself.hideAll();
endTest();
}
public static void clickOnMenu() throws Exception {
Rectangle bounds = Util.invokeOnEDT(new Callable<Rectangle>() {
@Override
public Rectangle call() throws Exception {
return new Rectangle(menu.getLocationOnScreen(), menu.getSize());
}
});
Robot robot = new Robot();
robot.setAutoDelay(100);
robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
/**
* Drags from one point to another with the specified mouse button pressed.
*
* @param robot a robot to use for moving the mouse, etc.
* @param startPoint a start point of the drag
* @param endPoint an end point of the drag
* @param button one of {@code InputEvent.BUTTON1_MASK},
* {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
*
* @throws IllegalArgumentException if {@code button} is not one of
* {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
* {@code InputEvent.BUTTON3_MASK}
*/
public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
|| button == InputEvent.BUTTON3_MASK))
{
throw new IllegalArgumentException("invalid mouse button");
}
robot.mouseMove(startPoint.x, startPoint.y);
robot.mousePress(button);
try {
mouseMove(robot, startPoint, endPoint);
} finally {
robot.mouseRelease(button);
}
}
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
int column = tableHeader.columnAtPoint(e.getPoint());
int sortingColumn = headerRenderer.getSortingColumn();
if (column == sortingColumn) {
headerRenderer.reverseSortingOrder();
} else {
headerRenderer.setSortingColumn(column);
if (getInitialSorting(column)) {
headerRenderer.setSortingOrder(SORT_ORDER_ASC); // Default sort order for strings is Ascending
} else {
headerRenderer.setSortingOrder(SORT_ORDER_DESC); // Default sort order for numbers is Descending
}
}
tableHeader.repaint();
sortByColumn(column, headerRenderer.getSortingOrder());
}
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
new RemovedComponentMouseListener();
});
Robot r = Util.createRobot();
r.setAutoDelay(100);
r.waitForIdle();
Util.pointOnComp(button, r);
r.waitForIdle();
r.mousePress(InputEvent.BUTTON1_MASK);
r.waitForIdle();
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.waitForIdle();
if (!mouseReleasedReceived) {
throw new RuntimeException("mouseReleased event was not received");
}
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
new RemovedComponentMouseListener();
});
Robot r = Util.createRobot();
r.setAutoDelay(100);
r.waitForIdle();
Util.pointOnComp(button, r);
r.waitForIdle();
r.mousePress(InputEvent.BUTTON1_MASK);
r.waitForIdle();
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.waitForIdle();
if (!mouseReleasedReceived) {
throw new RuntimeException("mouseReleased event was not received");
}
}
private static synchronized void initLegalButtonMask() {
if (LEGAL_BUTTON_MASK != 0) return;
int tmpMask = 0;
if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled()){
if (Toolkit.getDefaultToolkit() instanceof SunToolkit) {
final int buttonsNumber = ((SunToolkit)(Toolkit.getDefaultToolkit())).getNumberOfButtons();
for (int i = 0; i < buttonsNumber; i++){
tmpMask |= InputEvent.getMaskForButton(i+1);
}
}
}
tmpMask |= InputEvent.BUTTON1_MASK|
InputEvent.BUTTON2_MASK|
InputEvent.BUTTON3_MASK|
InputEvent.BUTTON1_DOWN_MASK|
InputEvent.BUTTON2_DOWN_MASK|
InputEvent.BUTTON3_DOWN_MASK;
LEGAL_BUTTON_MASK = tmpMask;
}
public URIListBetweenJVMsTest (Point targetFrameLocation, Point dragSourcePoint,
int transferredFilesNumber)
throws InterruptedException
{
TargetFileListFrame targetFrame = new TargetFileListFrame(targetFrameLocation,
transferredFilesNumber);
Util.waitForIdle(null);
final Robot robot = Util.createRobot();
robot.mouseMove((int)dragSourcePoint.getX(),(int)dragSourcePoint.getY());
sleep(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
sleep(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
sleep(100);
Util.drag(robot, dragSourcePoint, targetFrame.getDropTargetPoint(),
InputEvent.BUTTON1_MASK);
}
public static void main(String[] args) throws Throwable {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
constructTestUI();
}
});
Util.waitForIdle(null);
try {
Point loc = textArea.getLocationOnScreen();
Util.drag(new Robot(),
new Point((int) loc.x + 3, (int) loc.y + 3),
new Point((int) loc.x + 40, (int) loc.y + 40),
InputEvent.BUTTON1_MASK);
} catch (AWTException ex) {
throw new RuntimeException("Could not initiate a drag operation");
}
Util.waitForIdle(null);
}
public static void main(final String[] args) throws Exception {
Robot r = new Robot();
// accessibility tool will need time to react to our clicks
r.setAutoDelay(200);
try {
EventQueue.invokeAndWait(SlowPanelIteration::showUI);
for (int i = 0; i < 10; ++i) {
go = new CountDownLatch(1);
r.mouseMove(center.x, center.y);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
if (!go.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException("Too slow operation");
}
}
} finally {
EventQueue.invokeAndWait(SlowPanelIteration::dispose);
}
}
/**
* Handles scroll events.
*/
void handleScrollEvent(final int x, final int y, final int modifierFlags,
final double deltaX, final double deltaY) {
final int buttonNumber = CocoaConstants.kCGMouseButtonCenter;
int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
modifierFlags);
final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
// Vertical scroll.
if (!isShift && deltaY != 0.0) {
dispatchScrollEvent(x, y, jmodifiers, deltaY);
}
// Horizontal scroll or shirt+vertical scroll.
final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
if (delta != 0.0) {
jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
dispatchScrollEvent(x, y, jmodifiers, delta);
}
}
public static String getModifiersExText(int modifiers)
{
StringBuilder buf = new StringBuilder();
if ((modifiers & InputEvent.META_DOWN_MASK) != 0)
{
buf.append("Meta+");
}
if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0)
{
buf.append("Ctrl+");
}
if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0)
{
buf.append("Alt+");
}
if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0)
{
buf.append("Shift+");
}
if (buf.length() > 0)
{
buf.setLength(buf.length() - 1); // remove trailing '+'
}
return buf.toString();
}
/**
* determine the drop action from the event
*/
protected int mapDragOperationFromModifiers(MouseEvent e) {
int mods = e.getModifiersEx();
int btns = mods & ButtonMask;
// Prohibit multi-button drags.
if (!(btns == InputEvent.BUTTON1_DOWN_MASK ||
btns == InputEvent.BUTTON2_DOWN_MASK ||
btns == InputEvent.BUTTON3_DOWN_MASK)) {
return DnDConstants.ACTION_NONE;
}
return
SunDragSourceContextPeer.convertModifiersToDropAction(mods,
getSourceActions());
}
public void addMouseListenerToHeaderInTable(JTable table) {
final TableSorter sorter = this;
final JTable tableView = table;
tableView.setColumnSelectionAllowed(false);
MouseAdapter listMouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
TableColumnModel columnModel = tableView.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = tableView.convertColumnIndexToModel(viewColumn);
if (e.getClickCount() == 1 && column != -1) {
System.out.println("Sorting ...");
int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
boolean ascending = (shiftPressed == 0);
sorter.sortByColumn(column, ascending);
}
}
};
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(listMouseListener);
}
public static void main(final String[] args) {
final int mask = InputEvent.META_DOWN_MASK | InputEvent.CTRL_MASK;
Frame f = new Frame();
MouseEvent mwe = new MouseWheelEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
1, 1, 1);
MouseEvent mdme = new MenuDragMouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1,
true, null, null);
MouseEvent me = new MouseEvent(f, 1, 1, mask, 1, 1, 1, 1, 1, true,
MouseEvent.NOBUTTON);
test(f, mwe);
test(f, mdme);
test(f, me);
if (FAILED) {
throw new RuntimeException("Wrong mouse event");
}
}
public MultiKeyBinding[] getJavaKeyBindings() {
return new MultiKeyBinding[] {
new MultiKeyBinding(
new KeyStroke[] {
KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.ALT_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_G, 0)
},
JavaKit.makeGetterAction
),
new MultiKeyBinding(
new KeyStroke[] {
KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.ALT_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_S, 0)
},
JavaKit.makeSetterAction
),
new MultiKeyBinding(
new KeyStroke[] {
KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.ALT_MASK),
KeyStroke.getKeyStroke(KeyEvent.VK_I, 0)
},
JavaKit.makeIsAction
),
new MultiKeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.ALT_MASK),
JavaKit.abbrevDebugLineAction
)
};
}
public ExportAction() {
putValue(Action.SHORT_DESCRIPTION, "Export current graph as an SVG file");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
lookup = Utilities.actionsGlobalContext();
result = lookup.lookup(new Lookup.Template<ExportCookie>(ExportCookie.class));
result.addLookupListener(this);
resultChanged(null);
}
public static void main(String[] args) throws Exception {
toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
robot = new Robot();
robot.setAutoDelay(50);
UIManager.setLookAndFeel(new MetalLookAndFeel());
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
toolkit.realSync();
for (int i = 0; i < items.length; i++) {
// Open popup
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
toolkit.realSync();
Point point = getItemPointToClick(i);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
toolkit.realSync();
if (i != getSelectedIndex()) {
throw new RuntimeException("Test Failed! Incorrect value of selected index = " + getSelectedIndex() +
", expected value = " + i);
}
}
}
public static int convertModifiersToDropAction(final int modifiers,
final int supportedActions) {
int dropAction = DnDConstants.ACTION_NONE;
/*
* Fix for 4285634.
* Calculate the drop action to match Motif DnD behavior.
* If the user selects an operation (by pressing a modifier key),
* return the selected operation or ACTION_NONE if the selected
* operation is not supported by the drag source.
* If the user doesn't select an operation search the set of operations
* supported by the drag source for ACTION_MOVE, then for
* ACTION_COPY, then for ACTION_LINK and return the first operation
* found.
*/
switch (modifiers & (InputEvent.SHIFT_DOWN_MASK |
InputEvent.CTRL_DOWN_MASK)) {
case InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK:
dropAction = DnDConstants.ACTION_LINK; break;
case InputEvent.CTRL_DOWN_MASK:
dropAction = DnDConstants.ACTION_COPY; break;
case InputEvent.SHIFT_DOWN_MASK:
dropAction = DnDConstants.ACTION_MOVE; break;
default:
if ((supportedActions & DnDConstants.ACTION_MOVE) != 0) {
dropAction = DnDConstants.ACTION_MOVE;
} else if ((supportedActions & DnDConstants.ACTION_COPY) != 0) {
dropAction = DnDConstants.ACTION_COPY;
} else if ((supportedActions & DnDConstants.ACTION_LINK) != 0) {
dropAction = DnDConstants.ACTION_LINK;
}
}
return dropAction & supportedActions;
}
protected void processKeyEvent(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_CONTEXT_MENU ||
(code == KeyEvent.VK_F10 && e.getModifiers() == InputEvent.SHIFT_MASK)) {
e.consume();
showPopupMenu(null);
}
super.processKeyEvent(e);
}
/**
* downcall into native code
*/
native long createDragSource(Component component,
Transferable transferable,
InputEvent nativeTrigger,
int actions,
long[] formats,
Map formatMap);
private void setupRomTable() {
DefaultTableModel romTableModel = new DefaultTableModel();
romTableModel.addColumn("Character(s)");
romTableModel.addColumn("Romanization");
tblRom.setModel(romTableModel); // TODO: find way to make rom display RTL order when appropriate Maybe something on my custom cell editor
boolean useConFont = !core.getPropertiesManager().isOverrideRegexFont();
TableColumn column = tblRom.getColumnModel().getColumn(0);
column.setCellEditor(new PCellEditor(useConFont, core));
column.setCellRenderer(new PCellRenderer(useConFont, core));
column = tblRom.getColumnModel().getColumn(1);
column.setCellEditor(new PCellEditor(false, core));
column.setCellRenderer(new PCellRenderer(false, core));
// disable tab/arrow selection
InputMap procInput = tblRom.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.SHIFT_DOWN_MASK), "none");
}
private void autoinsertNamespace(EditorOperator eo, String typedElement, String expectedNS) {
eo.setCaretPositionToEndOfLine(9);
type(eo, typedElement);
eo.typeKey(' ', InputEvent.CTRL_MASK);
evt.waitNoEvent(1000);
eo.pressKey(java.awt.event.KeyEvent.VK_ENTER);
evt.waitNoEvent(1000);
assertTrue("Namespace declaration not inserted (" + expectedNS + ")", eo.getText().contains(expectedNS));
eo.typeKey('a', InputEvent.CTRL_MASK);
eo.pressKey(java.awt.event.KeyEvent.VK_DELETE);
eo.insert(NamespacesTest.originalContent);
eo.save();
}
/**
* mouse-over event on a visual item (node item or edge item)
*/
@Override
public void itemEntered(VisualItem item, MouseEvent e) {
// if ctrl is pressed, user zooms -> ignore itemEntered
if (e.getModifiers() == InputEvent.CTRL_MASK) {
ctrlZoom(e);
return;
}
// only mark items as highlighted if the layout process is active
RunLayoutControl rlc = new RunLayoutControl(viewManagerID);
if (rlc.isLayouting()) {
if (item instanceof NodeItem) {
/* set highlight attribute to true, NodeRenderer will change the color */
item.set(ColumnNames.IS_HIGHLIGHTED, true);
}
if (item instanceof EdgeItem) {
/* set highlight attribute to true, EdgeRenderer will change the color */
item.set(ColumnNames.IS_HIGHLIGHTED, true);
}
if (item instanceof TableDecoratorItem) {
/* set highlight attribute to true, EdgeRenderer will change the color */
item.set(ColumnNames.IS_HIGHLIGHTED, true);
}
}
}