需要将按键发送到网页(html5游戏) [英] Need to send key presses to a webpage(html5 game)

查看:102
本文介绍了需要将按键发送到网页(html5游戏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试构建一个机器人,用于试验A.I的网页。有问题的网页是一个游戏(HTML5)。



我想将密钥(上,下,左,右,空格)发送到外部打开的网页来控制游戏中的机器人。



我查看了机械化,但我觉得它是为表格和东西构建的。



顺便说一下,我正在服用AI现在,好奇心。
任何帮助将不胜感激。谢谢。

解决方案

根据你想要的方式,并假设你在Windows上,指南这里建议您只使用 Python Imaging Library (用于阅读屏幕), Numpy PyWin (实际点击游戏)。可以使用Win64的等效包此处



直接回答你的问题:

  import win32api,win32con 

def leftClick():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
打印点击。 #completely可选。但很适合调试目的。

Selenium 也提供类似于Mechanize的东西,因为你可以控制浏览器,虽然它必须由Selenium本身启动,它不能只是挂钩到当前正在运行的进程。您可以将点击事件直接发送到HTML元素,这可能正是您在这里寻找的。

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.

解决方案

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.

这篇关于需要将按键发送到网页(html5游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆