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: 413
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: 376
Location: State College, Pennsylvania

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
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