2012-10-21 135 views
5

私は、非アクティブなウィンドウ/ process/programm(win32/64)にいくつかのキーをpythonを使って送信しようとしています。すでにpywinautoとSendKeysについて読んでいますが、どちらもキーを送信する前にウィンドウをアクティブにしています。いくつかのキーをPythonで非アクティブなウィンドウに送信

アクティブでないウィンドウで作業する方法はありますか?

誰かが単純な例/スニペットを投稿するといいですね。

ありがとうございました。

+2

これは役立つかもしれない:http://stackoverflow.com/q/5080777/1129194 –

+0

うわー、私はwin32api.SendMessageに関する詳細な情報を検索します、あなたの例では、ワーキンのように見える)=ここに任意のコメントを逃したRLYメソッド。どうも! – killradio

答えて

3

これは本当に古いポストですが、ここに答えがなかったです、私はこれとまったく同じものを探していました。 Stackoverflowを経るのに6時間を費やしていたので、もっと便利だったので、すべてのCのドキュメントを読むだけでした。

<python> 
#you will need the win32 libraries for this snippet of code to work, Links below 
import win32gui 
import win32con 
import win32api 
from time import sleep 

#[hwnd] No matter what people tell you, this is the handle meaning unique ID, 
#["Notepad"] This is the application main/parent name, an easy way to check for examples is in Task Manager 
#["test - Notepad"] This is the application sub/child name, an easy way to check for examples is in Task Manager clicking dropdown arrow 
#hwndMain = win32gui.FindWindow("Notepad", "test - Notepad") this returns the main/parent Unique ID 
hwndMain = win32gui.FindWindow("Notepad", "test - Notepad") 

#["hwndMain"] this is the main/parent Unique ID used to get the sub/child Unique ID 
#[win32con.GW_CHILD] I havent tested it full, but this DOES get a sub/child Unique ID, if there are multiple you'd have too loop through it, or look for other documention, or i may edit this at some point ;) 
#hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD) this returns the sub/child Unique ID 
hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD) 

#print(hwndMain) #you can use this to see main/parent Unique ID 
#print(hwndChild) #you can use this to see sub/child Unique ID 

#While(True) Will always run and continue to run indefinitely 
while(True): 
    #[hwndChild] this is the Unique ID of the sub/child application/proccess 
    #[win32con.WM_CHAR] This sets what PostMessage Expects for input theres KeyDown and KeyUp as well 
    #[0x44] hex code for D 
    #[0]No clue, good luck! 
    #temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x44, 0) returns key sent 
    temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x44, 0) 

    #print(temp) prints the returned value of temp, into the console 
    print(temp) 
    #sleep(1) this waits 1 second before looping through again 
    sleep(1) 
</python> 

私は

hwndEdit = win32gui.FindWindowEx(hwndMain, hwndChild, "Edit", "test - Notepad"); 

を使用するすべての上の記事を見てきましたが、私はそれを把握することができませんでした。それに加えて、Microsoftのサイト上のすべてのドキュメントは曖昧で、私はそれを理解する方法を自分自身で追加しました。

これは、あなたを始めてくれるはずであり、他の人にとって役立つはずです。他の誰かが改訂を知っていれば私に知らせてください。

Win32 Python Library

+0

あなたは命を救う人です!ありがとうございました! –

関連する問題