Need to send key presses to a webpage(html5 game)

爱⌒轻易说出口 提交于 2019-12-11 16:37:07

问题


Trying to build a bot for experimenting with A.I for a webpage. The webpage in question is a game(HTML5).

I want to send keys (up, down, left, right, space)to an externally opened webpage to control a bot in the game.

I looked into mechanize, but it feels to me that its constructed for forms and stuff.

BTW, i'm taking A.I. right now, hence the curiosity. Any help would be appreciated. Thank You.


回答1:


Depending on how you want to approach this, and assuming you're on Windows, the guide here suggests that you just use The Python Imaging Library (for reading the screen), Numpy and PyWin (for actually clicking on the game). The equivalent packages for Win64 may be available here

To directly answer your question though:

import win32api, win32con

def leftClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
    print "Click."          #completely optional. But nice for debugging purposes.

Selenium also offers something similiar to Mechanize in that you get control of a browser, although it has to be launched by Selenium itself, it can't just hook into a currently running process. You're able to send a click event directly to an HTML element, which might be just what you're looking for here.



来源:https://stackoverflow.com/questions/18699356/need-to-send-key-presses-to-a-webpagehtml5-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!