.Net, Azure and occasionally gamedev

Key input simulator for tablets

2017/09/30

Recently I was on a long flight. Since I have a old Dell Venue 8 Pro (running full Windows 10) with superb battery life (something like 8+ hours under use) I wanted to use it on the flight.

Since I'm not someone who can just watch movies for the entire time, I decided to download a gameboy emulator and play some pokemon for old times sake.

The problem was that none of the emulators work with touch; they all expect keyboard input.

There are some software solutions, like tabletpro, but I didn't feel like paying 10$ for a one time usecase. Being a developer, I decided to just spend an hour writing a program to fix my issue.

The program creates two simple windows (one for DPad, one for the A, B, Start and Select buttons) and I can resize them to be left/right of the emulator display.

Touching any of these buttons then forwards a key event to the actual emulator.

This allows me to map the buttons to specific keys and from the emulator standpoint it is as if someone actually pressed a key.

Downside

I realized pretty quickly that this would only work with stateful games, as only one keypress could be sent at any time (e.g. strafing and shooting wouldn't be possible), but since Pokemon is a stateful game I was able to play it just fine.

Since this isn't really a fully working "touch to keyboard" simulator I decided to not open source it, but I've attached the core code below for anyone to use.

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private IInputSimulator _inputSimulator;
private Process _emulatorProcess;

public bool Setup()
{
    // using nuget: WindowsInput
    _inputSimulator = new InputSimulator();
    // find the process to attach to
    _emulatorProcess = Process.GetProcessesByName("VisualBoyAdvance").FirstOrDefault<Process>();
    return _emulatorProcess != null;
}

// after successful setup this method is called by each press of the dpad/buttons.

// pressed indicates if button was pressed or released
public void SendKey(Key k, bool pressed)
{
    // force foreground before sending key event, that way the key event is forwarded
    SetForegroundWindow(_emulatorProcess.Handle)
    if (pressed)
    {
        _inputSimulator.Keyboard.KeyDown(k);
    }
    else
    {
        _inputSimulator.Keyboard.KeyUp(k);
    }
}

tagged as Input Simulator and Tablet