I'm looking for a way to trigger a click event to the OS from a NodeJS application. All I need is to have control over x/y and mouse button.
Is there anything that does that? I've searched for existing packages but didn't find any...
RobotJS does exactly what you want!
http://github.com/octalmage/robotjs
Try this code:
//Get the mouse position, move it, then click.
var robot = require("robotjs");
//Get the mouse position, returns an object with x and y.
var mouse = robot.getMousePos();
console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y);
//Move the mouse down by 100 pixels.
robot.moveMouse(mouse.x, mouse.y + 100);
//Left click!
robot.mouseClick();
Related
I want to check if you clicked in an defined area, for example an area from 0,0 to 400,40 (pixel coordinates). I have this:
x = 0
y = 0
(mouse_posx, mouse_posy) = pygame.mouse.get_pos()
press = pygame.key.get_pressed()
if mouse_posx > x and mouse_posx < x+400 and mouse_posy > y and mouse_posy < y+40 and press != 0:
function()
I get no error but it does nothing.
Can someone tell me what i am doing wrong?
What you want to do is have your program listen for events. Once the event of mouse button is triggered, then record the mouse position. Then verify if the mouse position is inside the box.
Pygame mouse
Oh and also, I dont think the event of mouse button gets recorded in the > pygame.keys.get_pressed <. I believe the pygame.keys.get_pressed is just for the keyboard.
I may be wrong, im using my mobile and dont have a computer with me.
I am beginning to use snap.svg, and stuck in a probably simple question. I draw a circle, and want to let the user drag it around, and when the user stops dragging, I want to know the alert the final position of the circle.
I started with this:
var s = Snap("#svg");
var d = s.circle(20,20,5);
Then I created the following functions as drag event-handlers; I only need the drag-end event so I made the two other event-handlers null:
var endFnc = function(e) {
alert("end: "+e.layerX+","+e.layerY);
}
var startFnc = null;
var moveFnc = null;
Then I installed the drag event handlers:
d.drag(moveFnc,startFnc,endFnc);
Whenever I tried to drag a circle, the end event fired, but, the circle did not move.
So I figured maybe I have to also create the drag move event (although I don't need it):
var moveFnc = function(dx, dy, x, y) {
this.transform('t' + x + ',' + y);
}
Now the circle did move, but, not exactly at the mouse, but approximately 20 pixels at its bottom right.
I read the documentation here: http://snapsvg.io/docs and there seems to be no explanation about how to create working drag events.
How does anyone get this knowledge?!
After struggling for some hours to do this with snap.js, I finally discovered svg.js and its draggable plugin, with which it is so much easier:
var draw = SVG('svg');
var circle = draw.circle(10).attr({cx:30,cy:30,fill:'#f06'});
circle.dragend = function(delta, event) {
alert(this.attr('cx'))
}
circle.draggable();
So, I switched to svg.js ...
Ciao, I am trying to accomplish this: I would be able to know wich 3d object is double clicked in my scene, and animate camera position to bring the object in the center of the screen.
I tried without success to adapt the interactive cubes example that uses raycaster and projector...
http://www.gioblu.com/GiO/web/solarsystem/index_backup
As you can see you can navigate in space and change camera position with right and left mouse button. I would be able to come back to initial camera position (earth in the center of the screen) with a double click on the planet.
Why didn't you had success by adapting the example? What errors are occuring? Did you mean this example?
Because raycaster and projector is the way you are looking for.
First of all you need an eventListener for the ondblclick Event on your container. In the event function you can copy&paste from the linked example:
1) Saving mouse coordinates
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
2) Project coordinates to world system via the camera and create a ray
var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
projector.unprojectVector( vector, camera );
raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
3) Check if the double clicked element is your planet
var intersects = raycaster.intersectObject( "your_planet" );
if ( intersects.length > 0 ) {
reset your camera
}
Hope this helps!
I'm trying to make a simple game using Raphaƫl.js and I came across a problem very early in development, where I made a sight for aiming by attaching a circle to the cursors position, but when I click the target it doesn't trigger the targets click event , but the aim.
background.mousemove(function(e){
ePointer.attr({
'fill':'none',
'stroke':'none'
});
nx = e.clientX - $('#canvas').offset().left;
ny = e.clientY - $('#canvas').offset().top;
pointer.attr({
'fill':'none',
'stroke':'#CC0000',
'stroke-width': 3,
'cx':nx,
'cy':ny
});
});
background.click(function(e){
pointer.animate(bClick);
pointer.animate(bUnclick.delay(250));
pointer.animate(bUnclick);
});
enemies.mousemove(function(e){
pointer.attr({
'fill':'none',
'stroke':'none'
});
nx = e.clientX - $('#canvas').offset().left;
ny = e.clientY - $('#canvas').offset().top;
ePointer.attr({
'cx':nx,
'cy':ny,
'fill': '#00CC00',
'stroke':'none'
});
});
The jsfiddle link will say more then I could : http://jsfiddle.net/Uuqgx/6/
Thanks in advanced,
Giovanni.
Raphael's paper object has a method called Paper.getElementByPoint(x, y) that gets the top object under the mouse. Of course, this alone doesn't help you since the circle is still under the mouse. However, if you hide it, run this method inputing the current mouse coordinates, then unhide the circle, you'll get the rectangle under it without any visible interruption:
ePointer.click(function(e){
score++
tScore.attr({
'text': 'Score: ' + score
});
nx = e.clientX - document.getElementById('canvas').offsetLeft;
ny = e.clientY - document.getElementById('canvas').offsetTop;
//get targeted rectangle
this.hide();
console.log(paper.getElementByPoint(nx, ny));
this.show();
ePointer.animate(eClick);
ePointer.animate(eUnclick.delay(150));
ePointer.animate(eUnclick);
});
Here I've just logged the targeted rectangle to the console, but once you've found it like this you can do whatever you want to it.
Note that offset().Top becomes offsetTop in order for this to work in jsFiddle, since jQuery isn't present in the fiddle.
Updated fiddle.
I have a TextField for which I want a Tooltip to be shown under some circumstances.
After performing checks I run the followig code:
textFieldUsername.setTooltip(new Tooltip("Enter username!"));
textFieldUsername.getTooltip().setAutoHide(true);
textFieldUsername.getTooltip().show(textFieldUsername, 1, 1);
So when somebody tries to login with empty username he gets a prompting Tooltip over the "username" TextField.
But when it comes to action, the Tooltip pops up in the top left corner of the screen.
Should I calculate coords of my scene, then add my TextField coords to them, or there is a way to set these 1, 1 arguments from the call of show() to be relative to the TextField position?
I think the coordinates are always relative to the screen. To calculate component coordinates you need to incorporate scene and window coordinates.
Point2D p = label.localToScene(0.0, 0.0);
label.getTooltip().show(label,
p.getX() + label.getScene().getX() + label.getScene().getWindow().getX(),
p.getY() + label.getScene().getY() + label.getScene().getWindow().getY());