The online racing simulator
Android/Java help
(10 posts, started )
#1 - mr_x
Android/Java help
I'm creating an Android app for a university project and I'm having some troubles with it.

Basically the app is a button box (going to add outgauge stuff if I can get basic button box working first), I want it to run for all sims (LFS, rF2, RACE07 etc.) so far I've been using a PC listener/server which takes the messages from the phone and puts them onto a keyboard key. This works in notepad and all windows applications but not in any of the above mentioned games.

It's all programmed in Java (only language I know well enough to do this).

I'm guessing (after much Googling) it's some form of protection to stop macro cheating - is there any way around this? So far the only solution I've come up with is to program the PC listener in C# and use DirectInput, but looking at tutorials to do it I'm completely baffled and don't want to waste time going into it if it doesn't work.

This is the switch statement I have in the listener which works in things like Notepad, web browser etc:

Robot keyPress = new Robot();
if(Pattern.matches("[a-zA-Z]+", message)) {

switch (message){
case "ignition":
//MessageToLocalComputer("/press i");
keyPress.keyPress(KeyEvent.VK_I);
keyPress.keyRelease(KeyEvent.VK_I);

break;
...

I am aware there are better and cleaner ways of doing this, I'm just after something that works for a project demo.
Throwing a wild guess, it's long time since I did send keypresses to LFS xD

Do a pause/sleep before releasing the key.
When I tried to send a key to LFS I was using this to get the job dobe

void toggleIKey()
/** Presses and releases "I" key */
void toggleIKey()
{
keybd_event(KEY_I, HWKEY_I, 0, 0);
Sleep(delay);
keybd_event(KEY_I, HWKEY_I, KEYEVENTF_KEYUP, 0);
}

The keyb_event() function is a part of user32.dll and it's not directly available in C#. It can be imported like this

[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

In order to access the Windows user32.dll with Java you can use Java Native Access library. You would then need to read the documentation to figure out how to call the Win32 keybd_event function.

As MadCatX says it's a bit easier using C# as the .NET Framework has builtin support for calling native libraries on Windows. You can find out more about invoking the Win32 API using C# at pinvoke.net.
#5 - mr_x
Excellent thanks alot guys. Will try this out in my next programming session (probably Wednesday).
#6 - amp88
Adding a delay to the Robot should solve your problem. You can either call robot.delay() manually or you can set the autoDelay for the robot to a given period (via robot.setAutoDelay()). 10ms works OK for me, but I'm sure you can have lots of fun playing around to find the optimal value.

package tester;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class LFSRobotTest {
public static void main(String[] args) {
try {
Robot robot = new Robot();
robot.setAutoDelay(10);

robot.delay(5000);

robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
} catch (AWTException e) {
e.printStackTrace();
}
}
}

If you run LFS windowed with the above example you can run the example and switch to the LFS window and see the ignition toggle. Obviously there's a ~5 second delay between running the program and the 'i' KeyPress.
#7 - mr_x
So is the problem that the code is executing before the game has a chance to react?

Once again thanks for the help, will give it a go on Wednesday. (Tuesdays are a very long day at uni so usually can't be arsed to program after that!)
#8 - mr_x
All working with Amp88's delay idea - moved the robot.delay() between the keypress and keyrelease methods and now works perfectly!
Incidentally, I know there are many people including me interested in android development. Code examples of of working things would be greatly appreciated if anyone could spare a few lines
#10 - mr_x
Quote from hyntty :Incidentally, I know there are many people including me interested in android development. Code examples of of working things would be greatly appreciated if anyone could spare a few lines

I'll share my code after university assessment (mid-April) to anybody who requests it, I'm not a great programmer so it wouldn't be a great example, but if it helps anybody then I don't mind. I may stick it onto the Google Code site. Hopefully I'll learn a few things too if anybody has any improvements!

I can't spend much time on it which is why it's slapped together at the minute as I'm in my final year at uni and I've got so much to juggle so can't spend as much time on this as I like.

My plan is to re-write it after I finish uni in June into something much more usable for everyone. For the server at the minute I'm just using the debugger in NetBeans for example and the buttons are just raw buttons from the Eclipse GUI designer. Right now everything is written for functionality rather than performance, looks and usability. Will also look into adding insim/outgauge stuff too, but right now I have no time to even begin to figure out how to do it.

Android/Java help
(10 posts, started )
FGED GREDG RDFGDR GSFDG