Pagina 2 di 5 primaprima 1 2 3 4 ... ultimoultimo
Visualizzazione dei risultati da 11 a 20 su 50

Discussione: [JInvoke] hook globale

  1. #11
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    131
    guarda ho provato io con questo codice.. e funziona fa un beep ogni volta che cambia il cursore
    codice:
    #include <Windows.h>
    
    int main()
    {
    	CURSORINFO	CursorInfo;
    	HCURSOR		hCursor;
    
    	CursorInfo.cbSize = sizeof (CursorInfo);
    	GetCursorInfo (&CursorInfo);
    
    	
    	hCursor = CursorInfo.hCursor;
    
    	for (;; )
    	{
    		CursorInfo.cbSize = sizeof (CursorInfo);
    		GetCursorInfo (&CursorInfo);
    
    		if (CursorInfo.hCursor != hCursor)
    		{
    			Beep (1000, 1000);
    			hCursor = CursorInfo.hCursor;
    		}
    
    		Sleep (5);
    	}
    
    	return 0;
    }

  2. #12
    sicche io dovrei importare il tuo codice nel mio programma java?

  3. #13
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    131
    non conosco bene il java..
    nel tuo hook devi chiamare la GetCursorInfo e controllare se hCursor della struttura è cambiato rispetto all'ultima volta, in quel modo sa quando il cursore è cambiato..
    il codice che ho postato fa esattamente quella cosa, però senza l'hook..

    edit: comunque posta il codice se non funziona..

  4. #14
    ok ti ringrazio moltissimo ora vedo di provare ti faccio sapere al piu presto

  5. #15
    allora io ho queste due classi
    codice:
     
    
    package MouseHook;
    import static com.jinvoke.win32.WinConstants.*;
    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.TextArea;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    import com.jinvoke.Callback;
    import com.jinvoke.JInvoke;
    import com.jinvoke.NativeImport;
    import com.jinvoke.Util;
    import com.jinvoke.win32.Kernel32;
    import com.jinvoke.win32.User32;
    import com.jinvoke.win32.structs.Msg;
    
    public class MouseHook extends JPanel{
    	static {
    		JInvoke.initialize();
    	}	
    	
    	@NativeImport(library = "user32")
    	public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId);
    	
    	@NativeImport(library = "user32")
    	public native static int UnhookWindowsHookEx (int idHook);
    	
    	public static final int WH_MOUSE_LL = 14;
    	static JFrame frame;
    	
    	static TextArea AreaEventiMouse = new TextArea();
    	static JButton setHookBtn;
    	static JButton removeHookBtn;
    	
    	public MouseHook() {
            super(new BorderLayout());
    
    		AreaEventiMouse.setText("1) Clicca su \"Set Mouse Hook\" bottone.\n" +
    				"2) inizia a cliccare sul  desktop.  Gli eventi vengono qui catturati.\n" +
    				"3) ferma l'hook cliccando su \"Remove Mouse Hook\" bottone.\n\n");
    		
    	    JScrollPane MouseEventPane = new JScrollPane(AreaEventiMouse);
    	    
            add(MouseEventPane, BorderLayout.CENTER);
            
            JPanel buttonPanel = new JPanel();
            buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
            
            setHookBtn = new JButton("Set Mouse Hook");
            setHookBtn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				setMouseHook();
    			}} );
            
            removeHookBtn = new JButton("Remove Mouse Hook");
            removeHookBtn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				unsetMouseHook();
    			}} );
            removeHookBtn.setEnabled(false);
            buttonPanel.add(setHookBtn);	        
            buttonPanel.add(removeHookBtn);	     
            add(buttonPanel, BorderLayout.SOUTH);
    	}
    	
    	private void setMouseHook() {
    		setHookBtn.setEnabled(false);
    		removeHookBtn.setEnabled(true);
    		
    		// This hook is called in the context of the thread that installed it. 
    		// The call is made by sending a message to the thread that installed the hook.
    		// Therefore, the thread that installed the hook must have a message loop. 
    		//
    		// We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump
    		// nor do we want the main thread to be stuck in running a message pump
    		Thread hookThread = new Thread(new Runnable(){
    
    			public void run() {
    				if (MouseProc.hookHandle == 0) {
    					int hInstance = Kernel32.GetModuleHandle(null);
    								
    					MouseProc.hookHandle = SetWindowsHookEx(WH_MOUSE_LL, 
    									new Callback(MouseProc.class, "lowLevelMouseProc"), 
    									hInstance, 
    									0);
    					
    					
    					// Standard message dispatch loop (message pump)
    					Msg msg = new Msg();
    					while (User32.GetMessage(msg, 0, 0, 0)) {
    						User32.TranslateMessage(msg);
    						User32.DispatchMessage(msg);
    					}
    					
    				} else {
    					AreaEventiMouse.append("Hook già installato.\n");
    				}
    			}});
    		hookThread.start();
     	}
    
    	private void unsetMouseHook() {
    		setHookBtn.setEnabled(true);
    		removeHookBtn.setEnabled(false);
    		UnhookWindowsHookEx(MouseProc.hookHandle);
    		MouseProc.hookHandle = 0;
    	}
    	
    	
    	private static void createAndShowGUI() {
            //Create and set up the window.
            frame = new JFrame("Mouse Hook");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            MouseHook MouseEventsWindow = new MouseHook();
            MouseEventsWindow.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            //Add content to the window.
            frame.add(MouseEventsWindow, BorderLayout.CENTER);
       
            //Display the window.
            frame.pack();
             
            frame.setBounds(300, 200, 750, 600);
            frame.setVisible(true);
        }
    	
    	public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    
    	}
    }
    		
    class MouseProc {
    	static int hookHandle;
    	
    	@NativeImport(library = "user32")
    	public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam);
    	
    	static {
    		JInvoke.initialize();
    	}	
    	
    	public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) {
    		if (nCode < 0)
    			return CallNextHookEx(hookHandle, nCode, wParam, lParam);
    		
    		
    		  if (nCode == HC_ACTION) {
    			  MSLLHOOKSTRUCT mInfo = Util.ptrToStruct(lParam, MSLLHOOKSTRUCT.class);
    		      String message = "Mouse pt: (" + mInfo.pt.x + ", " + mInfo.pt.y + ") ";
    		      switch (wParam) {
    		      
    		      case WM_LBUTTONDOWN:
    		    	  message += "Bottone sinistro giù";
    		    	  break;
    		      case WM_LBUTTONUP:
    		    	  message += "Bottone sisnistro sù";
    		    	  break;
    		      case WM_MOUSEMOVE:
    		    	  message += "Mouse mosso";
    		    	  break;
    		      case WM_MOUSEWHEEL:
    		    	  message += "rotellina mouse girata";
    		    	  break;
    		      case WM_RBUTTONDOWN:
    		    	  message += "Bottone destro giu";
    		    	  break;
    		      case WM_RBUTTONUP:
    		    	  message += "Bottone sinistro giu";
    		    	  break;
    		      }
    		      System.out.println(message); 
    		      MouseHook.AreaEventiMouse.append(message+"\n");
    		    }
    		  
    		return CallNextHookEx(hookHandle, nCode, wParam, lParam);
    	}
    }
    codice:
     
    
    
    package MouseHook;
    
    import com.jinvoke.NativeStruct;
    import com.jinvoke.win32.structs.Point;
    
    @NativeStruct
    public class MSLLHOOKSTRUCT {
    	public Point pt = new Point();
            public int mouseData;
    	public int flags;
    	public int time;
    	public int dwExtraInfo;
            
    }
    non ho capito dove devo agire

  6. #16
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    131
    Nella lowLevelMouseProc ovviamente.

    Intanto metti una variabile statica che non si trovi nello stack della funzione lowLevelMouseProc, prima di installare l'hook la inizializzi con il valore che ricevi dalla GetCursorInfo.

    Poi nella lowLevelMouseProc richiami la GetCursorInfo e controlli se hCursor è diverso dal valore presente nella variabile statica.

  7. #17
    le tue parole sono state oro colato

  8. #18
    scusa mi secca darti il tormento, ma non è che potresti indicarmi come modificare il codice?

  9. #19
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    131
    cosa c'è che non riesci a fare? hai provato? non funziona? posta il codice che hai inserito.
    l'hai scritto tu il codice dell'hook? mi sempre strano che trovi difficoltà a inserire una chiamata a funzione e un controllo.

  10. #20
    non riesco a trovare getcursorinfo ma solo get cursor
    e sono ai primi approcci con la programmazione.
    per questo ti chiedevo se riuscivi a indirizzarmi in maniera corretta tu

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.