Re: generating keyboard events



I gave up trying to fake keyboard events in gdk. Since I've seen (and
asked) several questions on the mailing list about how to do this, but
no good answers, I'll post what I did do.

The approach I took was to emit fake events at the X level using
XTestFakeKeyEvent (X11/extensions/XTest.h).

What follows are some good references for anyone wishing to also do
this.

Sources of information:
http://zgp.org/linux-elitists/20021114022353 GA1934 zgp org html
http://www.mail-archive.com/fvwm hpc uh edu/msg01118.html

OOS Projects using:
http://hoopajoo.net/projects/xautomation.html (good example)
http://savannah.nongnu.org/projects/swinput
http://www.sandklef.com//software/xbut/download.html
http://www.gnu.org/software/xnee/www/index.html


Thanks,

PG


On Tue, 2003-11-04 at 09:28, Peter Gasper wrote:
Ok so I've worked on it a little more but still no luck. Does anyone
have a working example they can point to? Thanks.

test()
{
gint n = 0;
GdkKeymapKey **keys = new GdkKeymapKey*[100];
                      
// generate press event
GdkEvent *event = new GdkEvent();
event->type = GDK_KEY_PRESS;
                      
// init any fields
GdkEventAny *tmp = (GdkEventAny*)event;
tmp->type = GDK_KEY_PRESS;
tmp->window = gdk_get_default_root_window();
tmp->send_event = FALSE;
                      
GdkEventKey *tmpkey = (GdkEventKey*)event;
tmpkey->type = GDK_KEY_PRESS;
tmpkey->window = tmp->window;
tmpkey->send_event = FALSE;
tmpkey->time = GDK_CURRENT_TIME;
tmpkey->state = 0;
tmpkey->keyval = GDK_KP_Enter;
tmpkey->length = 0;
tmpkey->string = NULL;
                      
if( gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), 
                                      GDK_KP_Enter, keys, &n ) )
{
      tmpkey->hardware_keycode = keys[0]->keycode;
      tmpkey->group = keys[0]->group;
}
else // failed - no combination to get that.
{ 
      delete event;
      g_free(keys);
      return;
}

// post our event
gdk_event_put(event);
gdk_flush(); // don't know if need this, didn't make any difference.
                      
//setup release
event->type = GDK_KEY_RELEASE;
tmp->type = GDK_KEY_RELEASE;
tmpkey->type = GDK_KEY_RELEASE;
tmpkey->state = GDK_RELEASE_MASK;
                      
// post our event
gdk_event_put(event);
gdk_flush();
                      
delete event;
g_free(keys);
}
              

On Sat, 2003-11-01 at 19:38, Peter Gasper wrote:
I'm trying to generate keyboard events using gdk_event_put() in gtkmm.
The following code compiles and runs but does not generate any events.
Does anyone know of some example code that does this or what am I doing
wrong? 

Also, is there a good way to find the hardware_keycodes for a given key
or keyval?

Thanks.


// generate press event
GdkEvent *event = new GdkEvent();
event->type = GDK_KEY_PRESS;

// init any fields
GdkEventAny *tmp = (GdkEventAny*)event;
tmp->type = GDK_KEY_PRESS;
tmp->window = gdk_get_default_root_window();
tmp->send_event = FALSE;

GdkEventKey *tmpkey = (GdkEventKey*)event;
tmpkey->type = GDK_KEY_PRESS;
tmpkey->window = gdk_get_default_root_window();
tmpkey->send_event = FALSE;
tmpkey->time = GDK_CURRENT_TIME;
tmpkey->state = 0;
tmpkey->keyval = GDK_Left;
tmpkey->length = 0;
tmpkey->string = NULL;
tmpkey->hardware_keycode = 102; // hardcoded arrow key
tmpkey->group = 0;

// post our event
gdk_event_put(event);

//setup key release
event->type = GDK_KEY_RELEASE;
tmp->type = GDK_KEY_RELEASE;
tmpkey->type = GDK_KEY_RELEASE;
tmpkey->state = GDK_RELEASE_MASK;

// post our event
gdk_event_put(event);

delete event;
-- 
Peter Gasper <pgasper designadvantage com>




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