Wednesday, March 18, 2015

python with win32 api

ref: http://sourceforge.net/projects/pywin32/

import win32com.client
import win32api
import win32con
import time

def IsPressed(key):
    # ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx
    return win32api.GetKeyState(key) & 0x8000 == 0x8000

exit = False;
# for tracking key state
prevSpace = 0;
prevUp = 0;
currentSpace = 0;
currentUp = 0;

# loop to track key state
while (exit == False):
    currentSpace = IsPressed(win32con.VK_SPACE);
    currentUp = IsPressed(win32con.VK_UP);
    if IsPressed(win32con.VK_ESCAPE):
       exit = True;
       print "Escape"
    prevSpace = currentSpace;
    prevUp = currentUp;
    time.sleep(0.0001)


# if need to send key to another app
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("app to activate or send key to ...")
shell.SendKeys("whatever key to send", 0)

No comments: