You are not logged in.

#1 2009-05-24 20:32:54

DarkLikeHell
Member
Registered: 2009-02-07
Posts: 71

Performing a mouse click using python

Is it possible?
I tried to use this script but it doesn't work:

#!/usr/bin/python
import Tkinter as tk

def change_color(event):
      btn1.config(fg='red')

root = tk.Tk()
btn1 = tk.Button(root, text='Click me with the right mouse button ...')
btn1.pack()
btn1.bind('<Button-1>', change_color)
root.mainloop()

Thanks.

Offline

#2 2009-05-24 22:42:02

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: Performing a mouse click using python

Are you trying to simulate a click to an external application? Your title suggests this but the code suggests the opposite, in which you want to act upon a mouse click. I'm not terribly familiar with Python and Tk, but Button-1 to me suggest the left mouse button as opposed to the right one, so that might be something to check.

Offline

#3 2009-05-25 08:45:03

DarkLikeHell
Member
Registered: 2009-02-07
Posts: 71

Re: Performing a mouse click using python

I'm trying to write an auto clicker script, so it needs to simulate an external click, just where the mouse is located.
I have found this code on the web. I have no idea how to do it.
I read that in linux I should use "Xlib" or "pygame", but I didn't find any helpful docs about this subject.
Thanks.

Offline

#4 2009-05-25 09:03:22

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,229
Website

Re: Performing a mouse click using python

I found a code example once, which was using Xlib. I'll see if I can dig it out from the depths of the interweb.

Offline

#5 2009-05-25 09:23:25

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: Performing a mouse click using python

Yeah you will most likely need to use some interface to Xlib along with the "XTEST" extension I _think_. I don't think there is any other way of simulating a click.

Offline

#6 2009-05-25 09:26:37

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,229
Website

Re: Performing a mouse click using python

I found something which is not the exact example I found three months ago, but have a look here in the middle of the thread: http://www.tuxisalive.com/tux-droid-for … /245877021

Use xev to find keycode and state.

Offline

#7 2009-05-25 10:15:32

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: Performing a mouse click using python

That link should prove to be useful, but if you get stuck with that you may be able to get away with calling "xte", or even taking a look at its source code and seeing how it does it smile xdotool seems capable of simulating clicks also

Offline

#8 2009-05-25 21:13:56

DarkLikeHell
Member
Registered: 2009-02-07
Posts: 71

Re: Performing a mouse click using python

xev doesn't show keycode for mouse, only for the keboard...
Or should I use "button"?
Thank you for helping

Offline

#9 2009-05-25 21:20:35

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Performing a mouse click using python

DarkLikeHell wrote:

xev doesn't show keycode for mouse, only for the keboard...

It does for me:

ButtonPress event, serial 35, synthetic NO, window 0x1800001,
    root 0x13c, subw 0x0, time 29072809, (63,102), root:(787,874),
    state 0x10, button 3, same_screen YES

Offline

#10 2009-05-25 21:45:29

DarkLikeHell
Member
Registered: 2009-02-07
Posts: 71

Re: Performing a mouse click using python

Never mind, i found this:
http://python-xlib.sourceforge.net/doc/ … ib_13.html
smile

Last edited by DarkLikeHell (2009-05-25 21:51:33)

Offline

#11 2009-05-25 22:01:16

DarkLikeHell
Member
Registered: 2009-02-07
Posts: 71

Re: Performing a mouse click using python

Ok, so thats what I did:

#!/usr/bin/env python
from Xlib.display import Display
from Xlib import X
from Xlib.protocol import event
import time

display = Display()
focus = display.get_input_focus().focus

keycode = 1
state = 0

keyevt = event.KeyPress(
    detail=keycode,
    time=X.CurrentTime,
    root=display.screen().root,
    window=focus,
    child=X.NONE,
    root_x=1,
    root_y=1,
    event_x=1,
    event_y=1,
    state=state,
    same_screen=1
    )
focus.send_event(keyevt)

Still doesn't work..

Offline

#12 2009-05-26 11:51:21

Shapeshifter
Member
Registered: 2008-03-11
Posts: 230

Re: Performing a mouse click using python

Why so complicated? Why don't you just use xdotool?
xdotool mousemove x y # to move the pointer
xdotool click 3 # to do a right click.
You can also call it from within python as a system command.

Last edited by Shapeshifter (2009-05-26 11:53:03)

Offline

#13 2009-05-26 12:28:21

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Performing a mouse click using python

DarkLikeHell wrote:

Ok, so thats what I did:
Still doesn't work..

You also need to have a short delay and send a button release event (or at least, that's what is in the C function I found which seems to work)

Offline

#14 2009-05-26 12:29:24

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Performing a mouse click using python

Shapeshifter wrote:

Why so complicated? Why don't you just use xdotool?
xdotool mousemove x y # to move the pointer
xdotool click 3 # to do a right click.
You can also call it from within python as a system command.

Maybe because not everybody has it installed, and having it as a dep for such a simple thing seems a bit silly.

Offline

#15 2009-05-26 12:43:40

DarkLikeHell
Member
Registered: 2009-02-07
Posts: 71

Re: Performing a mouse click using python

Yes, I would like to do it without deps.
Here:

#!/usr/bin/env python
from Xlib.display import Display
from Xlib import X
from Xlib.protocol import event
import time

display = Display()
focus = display.get_input_focus().focus

keycode = 1
state = 0

keyevt = event.KeyPress(
    detail=keycode,
    time=X.CurrentTime,
    root=display.screen().root,
    window=focus,
    child=X.NONE,
    root_x=1,
    root_y=1,
    event_x=1,
    event_y=1,
    state=state,
    same_screen=1)
    
state=1

keyevt2 = event.KeyPress(
    detail=keycode,
    time=X.CurrentTime,
    root=display.screen().root,
    window=focus,
    child=X.NONE,
    root_x=1,
    root_y=1,
    event_x=1,
    event_y=1,
    state=state,
    same_screen=1)
focus.send_event(keyevt)
focus.send_event(keyevt2)

I think the problem is with the "state".
I checked it with xev, and it tells that the state on press is 0x10 (16 decimal?) and on release is 0x110 (272 decimal?).
I tried using these values too, and again, not working.
In addition, X.CurrentTime returns 0, so I tried using int(time.time()), and still, not working.
Any suggestions?
Thanks smile

Offline

#16 2010-03-14 19:46:39

unexist
Member
From: Germany
Registered: 2008-05-13
Posts: 300
Website

Re: Performing a mouse click using python

Let's revive it, because I just had the same problem right now:

The solution is to calculate the coordinates of the window coordinates on the root window and either warp the mouse or use an enter event to this. You can ignore time and state of the is event and don't forget to flush afterwards:

event.type        = ButtonPress;
event.window      = win;
event.root        = DefaultRootWindow(display);
event.subwindow   = win;
event.button      = 1;
event.same_screen = True;
event.x           = 5;
event.y           = 5;

XTranslateCoordinates(display, event.window, event.root,
  event.x, event.y, &event.x_root, &event.y_root, &subwin);

XWarpPointer(display, None, event.root, 0, 0, 0, 0,
  event.x_root, event.y_root);

XSendEvent(display, win, True, ButtonPressMask, (XEvent *)&event);
XFlush(display);

usleep(12000);

event.type = ButtonRelease;

XSendEvent(display, win, True, ButtonReleaseMask, (XEvent *)&event);
XFlush(display);

Offline

Board footer

Powered by FluxBB