简体   繁体   中英

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

github link

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?

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(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

Using Java applets you can add a text listener to your component and capture the 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);
                    }
                }
                );
            }

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM