相关文章推荐
坏坏的茴香  ·  Graphics2D.setComposit ...·  1 年前    · 
想旅行的地瓜  ·  ImportError: ...·  1 年前    · 
冷冷的盒饭  ·  django中ajax ...·  1 年前    · 
逆袭的黑框眼镜  ·  c++ - reference to ...·  2 年前    · 
自信的酱肘子  ·  Pandas ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to send a keystroke to an inactive TeraTerm Window using Pywin32.

This answer led me to write this code:

import win32gui
import win32con
import win32api
hwndMain = win32gui.FindWindow("Tera Term VT", None)
print hwndMain
hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD)
win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x5b, 0)
hwndMain = win32gui.FindWindow("Tera Term VT", None) returns 0, it can't find the window. 

If I change "Tera Term VT" to "Notepad", I can happily send keystrokes to an active Notepad window all day long. So, why can't I get the TeraTerm window?

According to the ActiveState documentation:

PyHANDLE = FindWindow(ClassName, WindowName)

ClassName : PyResourceId Name or atom of window class to find, can be None
WindowName : string Title of window to find, can be None

So how can I get the correct ClassName to use?

I've tried just about every variation of Tera Term VT, escaping the spaces: "Tera\ Term\ VT", enclosing the whole in single quotes: "'Tera Term VT'", but nothing works. I've even tried using the name of the process:"ttermpro.exe", and included the child name in the string "COM11:115200baud - Tera Term VT" in my desperation, but nothing works.

Interestingly, this:

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Tera Term VT")
shell.SendKeys("\%i", 0)

works just fine, but brings the window to the foreground, which I don't wan't. The Tera Term VT string works fine in this instance though.

You should be able to to the same with

hwndMain = win32gui.FindWindow(None, "Tera Term VT")  

that is, swapping the arguments so that it also works based on the window title

If you want to work based on the window class name you could use a tool like Spy++ with its Finder Tool to target the Tera Term window and get its window class name from the properties

Alas, swapping the arguments does not help. I will investigate the Finder Tool. though. Thanks. – SiHa Aug 16, 2016 at 9:05 Then the title did not match exactly with "Tera Term VT" at that time I guess - maybe you can provide a screenshot of the window. Nevertheless, a solution based on the window class name is better anyway as the application may change the title at any time. – DAXaholic Aug 16, 2016 at 9:14 Thanks, I've learnt something today; Window Classes are new to me! Downloaded WinSpy++, and found that the Class is, in fact VTWin32, and this works perfectly. – SiHa Aug 16, 2016 at 9:17

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.