-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleapMouse.java
More file actions
194 lines (151 loc) · 6.32 KB
/
Copy pathleapMouse.java
File metadata and controls
194 lines (151 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package leapWatch;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture.State;
import com.leapmotion.leap.Gesture.Type;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
class CustomListener extends Listener {
public Robot robot;
public void onConnect(Controller c) {
c.setPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES); // allows the program to run in places other than the terminal
c.enableGesture(Gesture.Type.TYPE_CIRCLE);
c.enableGesture(Gesture.Type.TYPE_SWIPE);
c.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
}
public void onFrame(Controller c){
try {robot = new Robot();} catch (Exception e) {}
Frame frame = c.frame();
InteractionBox box = frame.interactionBox();
for (Hand h: frame.hands()) {
int fCount = 0;
String handType = h.isLeft() ? "left" : "right";
// get the normal vector of the hand. Used to detect tilt
Vector normal = h.palmNormal();
Vector direction = h.direction();
Vector palmPos = h.stabilizedPalmPosition();
Vector palmVel = h.palmVelocity();
Vector boxPalmPos = box.normalizePoint(palmPos);
double grab = h.grabStrength();
double roll = Math.toDegrees(normal.roll());
double pitch = Math.toDegrees(direction.pitch());
double hand_angle = Math.toDegrees(h.grabStrength());
double yaw = Math.toDegrees(direction.yaw());
Vector handSpeed = h.palmVelocity();
if (h.isRight()) {
Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
//System.out.print(screen.width * (boxPalmPos.getX() - 0.4));
//System.out.print("\n");
// move hand's center to the right for better controd
// vanilla game pad ctrl
robot.mouseMove((int)(screen.width * (boxPalmPos.getX() - 0.4)), (int) (screen.height - boxPalmPos.getY() * screen.height));
// natural mouse control
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
//robot.mouseMove((int)(x + palmVel.getX()), (int) (y - palmVel.getY()));
//robot.mouseMove((int)(screen.width * (yaw + 180)/360), (int) (screen.height - (pitch+180)lll * screen.height));
if (grab >= 0.95){
//System.out.print(grab);
//System.out.print("\n");
robot.mousePress(InputEvent.BUTTON1_MASK); //press the left mouse button
robot.mouseRelease(InputEvent.BUTTON1_MASK); //release the right mouse button
}else if (roll > -60 && roll <-30) {
System.out.print(roll);
System.out.print("\n");
robot.keyPress(KeyEvent.VK_L); //press the right mouse button
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
robot.keyRelease(KeyEvent.VK_L); //release the right mouse button
}
}
else if (h.isLeft()) {
if(grab >=0.95) {
robot.keyPress(KeyEvent.VK_Q); //press the right mouse button
robot.keyRelease(KeyEvent.VK_Q); //release the right mouse button
}
if(roll > 30 + 90) {
//System.out.print(roll);
//System.out.print("\n");
robot.keyPress(KeyEvent.VK_A); //press the right mouse button
robot.keyRelease(KeyEvent.VK_A); //release the right mouse button
}
if(roll < -30 + 90) {
//System.out.print(roll);
//System.out.print("\n");
robot.keyPress(KeyEvent.VK_D); //press the right mouse button
robot.keyRelease(KeyEvent.VK_D); //release the right mouse button
}
if(pitch > 20) {
//System.out.print(roll);
System.out.print(pitch);
System.out.print("\n");
//System.out.print("\n");
robot.keyPress(KeyEvent.VK_S); //press the right mouse button
robot.keyRelease(KeyEvent.VK_S); //release the right mouse button
}
if(pitch < -10) {
System.out.print(pitch);
System.out.print("\n");
robot.keyPress(KeyEvent.VK_W); //press the right mouse button
robot.keyRelease(KeyEvent.VK_W); //release the right mouse button
}
/**
if(palmPos.getY() > 10) {
robot.keyPress(KeyEvent.VK_W); //press the right mouse button
robot.keyRelease(KeyEvent.VK_W); //release the right mouse button
}
if(palmPos.getY() < -10) {
robot.keyPress(KeyEvent.VK_S); //press the right mouse button
robot.keyRelease(KeyEvent.VK_S); //release the right mouse button
}
**/
}
}
/**
for (Finger f: frame.fingers()){
if (f.type() == Finger.Type.TYPE_INDEX) {
// maybe better with tipVelo
Vector fingerPos = f.stabilizedTipPosition();
Vector boxFingerPos = box.normalizePoint(fingerPos);
Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
robot.mouseMove((int)(screen.width * boxFingerPos.getX()), (int) (screen.height - boxFingerPos.getY() * screen.height));
}
}
**/
for (Gesture g: frame.gestures()) {
if (g.type() == Type.TYPE_CIRCLE) {
CircleGesture circle = new CircleGesture(g);
if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI/4) {
System.out.print("R");
robot.keyPress(KeyEvent.VK_R); //press r
robot.keyRelease(KeyEvent.VK_R); //release r
}
}else if (g.type() == Type.TYPE_SWIPE && g.state() == State.STATE_START) {
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
}
}
}
}
public class leapMouse {
public static void main(String[] args) {
CustomListener l = new CustomListener();
Controller c = new Controller();
c.addListener(l);
try {
System.in.read();
} catch (Exception e){
}
c.removeListener(l);
}
}