GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 29, 2014 2:23 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Move Cursor with Arrow Keys using GTK+/GDK
PostPosted: Fri Jan 12, 2007 7:39 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
This little example moves the mouse cursor on the default display/screen when the keyboard arrow keys are pressed in the GTK window (the window has to have focus but the cursor will move beyond the window).

I was just curious so I tinkered. Please, if anybody has any warnings, comments, please post (Andrew?).

Compile using:
Code:
gcc -Wall -g `pkg-config --cflags --libs gtk+-2.0` -o pointer pointer.c


Code:
/*  main.c
*  Micah Carrick <email@micahcarrick.com>
*
*  This file is just a little test program to move the cursor when the up,
*  down, left, or right arrows are pressed in a GtkWindow (the GtkWindow has
*  focus) using GDK functions.
*
*  NOTES:
*
*  This program was created as a very simple, quick test. It is not a complete
*  solution.
*
*  This program uses defines from gdkkeysyms.h which is not included from gtk.h
*  and must be explicity included.
*
*  For simplicity, this function assumes default display/screen.
*
*/
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

/* prototypes */
gboolean
on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data);

int main (int argc, char *argv[])
{
    GtkWidget *window;
 
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   
    /* connect signal callbacks */
    g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect (G_OBJECT (window), "key_press_event",
                    G_CALLBACK (on_key_press), NULL);
                   
    gtk_widget_show_all (window);
   
    gtk_main ();
 
    return 0;
}

gboolean
on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
   
    gint x=0, y=0, xadd=0, yadd=0;
    GdkDisplay *display = NULL;
    GdkScreen *screen = NULL;
   
    /* check for up/down/left/right arrow key press */
    switch (event->keyval)
    {
        case GDK_Left:
            xadd -= 5;
            break;
        case GDK_Right:
            xadd += 5;
            break;
        case GDK_Up:
            yadd -= 5;
            break;
        case GDK_Down:
            yadd += 5;
            break;   
        default:
            return FALSE; /* propogate event */
    }
   
    /* get default display and screen */
    display = gdk_display_get_default ();
    screen = gdk_display_get_default_screen (display);
   
    /* get cursor position */
    gdk_display_get_pointer (display, NULL, &x, &y, NULL);
   
    /* set new cusor position */
    x += xadd;
    y += yadd;
    gdk_display_warp_pointer (display, screen, x, y);
   
    return FALSE; /* propogate event */
}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 9:48 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
This is actually a really cool little app. It would be cool to have this automatically start up if there is no mouse present. You could just have it applied to the actually WM where something like Alt+ArrowKey would scroll it. You could also add the acceleration factor for when the key is held down. Or, if the key is single clicked & held down, accelerate slowly, but if it is double-clicked, accelerate faster. This would be great to have as a little app nevertheless though if you could get it to work whether the keyboard has focus or not. (That would have to be at the level of the WM I believe.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: Set and Get Cursor Position?
PostPosted: Mon Jun 23, 2008 1:41 pm 
Offline

Joined: Mon Jun 23, 2008 1:23 pm
Posts: 2
Hi!

Thanks for your post, it has given me some ideas of about how to port a little application that I have in Win32 API. I am trying to create a program that moves the mouse pointer 10 pixels to the right, but at the same time I want to be able to move around the screen while the mouse is moving to the right. So, from the new position of the mouse, it will still move to the right.
This is the code I have so far:

-----Start code-----

gdk_display_get_pointer (display, NULL, &x, &y, NULL);

newX = x;
newY = y;

newX += 10;


gdk_display_warp_pointer (display, screen, newX, newY);

-----End code-----

Indeed, it moves the mouse 10 pixels right from where the mouse position is. But the problem is that if I try to move the mouse to another place on the screen, it will come back to where the mouse was originally.
I want to be able to moves the mouse around, and it stills move 10 pixels right.

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 9:55 pm 
Offline
GTK+ Geek

Joined: Wed May 28, 2008 5:49 pm
Posts: 66
I'm just curious... what is the practical application of this? I didn't quite find the usefulness of the window manager thing.


Top
 Profile  
 
 Post subject: For My Disability
PostPosted: Wed Jun 25, 2008 2:17 pm 
Offline

Joined: Mon Jun 23, 2008 1:23 pm
Posts: 2
Hi!

I have a reading disability. My problem is that I cannot read well any kind of print. My eyes move too fast, and I cannot control them. I have done the program on Windows, but I try to make the corresponding GTK+ function calls, but it does not work.
What I want to do is to make the mouse pointer moves to the right, this will be like a guide for my eyes. Also, I want to be able to move the mouse pointer around to another line of text. But, I will still move rightward.

Here is my code on Windows:
Code:
int x, y;
POINT pos;

while (TRUE)
{
     GetCursorPos (&pos) ;
     x = pos.x ;
     y = pos.y ;
     if (x>=0 && y >=0 && x<100 && y<100)
          return ;

     x += 10;
     SetCursorPos (x, y) ;

     GetCursorPos (&pos) ;
     x = pos.x;
     y = pos.y;
     SetCursorPos (x, y);
     Sleep (270) ;
}


I try to make the same logic, but calling the corresponding functions on GTK+, it does not work. The mouse pointer moves to the right, but it does not allow to go to another position by moving the physical mouse.

If you have any question, please, let me know. I really want to implement this simple program on Gnome. I would like to read under Linux.

Thanks in advance![/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group