aidanrwt / twitch-plays

Twitch IRC bot that takes input from the chat and presses the corresponding key.
MIT License
275 stars 59 forks source link

Add an option to use PostMessage for minimized keystrokes #7

Closed Francesco149 closed 9 years ago

Francesco149 commented 10 years ago

Some games will register keystrokes from PostMessage which sends a keystroke only to a certain window, even if it's hidden or minimized. This could avoid using a VM to stream certain games. I used to use this to make MMO bots to keep using your PC even while you're botting.

c++ example:

HWND wnd = FindWindow(0, "VisualBoyAdvance");
UINT key = 0x31; // 1 key
PostMessage(wnd, WM_KEYDOWN, key, MapVirtualKey(key, 0) << 16);
Sleep(150);
PostMessage(wnd, WM_KEYUP, key, MapVirtualKey(key, 0) << 16);
tcsnyder commented 10 years ago

here is code a simple text entry system that will let you use the keyboard. I don't have time to integrate it into the git, but here is my sample.

import win32con
import win32api
import win32gui

def enumHandler(hwnd, lParam):
    if win32gui.IsWindowVisible(hwnd):
        if 'testwindowinfo' in win32gui.GetWindowText(hwnd):
            for c in "Hello World\n":
                win32api.PostMessage(
                                    hwnd, 
                                    win32con.WM_CHAR, 
                                    ord(c), 
                                    0)             

win32gui.EnumWindows(enumHandler, None)

This finds a window with the name 'testwindowinfo' in it and posts 'Hello World' to it

ynohtna92 commented 10 years ago

Why does it only work for certain games and not all? Is it an emulator limitation or a game issue?

That being said I will add this into my fork.

Francesco149 commented 10 years ago

I think it depends on how the game handles the input. For example, it doesn't work for VisualBoyAdvance (which also seems to have system-wide input, as it will register input from other windows even when minimized), but it works on MapleStory. I think it only works on games or engines that use the win32 message loop to handle input but I'm not entirely sure.

ynohtna92 commented 10 years ago

Your solution @tcsnyder in your fork produces errors.

      win32gui.EnumWindows(self.enumHandler, winlist)
    TypeError: enumHandler() takes exactly 2 arguments (3 given)
ynohtna92 commented 10 years ago

@Francesco149 @tcsnyder I have posted my solution but it doesn't seem to register a post message.

Francesco149 commented 10 years ago

It only works with games that use the Win32 message queue to process keystrokes. So far I've only used it on bots for MapleStory.