PDA

View Full Version : Python help..



Nikron
February 7th, 2007, 10:59 PM
I'm trying to create a python program that when executed, checks the last time it was executed and if the last time is either null or more than a second sends ALT + RIGHT to the computer as if it were coming from the keyboard.

Basically, I'm wondering how to send key events from a python program..

Thanks for any help

jblebrun
February 7th, 2007, 11:35 PM
Check out the python Xlib package
http://python-xlib.sourceforge.net/doc/html/python-xlib_14.html#SEC13

Nikron
February 8th, 2007, 03:20 AM
Oh burn, this is going to be a lot harder than I thought, namely because I really don't know Python. Thanks for the link

Nikron
February 8th, 2007, 04:22 AM
Ohh nice, figured out how to do it without connecting to the x server, or even making a class

import os
os.system("/usr/bin/xvkbd -xsendevent -text '\[Alt_L]\[Left]'")

Wybiral
February 8th, 2007, 04:32 AM
Classes at least are a good thing to know, just for future reference... If you don't know any OOP at all, at least learn to use classes... It helps a lot.

Nikron
February 9th, 2007, 01:47 AM
Classes at least are a good thing to know, just for future reference... If you don't know any OOP at all, at least learn to use classes... It helps a lot.

Well.. I kinda made it object oriented but not really, since it's such a small program...

It works though



import os
import time

class altLeftSticky:
read = open("/home/nikron/General/lasttime.txt",'r')

def sendAltLeft():
"""Sends ALT+LEFT to the X server"""
os.system("/usr/bin/xvkbd -xsendevent -text '\[Alt_L]\[Left]'")

def wtime():
return str(time.time())


last = float(read.read()) + 1

if time.time() > last:
sendAltLeft()
write = open("/home/nikron/General/lasttime.txt",'w')
write.write(wtime())
write.close()


My first python program