Results 1 to 10 of 10

Thread: Mouse Right or Left click ???

  1. #1
    Join Date
    Aug 2011
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Lightbulb Mouse Right or Left click ???

    Dear Friends,

    How do I perform mouse Right or Left click through code ???
    Been scratching my head from the last two days but cannot seem to get through this

    all I am trying to do is when a button is clicked then the mouse Right Click should happen:

    Qt Code:
    1. void Widget::on_pushButton_6_clicked()
    2. {
    3. QPoint point(cur.pos().x(), cur.pos().y());
    4.  
    5. QApplication::postEvent(QApplication::desktop()->window(), new QMouseEvent(QMouseEvent::MouseButtonPress,point,Qt::RightButton, Qt::RightButton,Qt::NoModifier));
    6.  
    7. QApplication::postEvent(QApplication::desktop()->window(), new QMouseEvent(QMouseEvent::MouseButtonRelease,point,Qt::RightButton, Qt::RightButton,Qt::NoModifier));
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    First I get the current cursor position and then I am trying to perform a Right Click event...but nothing happens
    Last edited by anjanu; 26th August 2011 at 20:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Mouse Right or Left click ???

    first, how deletes the event in your code? and is window() returning a valid pointer and is the point value correct for window()?

  3. #3
    Join Date
    Aug 2011
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mouse Right or Left click ???

    Thanks Lykurg

    Now I have changed my code a bit to this:

    Qt Code:
    1. void Widget::on_pushButton_6_clicked()
    2. {
    3. QTimer::singleShot(4000, this, SLOT(clickAction()));
    4. }
    5.  
    6. void Widget::clickAction()
    7. {
    8. QObject *desk = QApplication::activeWindow();
    9. QPoint point(cur.pos().x(), cur.pos().y());
    10. ui->plainTextEdit->appendPlainText("X:" + QString::number(cur.pos().x()) + " Y:" + QString::number(cur.pos().y()));
    11. QApplication::postEvent(desk, new QMouseEvent(QMouseEvent::MouseButtonPress,point,Qt::RightButton, Qt::RightButton,Qt::NoModifier));
    12. QApplication::postEvent(desk, new QMouseEvent(QMouseEvent::MouseButtonRelease,point,Qt::RightButton, Qt::RightButton,Qt::NoModifier));
    13. }
    To copy to clipboard, switch view to plain text mode 

    as you can see that the Window() is the current active window.
    When I click the button which creates the timer for 5 sec, I quickly point and click the curson on the Desktop to make it the current window and after the timer timeouts I expect an Right Click event...but I get the correct co-ordinates (X, Y) value but still not Right Click happens...pls help

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse Right or Left click ???

    Why do you want to mimic mouse events? Sending events to widgets outside your application won't work this way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2011
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mouse Right or Left click ???

    hi Wysota,
    Actually I am trying to control the mouse cursor remotely...I can easily move the mouse cursor but cannot simulate click events...all this was very easy in java using Robot API.

    and friend do you mean sending events outside the application won't work...I mean is't it possible in Qt ???
    can you recommend me reading some this so that I can have better understanding...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse Right or Left click ???

    It's possible but not using Qt's event API. You have to do that using native API, Qt events can't be translated to system events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2011
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mouse Right or Left click ???

    do you mean the native OS(MS Windows and Linux) APIs...

    if yes how can I begin with them ???

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse Right or Left click ???

    Quote Originally Posted by anjanu View Post
    do you mean the native OS(MS Windows and Linux) APIs...
    Yes. But X11 rather than Linux.

    if yes how can I begin with them ???
    By getting familiar with their docs, probably
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    anjanu (27th August 2011)

  10. #9
    Join Date
    Aug 2011
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mouse Right or Left click ???

    I dont get this...do I need to use Visual C++ APIs for windows ???
    win32API, windows.h...what do I need to do ???


    Added after 52 minutes:


    friends I finally got it working...here's what I did:

    include the header file "windows.h"
    and use mouse_event(5 param);

    here's what I did for Right Click

    Qt Code:
    1. mouse_event(MOUSEEVENTF_RIGHTDOWN,100, 200, 0,0);
    2. mouse_event(MOUSEEVENTF_RIGHTUP, 100, 200, 0,0);
    To copy to clipboard, switch view to plain text mode 
    Last edited by anjanu; 27th August 2011 at 17:54.

  11. The following user says thank you to anjanu for this useful post:

    hakermania (27th August 2011)

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse Right or Left click ???

    Quote Originally Posted by anjanu View Post
    I dont get this...do I need to use Visual C++ APIs for windows ???
    There is no such thing as "Visual C++ API". There is only WinAPI and it is C-based.

    Congrats that you made it work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 8
    Last Post: 2nd September 2010, 15:49
  2. Replies: 4
    Last Post: 29th August 2010, 18:16
  3. Qtablewidget left mouse click event in python
    By gerocampo in forum Newbie
    Replies: 0
    Last Post: 23rd July 2010, 17:05
  4. Replies: 4
    Last Post: 30th May 2010, 12:55
  5. Emulating Enter key with left mouse click
    By wconstan in forum Newbie
    Replies: 6
    Last Post: 30th December 2009, 16:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.