Need for Speed ​​Most Wanted game cannot be controlled by Robot Java class

I wrote a bot to control a racing game using Java Robot. The bot works well for Need For Speed ​​Underground, with the exception of the Down key (the Up, Left, Right keys work very well). However, my bot cannot control Need For Speed ​​Most Wanted.

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

I am programming on Windows 7 using Eclipse and Java 1.6. So I want to ask why "Need for Speed ​​Most Wanted" does not accept simulated key events and how to solve this program? Thanks.

+6
java
source share
5 answers

I wrote a bot to control a racing game using Java Robot.

What key opportunities are generated for you?

For Java Robot, key events are modeled as "VK_Up, VK_Down, Vk_Left, VK_Right", "Need for Speed ​​Most Wanted" is ignored. But, for the "AZ" keys, the game is accepted!

Perhaps you are trying to generate keyword events when you should use keyPressed and keyReleased?

Here is a simple example that works with the right / left / up down keys. Try typing (1, 2, 3), then back to the beginning and typing (0). Then press the play 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 an account only this morning and 11 reputations. Not enough to continue

Even with 11 points, you can “accept” the answer if it answers your question.

+2
source share

As far as I understand, DirectX does not rely on events created by Windows to receive keyboard input. Thus, running simulated events will not work for you. I don’t think you can do what you want.

+2
source share

Java Robot has a bug when it comes to arrow keys. He will press the arrows num pad. It is currently not possible to press arrows not a num pad.

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

+2
source share

Although this is an old question, I will post my answer if it helps someone later.

I recently ran into the same problem. I called keyPress and keyRelease right after the other for each of the left, right, up and down keys, based on some external input. I tried a bunch of games, but Robot keystrokes didn't work in any of them. When I tried the same code with the cursor in a text file, the cursor moved correctly.

The problem was that games usually require continuous keystrokes, so the combination of keyPress + keyRelease has no effect. So I fixed the problem by calling keyRelease in the opposite direction for each arrow key.

For example, if you need to press the left arrow key, I call

keyRelease(KeyEvent.VK_RIGHT);
keyPress(KeyEVent.VK_LEFT);

This holds the left key down until the right arrow key is finally pressed. There are better ways to do this by tracking the last keystroke, etc., but this method works fine.

0
source share

I know this is a very old post, but my answer may be useful for those looking for an answer

Answer: -

Because some games use the DirectInput API to read keyboard scan codes , not virtual key codes. Since java.awt.Robot performs keystrokes on virtual key codes and not scan codes, so keyboard events simulated using java.awt.Robot may not affect the game. You must make your own Windows call from your java application using JNI / JNA.

code is available here https://github.com/umer0586/winKeyboad

 Keyboard keyboard = new Keyboard(); keyboard.winKeyPress(ScanCode.DIK_UP); //Thread.sleep(1000); keyboard.winKeyRelease(ScanCode.DIK_UP); 
0
source share

All Articles