JxBrowser API provides functionality that allows programmatically send/forward mouse events to the currently loaded web page. The mouse events will be send to the loaded web page as if they were sent due to user actions.(JxBrowser API提供的功能允许以编程方式将鼠标事件发送/转发到当前加载的网页。鼠标事件将被发送到加载的网页,就像它们是由于用户操作而发送的一样。)
The following example demonstrates how to use this functionality:(下面的示例演示如何使用此功能:)
/* * Copyright (c) 2000-2017 TeamDev Ltd. All rights reserved. * TeamDev PROPRIETARY and CONFIDENTIAL. * Use is subject to license terms. */ import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.BrowserKeyEvent.KeyModifiers; import com.teamdev.jxbrowser.chromium.BrowserKeyEvent.KeyModifiersBuilder; import com.teamdev.jxbrowser.chromium.BrowserMouseEvent.BrowserMouseEventBuilder; import com.teamdev.jxbrowser.chromium.BrowserMouseEvent.MouseButtonType; import com.teamdev.jxbrowser.chromium.Callback; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import static com.teamdev.jxbrowser.chromium.BrowserMouseEvent.MouseButtonType.*; import static com.teamdev.jxbrowser.chromium.BrowserMouseEvent.MouseEventType.*; import static com.teamdev.jxbrowser.chromium.BrowserMouseEvent.MouseScrollType.WHEEL_BLOCK_SCROLL; /** * This sample demonstrates how to create and forward mouse events * containing scroll parameters, modifiers, and button options to Chromium engine. */ public class ForwardMouseEventsSample { public static void main(String[] args) { final Browser browser = new Browser(); BrowserView view = new BrowserView(browser); JButton scrollDown = new JButton("Scroll Down"); scrollDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { forwardMouseScrollEvent(browser, -1, 11, 62); } }); JButton scrollUp = new JButton("Scroll Up"); scrollUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { forwardMouseScrollEvent(browser, 1, 11, 62); } }); JButton clickMe = new JButton("Click"); clickMe.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { forwardMouseClickEvent(browser, PRIMARY, 11, 12, 629, 373); } }); JPanel actionPane = new JPanel(); actionPane.add(scrollDown); actionPane.add(scrollUp); actionPane.add(clickMe); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(actionPane, BorderLayout.SOUTH); frame.add(view, BorderLayout.CENTER); frame.setSize(600, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() { @Override public void invoke(Browser value) { browser.loadHTML("<div>" + "<button onclick=\"alert('Mouse has been clicked.');\">Click Me</button></div>" + "<textarea autofocus rows='10' cols='30'>" + "Line 1 \n Line 2 \n Line 3 \n Line 4 \n Line 5 \n " + "Line 6 \n Line 7 \n Line 8 \n Line 9 \n Line 10 \n " + "Line 11 \n Line 11 \n Line 12 \n Line 13 \n Line 14 \n " + "Line 15 \n Line 16 \n Line 17 \n Line 18 \n </textarea>"); } }); } private static void forwardMousePressEvent(Browser browser, MouseButtonType buttonType, int x, int y, int globalX, int globalY) { BrowserMouseEventBuilder builder = new BrowserMouseEventBuilder(); builder.setEventType(MOUSE_PRESSED) .setButtonType(buttonType) .setX(x) .setY(y) .setGlobalX(globalX) .setGlobalY(globalY) .setClickCount(1) .setModifiers(new KeyModifiersBuilder().mouseButton().build()); browser.forwardMouseEvent(builder.build()); } private static void forwardMouseReleaseEvent(Browser browser, MouseButtonType buttonType, int x, int y, int globalX, int globalY) { BrowserMouseEventBuilder builder = new BrowserMouseEventBuilder(); builder.setEventType(MOUSE_RELEASED) .setButtonType(buttonType) .setX(x) .setY(y) .setGlobalX(globalX) .setGlobalY(globalY) .setClickCount(1) .setModifiers(KeyModifiers.NO_MODIFIERS); browser.forwardMouseEvent(builder.build()); } private static void forwardMouseClickEvent(Browser browser, MouseButtonType buttonType, int x, int y, int globalX, int globalY) { forwardMousePressEvent(browser, buttonType, x, y, globalX, globalY); forwardMouseReleaseEvent(browser, buttonType, x, y, globalX, globalY); } private static void forwardMouseScrollEvent(Browser browser, int unitsToScroll, int x, int y) { BrowserMouseEventBuilder builder = new BrowserMouseEventBuilder(); builder.setEventType(MOUSE_WHEEL) .setX(x) .setY(y) .setGlobalX(0) .setGlobalY(0) .setScrollBarPixelsPerLine(25) .setScrollType(WHEEL_BLOCK_SCROLL) .setUnitsToScroll(unitsToScroll); browser.forwardMouseEvent(builder.build()); } }