-1

everyone.

I was trying to learn winapi/pywin32, by remotely controlling some programs. So I decided to try something different and chose The Powder Toy to test. Then I took that answer as an example and wrote this (Also used Spy++ to determine some parameters):

import win32gui
import win32con
import win32api
from time import sleep

hwndMain = win32gui.FindWindow(None, "The Powder Toy")

while True:
    win32api.PostMessage(hwndMain, win32con.WM_KEYDOWN, 0x44, 2097153)
    win32api.PostMessage(hwndMain, win32con.WM_KEYUP, 0x44, 0)
    sleep(1)

It takes HWND, as it supposed to be, also sends a similar message to The Powder Toy's window, but it's not reacting for some reason. For example:

When I press "d":

...WM_KEYDOWN nVirtKey:'D'cRepeat:1 ScanCode:20fExtended:0fAltDown:0fRepeat:0fUp:0
...WM_CHAR chCharCode:'100'(100)cRepeat:1 ScanCode:20fExtended:0fAltDown:0fRepeat:0fUp:0
...WM_KEYUP nVirtKey:'D'cRepeat:1 ScanCode:20fExtended:0fAltDown:0fRepeat:1fUp:1

When I try to send "d":

...WM_KEYDOWN nVirtKey:'D'cRepeat:1 ScanCode:20fExtended:0fAltDown:0fRepeat:0fUp:0
...WM_CHAR chCharCode:'100'(100)cRepeat:1 ScanCode:20fExtended:0fAltDown:1fRepeat:0fUp:0
...WM_KEYUP nVirtKey:'D'cRepeat:1 ScanCode:00fExtended:0fAltDown:1fRepeat:0fUp:0
...WM_CHAR chCharCode:'100'(100)cRepeat:1 ScanCode:00fExtended:0fAltDown:1fRepeat:0fUp:0

Can someone explain why it's not working, and if it's possible, how to fix it?

PLSmA1
  • 23
  • 1
  • 4
  • Stop asking the same question all over again. [You can't simulate keyboard input with PostMessage](https://devblogs.microsoft.com/oldnewthing/20050530-11/?p=35513). – IInspectable Mar 12 '21 at 17:00
  • Thank you for your kind words, @IInspectable. I used PostMessage, because in that situation, both SendMessage and PostMessage had the same problem, same way. The problem was at the lParameter of them. It looks like I posted the wrong part of the code there, with right lParameters :) I have called win32con.WM_KEYUP with lParameter 0 on my original code, which caused some problems. Still, thank you. Your comment made me look at my code more carefully. I wouldn't be noticed the problem and the importance of lParameters without your comment :) – PLSmA1 Mar 12 '21 at 19:42

1 Answers1

0

It looks like I had to give the ScanCode to lParameters for WM_KEYUP, along with 0x44, to make The Powder Toy work.

PLSmA1
  • 23
  • 1
  • 4
  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Zeus Mar 15 '21 at 01:39