Loading...

Tutorial.KeybaordAndMouseControl History

Show minor edits - Show changes to markup

October 31, 2012, at 04:43 PM by Scott Fitzgerald -
Changed lines 38-41 from:

(:source lang=arduino tabwidth=4:)

/*

  KeyboardAndMouseControl
to:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

Deleted lines 40-130:
 Controls the mouse from five pushbuttons on an Arduino Leonardo and Arduino Due.

 Hardware:
 * 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:

// set pin numbers for the five buttons: const int upButton = 2; const int downButton = 3; const int leftButton = 4; const int rightButton = 5; const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:

  pinMode(upButton, INPUT);       
  pinMode(downButton, INPUT);       
  pinMode(leftButton, INPUT);       
  pinMode(rightButton, INPUT);       
  pinMode(mouseButton, INPUT);

  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
  Keyboard.begin();

}

void loop() {

  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inChar = Serial.read();

    switch (inChar) {   
    case 'u':
      // move mouse up
      Mouse.move(0, -40);
      break; 
    case 'd':
      // move mouse down
      Mouse.move(0, 40);
      break;
    case 'l':
      // move mouse left
      Mouse.move(-40, 0);
      break;
    case 'r':
      // move mouse right
      Mouse.move(40, 0);
      break;
    case 'm':
      // move mouse right
      Mouse.click(MOUSE_LEFT);
      break;
    }
  }

  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u'); 
  }
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d'); 
  }
  if (digitalRead(leftButton) == HIGH) {
    Keyboard.write('l'); 
  }
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r'); 
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m'); 
  }

}

(:sourceend:)

October 22, 2012, at 05:15 AM by Federico -
Changed line 10 from:
  • Arduino Leonardo board
to:
  • Arduino Leonardo or Arduino Due board
Changed line 43 from:
 Controls the mouse from five pushbuttons on an Arduino Leonardo.
to:
 Controls the mouse from five pushbuttons on an Arduino Leonardo and Arduino Due.
May 21, 2012, at 01:42 PM by Scott Fitzgerald -
Changed lines 38-133 from:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source lang=arduino tabwidth=4:)

/*

  KeyboardAndMouseControl

 Controls the mouse from five pushbuttons on an Arduino Leonardo.

 Hardware:
 * 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:

// set pin numbers for the five buttons: const int upButton = 2; const int downButton = 3; const int leftButton = 4; const int rightButton = 5; const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:

  pinMode(upButton, INPUT);       
  pinMode(downButton, INPUT);       
  pinMode(leftButton, INPUT);       
  pinMode(rightButton, INPUT);       
  pinMode(mouseButton, INPUT);

  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
  Keyboard.begin();

}

void loop() {

  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inChar = Serial.read();

    switch (inChar) {   
    case 'u':
      // move mouse up
      Mouse.move(0, -40);
      break; 
    case 'd':
      // move mouse down
      Mouse.move(0, 40);
      break;
    case 'l':
      // move mouse left
      Mouse.move(-40, 0);
      break;
    case 'r':
      // move mouse right
      Mouse.move(40, 0);
      break;
    case 'm':
      // move mouse right
      Mouse.click(MOUSE_LEFT);
      break;
    }
  }

  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u'); 
  }
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d'); 
  }
  if (digitalRead(leftButton) == HIGH) {
    Keyboard.write('l'); 
  }
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r'); 
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m'); 
  }

}

(:sourceend:)

May 21, 2012, at 01:27 PM by Scott Fitzgerald -
Changed line 38 from:

(:source https://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

May 21, 2012, at 01:25 PM by Scott Fitzgerald -
Changed line 38 from:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source https://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

May 21, 2012, at 01:22 PM by Scott Fitzgerald -
Changed line 38 from:

(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

May 21, 2012, at 01:20 PM by Scott Fitzgerald -
Changed line 38 from:

(:source http://raw.github.com/arduino/Arduino/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

May 21, 2012, at 01:20 PM by Scott Fitzgerald -
Changed line 38 from:

(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source http://raw.github.com/arduino/Arduino/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

May 21, 2012, at 01:20 PM by Scott Fitzgerald -
Changed line 38 from:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source https://raw.github.com/arduino/Arduino/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

Changed line 45 from:

(:include LeonardoSeeAlsoInclude:)

to:

(:include LeonardoSeeAlsoInclude:)

May 02, 2012, at 10:13 AM by Scott Fitzgerald -
Changed line 38 from:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo only)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

to:

(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:)

April 27, 2012, at 12:53 PM by Scott Fitzgerald -
Changed lines 6-7 from:

NB: When you use these libraries, the Arduino takes over your computer's keyboard and mouse! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a reliable control system before you call Keyboard.print().

to:

NB: When you use these libraries, the Arduino takes over your computer's keyboard and mouse!

Deleted line 44:
April 27, 2012, at 11:35 AM by Scott Fitzgerald -
Added line 45:
April 27, 2012, at 11:34 AM by Scott Fitzgerald -
Added line 45:

(:include LeonardoSeeAlsoInclude:)

April 27, 2012, at 11:31 AM by Scott Fitzgerald -
Changed lines 31-32 from:
to:
April 27, 2012, at 11:30 AM by Scott Fitzgerald -
Changed line 12 from:
  • Five momentary pushbuttons
to:
  • Five pushbuttons
Changed lines 21-22 from:

Attach one end of the the pushbuttons to pins 2, 3, 4, 5, and 6 on the Leonardo. Attach the other end to +5V. Use the resistors as pull-downs, providing a reference to ground for the switches. Attach them from the pin connecting to the Leonardo 4 to ground.

to:

Attach one end of the the pushbuttons to pins 2, 3, 4, 5, and 6 on the Leonardo. Attach the other end to +5V. Use the resistors as pull-downs, providing a reference to ground for the switches. Attach them from the pin connecting to the Leonardo to ground.

April 27, 2012, at 11:29 AM by Scott Fitzgerald -
Changed lines 21-22 from:

Attach one end of the the pushbuttons to pins 4, 5, 6, 7, and 8 on the Leonardo. Attach the other end to +5V. Use the resistors as pull-downs, providing a reference to ground for the switches. Attach them from the pin connecting to the Leonardo 4 to ground.

to:

Attach one end of the the pushbuttons to pins 2, 3, 4, 5, and 6 on the Leonardo. Attach the other end to +5V. Use the resistors as pull-downs, providing a reference to ground for the switches. Attach them from the pin connecting to the Leonardo 4 to ground.

April 27, 2012, at 09:39 AM by Scott Fitzgerald - page creation
Added lines 1-44:

Examples > USB (Leonardo)

Keyboard and Mouse Control

This example illustrates the use of the Mouse and keyboard libraries together. Five momentary switches act as directional buttons for your cursor. When a button is pressed, the cursor on your screen will move, and a keypress will be sent to the computer. Once you have the Leonardo programmed and wired up, open up your favorite text editor to see the results.

NB: When you use these libraries, the Arduino takes over your computer's keyboard and mouse! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a reliable control system before you call Keyboard.print().

(:div class=BOM :) Hardware Required

  • Arduino Leonardo board
  • breadboard
  • Five momentary pushbuttons
  • Five 10-kiloohm resistors

Software Required

  • Any text editor

(:divend:)

Circuit
Attach one end of the the pushbuttons to pins 4, 5, 6, 7, and 8 on the Leonardo. Attach the other end to +5V. Use the resistors as pull-downs, providing a reference to ground for the switches. Attach them from the pin connecting to the Leonardo 4 to ground.

Once you've programmed your Leonardo, unplug the USB cable and open a text editor. Connect your Leonardo board to your computer and press the buttons to write in the document as you move the cursor.

click the images to enlarge (:div class=circuit:)

image developed using Fritzing. For more circuit examples, see the Fritzing project page

(:divend:)

Code

(:div class=code :) (:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/09.USB(Leonardo only)/KeyboardAndMouseControl/KeyboardAndMouseControl.ino lang=arduino tabwidth=4:) (:divend:)

See Also




Bookmark and Share