8

I'm writing a macro that will click certain spots on the screen when I press a key.

The first time I press a key, everything runs fine.
However, any other key press results in the error:

    time.sleep(0.1)
TypeError: an integer is required

Here is the code:

import win32api
import win32con
import time
import pythoncom
import pyHook
import os

def Click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def DeleteRun(event):
    Click(1250, 741)
    time.sleep(0.1)
    Click(649,261)
    time.sleep(0.1)
    Click(651, 348)
    time.sleep(0.1)
    Click(800, 442)
    time.sleep(0.1)
    Click(865, 612)


Click(20,20)
KeyGrabber = pyHook.HookManager()
KeyGrabber.KeyDown = DeleteRun
KeyGrabber.HookKeyboard()
pythoncom.PumpMessages()

It seems the first time the DeleteRun function is run by pyHook, time.sleep() accepts floats.
On any following function calls, it seems it only accepts integers.

What is causing this?
I can't wait 5 seconds for the mouse arrangement! It's supposed to save time!

Specs:

  • python 2.7.2
  • Windows 7 (32)
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
Anti Earth
  • 4,671
  • 13
  • 52
  • 83
  • 1
    Can you do `import time as time_safe` and use `time_safe` in place of `time`? Maybe pyHook is doing something with the `time` class. – Blender Mar 18 '12 at 21:47
  • No success I'm afraid. I've noticed however that it alternates working and failing. – Anti Earth Mar 18 '12 at 21:50
  • Maybe try specifically `from time import sleep`? – Karl Knechtel Mar 18 '12 at 21:56
  • try: `import time, functools` before any other module and use `Sleep = functools.partial(time.sleep, 0.1)` and later in the `DeleteRun(event): ... Sleep() ...` – jfs Mar 18 '12 at 22:00
  • Both of these: no luck. " sleep() TypeError: an integer is required" – Anti Earth Mar 18 '12 at 22:08
  • Where in your stack are you explicitly getting this error? It's possible that you're being thrown by the invocation of `time.sleep` in a different portion of the code. – MrGomez Mar 18 '12 at 22:12

2 Answers2

5

Okay, how about this? Add a return True to DeleteRun:

def DeleteRun(event):
    Click(1250, 741)
    time.sleep(0.1)
    [...]
    return True

I should probably confess that this was little more than google-fu: read the answer to this question.

Community
  • 1
  • 1
DSM
  • 342,061
  • 65
  • 592
  • 494
-1

I'm not sure what's wrong with Windows this time, but you could try faking a high-precision sleep using select() with no file descriptors.

user1277476
  • 2,871
  • 12
  • 10