GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Move Cursor with Arrow Keys using GTK+/GDK

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 514
Location: Portland, OR USA

PostPosted: Fri Jan 12, 2007 7:39 pm    Post subject: Move Cursor with Arrow Keys using GTK+/GDK Reply with quote

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: (Plaintext)
1
gcc -Wall -g `pkg-config --cflags --libs gtk+-2.0` -o pointer pointer.c


Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*  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 */
}
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 385
Location: Fairfax, Virginia

PostPosted: Fri Jan 12, 2007 9:48 pm    Post subject: Reply with quote

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.
Back to top
jhonnyamil



Joined: 23 Jun 2008
Posts: 2

PostPosted: Mon Jun 23, 2008 1:41 pm    Post subject: Set and Get Cursor Position? Reply with quote

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!
Back to top
Vadi
GTK+ Geek


Joined: 28 May 2008
Posts: 68

PostPosted: Mon Jun 23, 2008 9:55 pm    Post subject: Reply with quote

I'm just curious... what is the practical application of this? I didn't quite find the usefulness of the window manager thing.
Back to top
jhonnyamil



Joined: 23 Jun 2008
Posts: 2

PostPosted: Wed Jun 25, 2008 2:17 pm    Post subject: For My Disability Reply with quote

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP