简体   繁体   English

如何通过代码(例如java)将按键输入发送到浏览器

[英]How to send key press input into a browser via code (e.g. java)

I plan on writing a solver to the recently popular 2048 game 我打算为最近流行的2048游戏写一个求解器

github link github链接

I'm wondering how I could go about this without actually building the game first then solving it... My question is: Is there a way I can send key presses (eg 'left' 'right' 'up' and 'down' ) into a web-browser via some sort of language like java/c? 我想知道如何在没有实际建立游戏然后解决它的情况下解决这个问题...我的问题是:有没有办法可以发送按键(例如'左''右''上'和'下' )通过某种语言如java / c进入Web浏览器?

Sorry if this question has been posted before, I was not sure how to actually phrase the question and could not find any results. 很抱歉,如果此问题之前已发布,我不确定如何实际说出问题,但找不到任何结果。

use keybd_event function to send key press, example : 使用keybd_event函数发送按键,例如:

keybd_event(VK_UP,0xE0,0,0);//do click, it will be stay pressed until you release it
keybd_event(VK_UP,0xE0,KEYEVENTF_KEYUP,0);//release click

the second parameter is scan code,there is a list of make and break scan codes for each key http://stanislavs.org/helppc/make_codes.html , and here you can find the virtual key codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx 第二个参数是扫描码,每个键http://stanislavs.org/helppc/make_codes.html都有一个make和break扫描码列表,在这里你可以找到虚拟键码http://msdn.microsoft .COM / EN-US /库/窗/桌面/ dd375731(v = vs.85)的.aspx

Using Java applets you can add a text listener to your component and capture the Keystrokes. 使用Java applet,您可以向组件添加文本侦听器并捕获Keystrokes。 For example, in the code below you are capturing the keystrokes of a textbox. 例如,在下面的代码中,您捕获文本框的击键。

import java.awt.event.*;
import java.awt.*;
import java.applet.*;


 public class KeyReader extends Applet{
    private static final long serialVersionUID = 1L;
    public void init(){
    TextField textBox = new TextField(" ");
    add(textBox);

    textBox.addKeyListener (new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    System.out.println("You Pressed " + keyCode);
                    }
                }
                );
            }

   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java将单键(例如“ k”)发送到Selenium WebDriver中的窗口? - How to send single key (e.g. 'k') to window in Selenium WebDriver with Java? Java-生成源代码的库,例如通过JavaScript从浏览器的常规源代码更改? - Java - library to generate source code changed e.g. by JavaScript from normal source code from browser? 如何在Java Swing中定义特殊键的快捷方式,例如德国变音键Ä? - How to define a shortcut for special keys in Java Swing, e.g. german umlaut key Ä? Java - 如何验证以下输入格式“操作数操作数”(例如 1.4 * 5) - Java - how to validate following input format 'operand operator operand' (e.g. 1.4 * 5) 服务器浏览器如何在游戏中工作? - How does a server browser e.g. in games work? Java中的程序代码修改(例如,变量提取) - Programatic code modification (e.g. variable extraction) in Java GWT如何为每个浏览器提供正确的Javascript代码,例如执行i18n和浏览器兼容性? - How does GWT provide the correct Javascript code to every browser e.g. to carry out i18n and browser compatibility? 如何将Java代码迁移到更新的jdk(例如1.8) - How to migrate Java code to a more recent jdk (e.g. 1.8) 如何使用eclipse中编写的Java代码在远程桌面中导航(例如,打开Mozilla)? - How can i navigate (e.g. open Mozilla) in a Remote Desktop by using java code written in eclipse? 如何在Java中查看内置类的源代码(例如BigInteger等)? - How do I view source code of built-in classes in Java (e.g. BigInteger etc.)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM