1 | static int forwardClicks_windowSize = 11; // odd should look nice. Set to 1 for an invisible window |
2 | static int forwardClicks_clickDelay = 0; // Delay in ms between closing window and forwarding click. 0 seems to work fine. |
3 | static int forwardClicks_dragDelay = 100; // Delay in ms when dragging |
4 | static int forwardClicks_trackingSpeed = 10; // How often to move the window (ms) |
5 | static int forwardClicks_restartDelay = 1000; // How quickly to restart after click (e.g. to allow untraced drags [forwarding dragging doesn't always work]) |
6 | sbool forwardClicks_verbose; |
7 | |
8 | // onClick: voidfunc(Point, Point, int) - press location, release location, button (1 to 3) |
9 | static void forwardClicks(final O onClickAndRelease) { |
10 | awtIfNecessary { |
11 | final new JWindow window; |
12 | final int windowSize = forwardClicks_windowSize; |
13 | window.setSize(windowSize, windowSize); |
14 | window.setVisible(true); |
15 | window.setAlwaysOnTop(true); |
16 | JPanel panel = singleColorPanel(Color.red); |
17 | window.setContentPane(panel); |
18 | revalidate(window); |
19 | final new Robot robot; |
20 | panel.addMouseListener(new MouseAdapter { |
21 | Point pressed; |
22 | |
23 | public void mousePressed(final MouseEvent e) { |
24 | pressed = getMouseLocation(); |
25 | } |
26 | |
27 | public void mouseReleased(final MouseEvent e) { |
28 | window.setVisible(false); |
29 | final Point released = getMouseLocation(); |
30 | final Point _pressed = or(pressed, released); |
31 | final int b = e.getButton(); |
32 | final int mod = |
33 | b == 1 ? InputEvent.BUTTON1_DOWN_MASK |
34 | : b == 2 ? InputEvent.BUTTON2_DOWN_MASK |
35 | : InputEvent.BUTTON3_DOWN_MASK; |
36 | swingLater(forwardClicks_clickDelay, r { |
37 | pcallF(onClickAndRelease, _pressed, released, b); |
38 | robot.mouseMove(_pressed.x, _pressed.y); |
39 | robot.mousePress(mod); |
40 | swingLater(neq(_pressed, released) ? forwardClicks_dragDelay : 0, r { |
41 | robot.mouseMove(released.x, released.y); |
42 | robot.mouseRelease(mod); |
43 | |
44 | // Start tracking again - make new JWindow otherwise we get a crazy item on task bar in Linux |
45 | swingLater(forwardClicks_restartDelay, r { |
46 | forwardClicks(onClickAndRelease); |
47 | }); |
48 | }); |
49 | }); |
50 | } |
51 | }); |
52 | |
53 | swingEvery(window, forwardClicks_trackingSpeed, r { |
54 | final Point p = getMouseLocation(); |
55 | // Expand height if in task bar (crude fix for Peppermint Linux) |
56 | int bottom = getScreenHeight()-(768-743); |
57 | final Point loc = new Point(p.x-windowSize/2, min(bottom, p.y-windowSize/2)); |
58 | int h = max(windowSize, p.y-loc.y+windowSize/2); |
59 | window.setBounds(loc.x, loc.y, windowSize, h); |
60 | |
61 | if (forwardClicks_verbose) { |
62 | Point onScreen = window.getLocationOnScreen(); |
63 | print(loc + " " + onScreen); |
64 | } |
65 | /*if (neq(onScreen, loc)) |
66 | print("Location not honored");*/ |
67 | }); |
68 | } |
69 | } |
Began life as a copy of #1005566
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1005569 |
Snippet name: | forwardClicks - track mouse pointer forward clicks & drags continously |
Eternal ID of this version: | #1005569/1 |
Text MD5: | 3c24545bbb5ff0c8a30c122b47edff1a |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-12-02 15:21:32 |
Source code size: | 2970 bytes / 69 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 479 / 451 |
Referenced in: | [show references] |