Adam
Adam

Reputation: 2334

What is wrong with my SendMessage/PostMessage part of code?

I have created simple script which try get equity computed by enter link description here:

import time
import win32api
import win32con

from pywinauto import application

def getEquity(ps_pid, hand1, hand2):
    def set_hand(handle, hand, kf=0):
        win32api.SendMessage(handle, win32con.WM_SETFOCUS, 0, 0) # f: losefocus
        #win32api.SendMessage(handle, win32con.WM_GETDLGCODE, 0, 0)
        time.sleep(0.05)
        len = win32api.SendMessage(handle, win32con.WM_GETTEXTLENGTH, 0, 0)
        time.sleep(0.05)
        win32api.SendMessage(handle, win32con.EM_SETSEL, 0, len)
        time.sleep(0.05)
        for c in hand:
            win32api.PostMessage(handle, win32con.WM_CHAR, ord(c), 0)
            #win32api.SendMessage(handle, win32con.WM_GETDLGCODE, 0, 0)
            time.sleep(0.05)
        win32api.SendMessage(handle, win32con.WM_KILLFOCUS, 0, 0)

    app = application.Application()
    app.connect_(process=ps_pid)

    set_hand(app.PokerStove.REdit1.handle, hand1)
    set_hand(app.PokerStove.REdit2.handle, hand2)

    app.PokerStove.Evaluate.Click()
    while app.PokerStove.EvaluateButton.WindowText() != 'Evaluate':
        time.sleep(0.1)

    return app.PokerStove.Edit12.GetLine(0)

import sys
print getEquity(int(sys.argv[1]), sys.argv[2], sys.argv[3])

I decided to use window messages instead of SendKey because I need to have it working also when PokerStove is minimized.

This script works fine when when PokerStove is minimized. But strange things happens when is not. Script correctly fills text edits and click button and I'm getting correct results. But after that change his caption to something strange:

PokerStove

So it looks like PokerStove is still computing, but results are ready. Because of this change when I start my script again it will fail. But when PokerStove is minimized I don't have this issue.

I suspect that I did something wrong with sending messages to edit boxes. Because if I fill them manually and click button then everything is fine. When I fill it using set_hand function, then even if I click button manually I will get this strange result.

So what is wrong with my script?

EDIT:

When I connect spy++ to EvaluateButton I can see that button still gets WM_SETTEXT message which set it to "Stop (99% Complete)".

EDIT2:

It was tested on Windows 7. But at home on Windows XP in VirtualBox code is working fine...

Upvotes: 1

Views: 1185

Answers (1)

Maximus
Maximus

Reputation: 10845

You send chars with PostMessage. This function is async. Hm, results of following SendMessage's may be strange.

Upvotes: 1

Related Questions