SendKeys を使用して別のアプリケーションにキーストロークを送信する Python プログラムがあります。ただし、一部のキーストロークは、アプリケーションが何らかの処理を行った後にアプリケーションに送信する必要があります (不明な時間がかかります)。これまでのところ、Alt+Tabb キーを押して DOS ウィンドウに戻り、Enter キーを押して、処理が終了したことを Python アプリケーションに知らせる必要がありました。DOS ウィンドウに戻らなくても、Python プログラムに続行するように通知する受信側アプリケーションでヒットできるキーの組み合わせ (Shift+F1 など) が必要です。フォーカスが別のウィンドウにある場合でも、Python でキーストロークを検出できるようにするにはどうすればよいですか?
1030 次
1 に答える
0
pyHookを見てください。
キーボードフックが可能です。
import pythoncom, pyHook
def OnKeyboardEvent(event):
print 'MessageName:',event.MessageName
print 'Message:',event.Message
print 'Time:',event.Time
print 'Window:',event.Window
print 'WindowName:',event.WindowName
print 'Ascii:', event.Ascii, chr(event.Ascii)
print 'Key:', event.Key
print 'KeyID:', event.KeyID
print 'ScanCode:', event.ScanCode
print 'Extended:', event.Extended
print 'Injected:', event.Injected
print 'Alt', event.Alt
print 'Transition', event.Transition
print '---'
# return True to pass the event to other handlers
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
于 2010-01-20T01:40:28.400 に答える