joystick doesn't work with flash-games


i have build a jostick with an arduino micro for a disabled person, that he can play small games.
It emulates the arrow keys and i also added buttons for the most common keys like return, space and ctrl.

Everything works fine until i want to use it for a flash-game (in browser). it doesn't recognize any of the inputs.

Can somebody explain me why and how to fix this.
also the ctrl-key doesn't work at all.

Here is the code. Please don't judge me for it. It's really quick and dirty copy'n'paste. :slight_smile:


 Controls the mouse from five pushbuttons on an Arduino Leonardo or Micro.

 * 5 pushbuttons attached to D2, D3, D4, D5, D6

 The mouse movement is always relative. This sketch reads
 four pushbuttons, and uses them to set the movement of the mouse.

 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the mouse commands.

 created 15 Mar 2012
 modified 27 Mar 2012
 by Tom Igoe

 this code is in the public domain


// set pin numbers for the five buttons:
const int upButton = 7;
const int downButton = 4;
const int leftButton = 5;
const int rightButton = 6;
const int mouseButtonleft = 2;
const int mouseButtonright = 8;
const int Button3 = 3;
const int Button4 = 9;

int range = 1;              // output range of X or Y movement; affects movement speed
int responseDelay = 12;     // response delay of the mouse, in ms
int zeit = 1;
float range2 = 1 ;

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() {
  // initialize the buttons' inputs:
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);
  pinMode(mouseButtonleft, INPUT_PULLUP);
  pinMode(mouseButtonright, INPUT_PULLUP);
  pinMode(Button3, INPUT_PULLUP);
  pinMode(Button4, INPUT_PULLUP);



void loop() {

  reading = digitalRead(Button4);
  // read the buttons:
  int upState = digitalRead(upButton);
  int downState = digitalRead(downButton);
  int rightState = digitalRead(rightButton);
  int leftState = digitalRead(leftButton);
  int clickStateleft = digitalRead(mouseButtonleft);
  int clickStateright = digitalRead(mouseButtonright);
  int clickState3 = digitalRead(Button3);
 // int clickState4 = digitalRead(Button4);

  // calculate the movement distance based on the button states:
  int  xDistance = (leftState - rightState) * range;
  int  yDistance = (upState - downState) * range;
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
      state = HIGH;
    time = millis();

  if (state == LOW) {
    // if X or Y is non-zero, move:
    if (((xDistance != 0) || (yDistance != 0)) && (zeit > 50)) {
      Mouse.move(((leftState - rightState)*range2), ((upState - downState)*range2), 0);
      range2 += 0.01;
    else {
      Mouse.move(xDistance, yDistance, 0);
      zeit ++;

    if (digitalRead(upButton) && digitalRead(downButton) && digitalRead(rightButton) && digitalRead(leftButton)) {
      zeit = 1;
      range2 = 1;

    if (range2 > 5) {
      range2 -= 0.01;
  // if the left mouse button is pressed:
  if (clickStateleft == LOW) {
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    // if the right mouse button is pressed:
  if (clickStateright == LOW) {
    // if the mouse is not pressed, press it:
  // else the mouse button is not pressed:
    else {
    //if the mouse is pressed, release it:

    if (clickState3 == LOW) {

    // a delay so the mouse doesn't move too fast:
    //delay (range2);

  else {
    if (upState == LOW) {
    if (downState == LOW) {
    if (rightState == LOW) {
    if (leftState == LOW) {
    if (clickStateleft == LOW) {
    if (clickStateright == LOW) {
      Keyboard.write((char) 32);
    if (clickState3 == LOW) {

  previous = reading;

Hello Chromo.

I think this is an issue on Flash' end. See this bug report : Tracker

Maybe you could find a workaround by using joytokey or a similar program? Or just work around the mentioned bug itself?

Have you tried with other HTML5 games that support gamepads? If it works with them, it means that it is indeed a problem related to the flash player.

EDIT: Now that I'm reading your code I'm not sure anymore, since you're emulating the mouse and keyboard. I'm still thinking there could be an issue with Flash.

First of all the code contains to operation methods. You can use the joystick as a mouse and you kann also switch the mapping to the arrow keys.

I checked HTML5 games and it works perfectly. Unfortunately I had no luck with flashgames although i tried the workaround i
found in the link of the previous post.

so it seems to be a flash related issue.

Ok... found it.
Erverything works fine now. Also in Flash-Games.

I changed the code to and Keyboard.release() instead of Keyboard.write() even if i don't understand the difference.
Can somebody explain me why? : When called, functions as if a key were pressed and held on your keyboard.

Keyboard.write() : Sends a keystroke to a connected computer. This is similar to pressing and releasing a key on your keyboard. You can send some ASCII characters or the additional keyboard modifiers and special keys.

Only ASCII characters that are on the keyboard are supported.

I'm guessing "emulates" a key press and holding it down while keyboard.write() sends the information related to the ASCII value that the key pressed has.

In this case, the important part is that only ASCII characters that are on the keyboard are supported. Arrows don't have an ASCII value (see :

But if the arrows don't have an ASCII value and only ASCII values are supported by Keyboard.write(), why it worked anyhow with regular programms on all operating systems except browsergames.