Skip to content

Instantly share code, notes, and snippets.

@doubleo2
Created August 8, 2012 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doubleo2/3298052 to your computer and use it in GitHub Desktop.
Save doubleo2/3298052 to your computer and use it in GitHub Desktop.
Google's basketball doodle on Linux
#!/usr/bin/env python
# Based on code from
# pykey -- a Python version of crikey,
# http://shallowsky.com/software/crikey
# Simulate keypresses under X11.
# and
# https://gist.github.com/3295480
#
# Portions of this software are copyright 2008 by Akkana Peck.
# Please share and re-use this under the terms of the GPLv2
# or, at your option, any later GPL version.
import Xlib.display
import Xlib.X
import Xlib.XK
import Xlib.protocol.event
UseXTest = False
display = None
window = None
try :
import Xlib.ext.xtest
except ImportError:
UseXTest = False
print "no XTest extension; using XSendEvent"
import sys, time
KEYCODE = {
'spacebar': 65
}
def press_key(keycode, delay=0) :
global window
if (UseXTest) :
sys.stdout.write(".")
sys.stdout.flush()
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode)
display.sync()
time.sleep(delay)
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode)
display.sync()
else :
event = Xlib.protocol.event.KeyPress(
time = int(time.time()),
root = display.screen().root,
window = window,
same_screen = 0, child = Xlib.X.NONE,
root_x = 0, root_y = 0, event_x = 0, event_y = 0,
state = 0,
detail = keycode
)
window.send_event(event, propagate = True)
display.sync()
time.sleep(delay)
event = Xlib.protocol.event.KeyRelease(
time = int(time.time()),
root = display.screen().root,
window = window,
same_screen = 0, child = Xlib.X.NONE,
root_x = 0, root_y = 0, event_x = 0, event_y = 0,
state = 0,
detail = keycode
)
window.send_event(event, propagate = True)
display.sync()
def play():
global display, UseXTest, window
time.sleep(2)
s = time.time()
t,c = 0.5,0
display = Xlib.display.Display()
#window = display.get_input_focus()._data["focus"];
window = display.get_input_focus().focus;
wmname = window.get_wm_name()
wmclass = window.get_wm_class()
if wmclass is None and wmname is None:
window = window.query_tree().parent
wmname = window.get_wm_name()
print wmname
if UseXTest and not display.query_extension("XTEST") :
UseXTest = False
while time.time() - s < 25:
press_key(KEYCODE['spacebar'],t)
time.sleep(0.5)
c +=1
if c == 1 :
t = 0.6
if c == 5 :
t = 0.9
if c == 11 :
t = 1.1
if c == 17 :
t = 1.4
sys.stdout.write("\n")
sys.stdout.flush()
play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment