Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS 

Capturing the mousemove coordinates

Source code copied from The JavaScript Source: Page Details: Mouse Coordinates [internet.com]
Tested on Firefox.
<!-- ONE STEP TO INSTALL MOUSE COORDINATES:

  1.  Copy the coding into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the BODY of your HTML document  -->

<BODY>

<form name="Show">
X <input type="text" name="MouseX" value="0" size="4"><br>
Y <input type="text" name="MouseY" value="0" size="4"><br>
</form>

<script language="JavaScript1.2">
<!-- Original:  CodeLifter.com (support@codelifter.com) -->
<!-- Web Site:  http://www.codelifter.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
  var IE = document.all?true:false;
  if (!IE) document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = getMouseXY;
    
  var tempX = 0;
  var tempY = 0;
  
  function getMouseXY(e) {
    if (IE) { // grab the x-y pos.s if browser is IE
      tempX = event.clientX + document.body.scrollLeft;
      tempY = event.clientY + document.body.scrollTop;
    }
    else {  // grab the x-y pos.s if browser is NS
      tempX = e.pageX;
      tempY = e.pageY;
    }  
    
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}  
    
    document.Show.MouseX.value = tempX;
    document.Show.MouseY.value = tempY;
    
    return true;
  }
//  End -->

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size:  1.33 KB -->

*update 10:55am 21-Mar-08*
I have decided to use the following code instead as it looks a bit cleaner, and more up-to-date.
Tested on Firefox 2 and IE 6.
document.onmousemove = mouseMove;

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

Reference: How to Drag and Drop in JavaScript [webreference.com]

Make AS3 clips turn cursor into a hand on rollover..

  buttonMode = true; 
  mouseChildren = false;

Set Cursor to Hourglass

// Sets Mouse Cursor to Hourglass using Javascript

//Set cursor to hourglass
document.body.style.cursor = "wait";
//Turn hourglass off
document.body.style.cursor = "default";

Change Text Properties on Mouseover

This only works with the IE browsers. Add this code within your <head> tags. To keep the underline on your links, replace "none" with "underline" Change "YourColor" with the hexidecimal number of color required.
Find this and other snippets on my site, if you wish, by clicking here

<style
type="text/css"> <!-- A:link { text-decoration: none; color:#YourColor
} A:visited { text-decoration: none; color:#YourColor } A:hover { text-decoration:
none; color:#YourColor } --> </style>

Xlib - mouseClick

// Simula il click del mouse

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

void mouseClick(int button)
{
	Display *display = XOpenDisplay(NULL);

	XEvent event;
	
	if(display == NULL)
	{
		fprintf(stderr, "Errore nell'apertura del Display !!!\n");
		exit(EXIT_FAILURE);
	}
	
	memset(&event, 0x00, sizeof(event));
	
	event.type = ButtonPress;
	event.xbutton.button = button;
	event.xbutton.same_screen = True;
	
	XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
	
	event.xbutton.subwindow = event.xbutton.window;
	
	while(event.xbutton.subwindow)
	{
		event.xbutton.window = event.xbutton.subwindow;
		
		XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
	}
	
	if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
	
	XFlush(display);
	
	usleep(100000);
	
	event.type = ButtonRelease;
	event.xbutton.state = 0x100;
	
	if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
	
	XFlush(display);
	
	XCloseDisplay(display);
}


// gcc source.c -L /usr/X11R6/lib -lX11

Allegro Dibujos de posicion relativa

Dibujos en allegro dependiendo de la posicion del mouse

#include <allegro.h>


void  dibujo(int x,int y){
      circle (screen, x+100,y+100,100,makecol(128,0,0));
      circlefill (screen,x+50,y+50,20,makecol(0,255,0));
      circlefill (screen,x+150,y+50,20,makecol(0,255,0));
      rect (screen,x+80,y+100,x+120,y+150,makecol(0,0,128));
      line (screen,x+50,y+180,x+150,y+180,makecol(128,128,0));
}

void init();
void deinit();


int main() {
	init();
	show_mouse(screen);
	while (!key[KEY_ESC]) {  
          clear_to_color(screen,0);
          dibujo(mouse_x,mouse_y);
	}

	deinit();
	return 0;
}
END_OF_MAIN();

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}

Manejo de mouse en Allegro

Manejo de mouse en Allegro

#include <allegro.h>

void init();
void deinit();

int main() {
	init();


    int x,y;
    show_mouse(screen);
	while (!key[KEY_ESC]) {
		/* put your code here */
  
       for (x=10;x<600;x+=50){
           for (y=10;y<420;y+=60){        
               if (mouse_x > x && mouse_x < x+40 && mouse_y >y && mouse_y < y+50){
                  circlefill(screen, x+20,y+25,20,makecol(128,y%255,0));    
               } else {
                  rectfill(screen, x,y,x+40,y+50,makecol(0,255,0));           
               }
           }    
       }
	}

	deinit();
	return 0;
}
END_OF_MAIN();
 
void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}

Simple Mouse Wheel support for GWT

I was looking to a way to use the mouse wheel in my GWT app, but I didn“t find a straightforward way... so today I spend my morning learning JSNI and, with some piece of code I found on the Internet, I coded my own approach... Feel free to use anywere .. Hope that it might be useful for someone else too...

public interface MouseWheelListener {
   
    public void onMouseWheelUp(int intensity);
   
    public void onMouseWheelDown(int intensity);
   
}

public class MouseWheel {
   
    private MouseWheel(Element e, MouseWheelListener listener) {
        attachMouseWheelListener(e, listener);
    }
   
    /**
     * Sets a MouseWheelListener to a given Element
     */
    public static void setMouseWheelListener(Element e, MouseWheelListener listener) {
        new MouseWheel(e, listener);
    }
   
   
    /**
     * This method is used by FF
     * @param event
     */
    private static native void dispatchMouseWheelEvent(JavaScriptObject event) /*-{
       
        @br.com.example.client.util.MouseWheel::dispatchMouseWheelEvent(Lcom/google/gwt/core/client/JavaScriptObject;Lbr/com/example/client/util/MouseWheelListener;)(event, this.__mousewheellistener);
   
    }-*/;
   
    /**
     * This method is used by IE and FF
     * Part of this method was retrieved from http://adomas.org/notes/mouse-wheel.html
     *
     * @param event
     * @param listener
     */
    private static native void dispatchMouseWheelEvent(JavaScriptObject event, MouseWheelListener listener) /*-{

         if (!event) event = $wnd.event; // For IE

         var delta = 0;
         if (event.wheelDelta)             // IE case, delta is multiple of 120
              delta = event.wheelDelta / 120;
         else if (event.detail )            // Mozilla case
              delta = -event.detail / 3;   // different sign and multiple of 3
        
         if ( delta > 0 ) {
             listener.@br.com.example.client.util.MouseWheelListener::onMouseWheelUp(I)(delta);
         } else {
             listener.@br.com.example.client.util.MouseWheelListener::onMouseWheelDown(I)(-delta);
         }

         //taken from http://adomas.org/javascript-mouse-wheel/test2.html
         if (event.preventDefault)
             event.preventDefault();
         event.returnValue = false;

    }-*/;
   
   
    private native void attachMouseWheelListener(Element e, MouseWheelListener listener) /*-{
       
        e.__mousewheellistener = listener;
       
        // for FF
        if (e.addEventListener) {
            e.addEventListener('DOMMouseScroll', @br.com.example.client.util.MouseWheel::dispatchMouseWheelEvent(Lcom/google/gwt/core/client/JavaScriptObject;), false);
            return;
        }
       
        // for IE
        e.onmousewheel = function(event) {
            @br.com.example.client.util.MouseWheel::dispatchMouseWheelEvent (Lcom/google/gwt/core/client/JavaScriptObject;Lbr/com/example/client/util/MouseWheelListener;)(event, this.__mousewheellistener);
        }
       
    }-*/;
   
}



And here is how to use it:


        MouseWheel.setMouseWheelListener(RootPanel.get("slot2").getElement(), new MouseWheelListener() {

            public void onMouseWheelUp(int intensity) {
                Window.alert("up.. " + intensity);
            }

            public void onMouseWheelDown(int intensity) {
                Window.alert("down..." + intensity);
            }
       
        }); 



Remember to replace my hardcoded package name (br.com.example) for your own... If you have any suggestion or critic, feel free to tell me... "tserafim" is my gmail username...

Python - Mouse Capture

// Minimo Esempio di pannello con evento

import wx

class MyFrame(wx.Frame):
    
    def __init__(self):
        
        # creo un frame
        wx.Frame.__init__(self, None, -1, 'My Frame', size=(300, 300))
        # aggiungo un pannello
        panel = wx.Panel(self, -1)
        # aggiungo un evento al pannello
        panel.Bind(wx.EVT_MOTION, self.OnMove)
        # aggiungo un controllo di testo
        self.posCtrl = wx.TextCtrl(panel, -1, 'Pos: ', pos=(40, 10))
        
    def OnMove(self, event):
        
        # catturo la posizione del mouse
        pos = event.GetPosition()
        # scrivo tale posizione nel controllo di testo
        self.posCtrl.SetValue('%s, %s' % (pos.x, pos.y))
        
if '__main__' == __name__:
    
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

mouse scroll-wheel events

; mouse-wheel scroll events
view/new layout [
	b: box "A Box" forest feel [
		engage: func [face action event] [
			if action = 'scroll-line [
				print ["Scroll line" event/offset]
			]
			if action = 'scroll-page [ ; Ctrl+wheel
				print ["scroll page" event/offset]
			]
		]
	]
]
focus b
do-events
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS