Re: How to capture mouse movements independend of a Window?



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Dec 29, 2006 at 09:01:57AM +0100, Andreas Stricker wrote:
I try to do something that is similar to some 3D projection plan
window rotating/shifting function: When a key is pressed, the relative
mouse movement should be recorded.

I'm primary interested if somebody solved a similar problem or got
an idea how to solve this.

OK, here is a rough sketch (tested). As soon as the pointer crosses the
window boundary (inward), it is grabbed (the app changes the cursor's
appearance to show it). WARNING! IT IS A TRAP! Once your pointer is
inside, you won't be let out. See to it that you have a console around
(a virtual console wth ctrl-alt-Fn will do) to kill the app. Keep out of
reach of children ;-)

+---------------------------------------------------------------- Makefile
| grab: grab.c
|       gcc -Wall -g $< -o $@ \
|               `pkg-config --cflags gtk+-2.0` \
|               `pkg-config --libs gtk+-2.0`
+----------------------------------------------------------------

+---------------------------------------------------------------- grab.c
| /* grab.c
|    test mouse grabs
| */
| #include <gtk/gtk.h>
| 
| static void destroy(GtkWidget *widget, gpointer udata)
| {
|   gtk_main_quit ();
| }
| 
| static gboolean entered(GtkWidget *widget, GdkEventCrossing *ev, gpointer udata)
| {
|   if(ev->type == GDK_ENTER_NOTIFY) {
|     GdkCursor *cursor = gdk_cursor_new(GDK_PIRATE);
|     gdk_pointer_grab(widget->window, FALSE, GDK_POINTER_MOTION_MASK,
|                      widget->window, cursor, ev->time);
|     gdk_cursor_unref(cursor); /* X has it now */
|     cursor = NULL;
|   }
|   else { /* leaving: won't happen */
|     gdk_pointer_ungrab(ev->time);
|   }
|   return FALSE;
| }
| 
| int main(int argc, char *argv[])
| {
|   GtkWidget *window;
|     
|   gtk_init (&argc, &argv);
|   
|   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|   g_signal_connect (G_OBJECT(window), "destroy",
|                     G_CALLBACK(destroy), NULL);
|   g_signal_connect (G_OBJECT(window), "enter-notify-event",
|                     G_CALLBACK(entered), NULL);
| 
|   gtk_widget_show_all(window);
|     
| 
|   gtk_main ();
|   /* in the current setting, this is never reached.
|      We put it there to remind us to be good citizens.
|      Oh well
|   */
|   gdk_pointer_ungrab(GDK_CURRENT_TIME);
|   return 0;
| }
+----------------------------------------------------------------

Enjoy
- -- tomÃs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFlPErBcgs9XrR2kYRAjgxAJ9m8Ax3uDhiAyg77TXbKndITtlSRgCeI4sU
NHQZE707PFJn+nGNiurluQA=
=Q8Zx
-----END PGP SIGNATURE-----




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]