Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game "Need for Speed Most Wanted" can't controlled by Java class Robot

Tags:

java

I wrote a bot for controlling racing game using Java Robot. The bot works well for Need For Speed Underground except for key "Down" (Up, Left, Right keys work very well). However my bot can't control Need For Speed Most Wanted.

The bot works fine, but the Game doesn't accept the simulated key events. I did some searching, and found the game is DirectX based. In DirectX the keyboard/mouse events are special. It seems that the game "asks" the keyboard directly, not through Windows. And I try my program in CS, and found it works pretty well.

I program in Windows 7, using Eclipse and Java 1.6. So I want to ask why doesn't Need for Speed Most Wanted accept the simulated key events and how to solve this program? Thank you.

like image 665
suiyemin Avatar asked Oct 16 '10 04:10

suiyemin


3 Answers

I wrote a bot for controlling racing game using Java Robot.

What KeyEvents to you generate?

For Java Robot's simulated key events like "VK_Up, VK_Down,Vk_Left,VK_Right", the "Need for Speed Most Wanted" ignored. But, for keys "A-Z", the game accepted!

Maybe you are trying to generate keyTyped events when you should be using keyPressed and keyReleased?

Here is a simple example that works with the right/left/up down keys. Try entering (1, 2, 3) then backspace to the beginning and enter (0). Then press the playback button.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class RobotPlayback extends JFrame implements KeyListener, ActionListener
{
    JTextField textField1;
    JTextField textField2;
    List playback = new ArrayList();

    public RobotPlayback()
    {
        textField1 = new JTextField(30);
        textField1.addKeyListener( this );
        getContentPane().add(textField1, BorderLayout.NORTH);
//
        JButton button = new JButton("Playback");
        button.addActionListener( this );
        button.setFocusable(false);
        getContentPane().add(button);
//
        textField2 = new JTextField(30);
        getContentPane().add(textField2, BorderLayout.SOUTH);
    }

    public void keyPressed(KeyEvent e)
    {
        playback.add(e);
    }

    public void keyReleased(KeyEvent e)
    {
        playback.add(e);
    }

    public void keyTyped(KeyEvent e) {}

    public void actionPerformed(ActionEvent e)
    {
        Thread playback = new Thread()
        {
            public void run()
            {
                 playback();
            }
        };
        playback.start();
    }

    private void playback()
    {
        textField2.requestFocus();

        try
        {
            Robot robot = new Robot();
            robot.setAutoDelay( 200 );

            for (int i = 0; i < playback.size();i++)
            {
                KeyEvent event = (KeyEvent)playback.get(i);

                if (event.getID() == KeyEvent.KEY_PRESSED)
                    robot.keyPress( event.getKeyCode() );
                else
                    robot.keyRelease( event.getKeyCode() );
            }
        }
        catch(Exception exc)
        {
            System.out.println(exc);
        }

        setVisible(true);
        playback = new ArrayList();
        textField1.requestFocus();
    }

    public static void main(String[] args)
        throws Exception
    {
        JFrame frame = new RobotPlayback();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
     }
}

I registered the account just in this morning and 11 reputations only. Not enough for upvoting

Even with 11 points you should be able to "accept" an answer if it answers your question.

like image 113
camickr Avatar answered Oct 21 '22 16:10

camickr


As far as I understand, DirectX doesn't rely on the events generated by Windows to receive input from the keyboard. Thus, firing simulated events won't work for you. I don't think you can do what you want.

like image 38
slezica Avatar answered Oct 21 '22 15:10

slezica


The Java Robot has a bug when it comes to arrow keys. It will be pressing the num pad arrows. Currently, it is not possible to press the non num pad arrows.

Ref: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4838497

like image 35
Craigo Avatar answered Oct 21 '22 16:10

Craigo