You are not logged in.
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
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
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
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
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
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
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 xdotool seems capable of simulating clicks also
Offline
xev doesn't show keycode for mouse, only for the keboard...
Or should I use "button"?
Thank you for helping
Offline
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
Never mind, i found this:
http://python-xlib.sourceforge.net/doc/ … ib_13.html
Last edited by DarkLikeHell (2009-05-25 21:51:33)
Offline
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
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
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
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
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
Offline
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