[Kde-bindings] Ruby, sendEvent and KDE

schampailler at easynet.be schampailler at easynet.be
Mon Oct 17 06:12:41 UTC 2005


Finally I did it with X11 FakeEvents... But if you know how to do it with 
KDE/Qt I'm still interested...

Here's the code (c binding)

#include <stdlib.h>
#include <stdio.h>

// cc test.c -L/usr/X11R6/lib -lX11 -lXext -lXtst 

// require'mouseClick'
// i = MouseClick::Control.new


#include <X11/Intrinsic.h>     /* Intrinsics Definitions*/
#include <X11/StringDefs.h>    /* Standard Name-String definitions*/
#include <X11/extensions/XTest.h>    /* Standard Name-String definitions*/

#include "ruby.h"

char *display_name;
Display *display;


// Define the Ruby Module
static VALUE rb_mMouseClick;

// Define the Ruby Class
static VALUE rb_cControl;


static VALUE t_mcControl_moveMouseTo( VALUE self, VALUE x, VALUE y) {
   XTestFakeMotionEvent(display, -1, NUM2UINT(x), NUM2UINT(y), 10) ;
   XFlush(display);
   return self;
}

static VALUE t_mcControl_leftClick( VALUE self) {
    XTestFakeButtonEvent(display, 1, True, 1);
    XTestFakeButtonEvent(display, 1, False, 50);
    XFlush(display);
    return self;
}


static VALUE t_mcControl_initialize( VALUE self) {
   return self;
}

void Init_mouseClick()
{ 
    int event_base;
    int error_base;
    int major_version = 0;
    int minor_version = 0;
    
    display = XOpenDisplay( display_name);
    
    if( display == NULL) {
    	printf("Cant open display\n");
    }
    
    if( True == XTestQueryExtension(display, &event_base, &error_base, 
&major_version, &minor_version) ) {
    	printf("XTest supported %d - %d\n",major_version, minor_version);
    }
    XSync(display, True);


  rb_mMouseClick = rb_define_module ("MouseClick");
  rb_cControl = rb_define_class_under (rb_mMouseClick, "Control", rb_cObject);
  
  rb_define_method( rb_cControl, "initialize", t_mcControl_initialize, 0);
  rb_define_method( rb_cControl, "moveMouseTo", t_mcControl_moveMouseTo, 2);
  rb_define_method( rb_cControl, "leftClick", t_mcControl_leftClick, 0);
  
}

int main (int argc, char **argv) 
{
  
	return 0;
}



> Dear All,
>
> I'm now facing a problem with sendEvent. The deal is this : I have a
> KDE::HTMLPart in a KDE::MainWindow that I use as a web browser. I'd
> like to simulate a mouse cursor move over on particular place on the
> view of the browser and a mouse click there. For example, open the
> www.kde.org and click on the "announcement" link.
>
> I've written a piece of code that so far can display the browser and
> move the mouse cursor on the link I want :
>
>         p = Qt::Point.new(30,30)
>         p_global = self.mapToGlobal(p)
>         Qt::Cursor.setPos( p_global)
>         KDE::Application::kApplication.processEvents()
>
> The next thing I want to do is the "click". So I do :
>
>         current_widget =
> KDE::Application::kApplication.widgetAt( p_global)
>         mouseEvent( current_widget, Qt::Event::MouseButtonPress, p,
> p_global, Qt::RightButton, 0)
>         mouseEvent( current_widget, Qt::Event::MouseButtonRelease, p,
> p_global, Qt::RightButton, 0)
>
>
> with
>
>     def mouseEvent( target, type, pos, global_pos,button, state)
>         res = Qt::Application::sendEvent( target,
>             Qt::MouseEvent.new( type, target.rect().center(), button,
> state))
>         puts "Cant send event to target" if !res
>     end
>
>
> problem is the sendEvent always returns false and the click never
> occurs.
>
> If I add an event filter I can see that the events I'm sending
> arrives to the widget.
>
> I've been coding ruby/qt/korundum for 2 days so far so I'm quite
> newbie... I'va also checked Qt Test Lib from TrollTech but I think I
> do things like them. The big difference is that to simplify my code
> (i.e. no good reason :)), all this code is part of the MainWindow
> object :
>
> class MainWindow < KDE::MainWindow
>
> which I use like this :
>
> a = KDE::Application.new()
> window = MainWindow.new( "Tutorial - p4", a )
> window.resize( 1200, 800 )
> a.setTopWidget( window)
> window.show
> a.exec
>
>
> help would be much appreciated !
>
> Stefan
>
> _______________________________________________
> Kde-bindings mailing list
> Kde-bindings at kde.org
> https://mail.kde.org/mailman/listinfo/kde-bindings



More information about the Kde-bindings mailing list