Skip to content

Instantly share code, notes, and snippets.

@tawashi5454
Created November 15, 2011 12:27
Show Gist options
  • Save tawashi5454/1366964 to your computer and use it in GitHub Desktop.
Save tawashi5454/1366964 to your computer and use it in GitHub Desktop.
Arduino USB Keyboard
/**
* VirtualUsbKeyboard
*
* Enumerates itself as a HID (Human Interface Device) to a host
* computer using a USB shield. The Arduino then appears to the host to
* be a USB keyboard and keypress events can be sent on demand.
*
* This example watches the state of 6 push buttons and when a button
* is pressed it sends a matching keypress event to the host.
*
* Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. http://www.gnu.org/licenses/
*
* www.practicalarduino.com/projects/easy/virtual-usb-keyboard
*/
// Requires the use of the "UsbKeyboard" library available from
// http://code.rancidbacon.com/ProjectLogArduinoUSB
//
// ** Note
// Now moved to http://code.google.com/p/vusb-for-arduino/
//
/**
* Modified by Takafumi IWAI
* takafumi@tawashi.org
*/
#include "UsbKeyboard.h"
// Define the inputs to use for buttons
#define BUTTON_A 6
#define BUTTON_B 7
#define BUTTON_C 8
#define BUTTON_MSG 10
// Use the on-board LED as an activity display
int ledPin = 13;
/**
* Configure button inputs and set up the USB connection to the host
*/
void setup()
{
delay(1000);
Serial.begin(9600);
Serial.println("Setup");
// Set up the activity display LED
pinMode (ledPin, OUTPUT);
digitalWrite (ledPin, LOW);
// Set the button pins to inputs
pinMode (BUTTON_A, INPUT);
pinMode (BUTTON_B, INPUT);
pinMode (BUTTON_C, INPUT);
pinMode (BUTTON_MSG, INPUT);
// Enable the CPU's internal 20k pull-up resistors on the button
// inputs so they default to a "high" state
digitalWrite (BUTTON_A, HIGH);
digitalWrite (BUTTON_B, HIGH);
digitalWrite (BUTTON_C, HIGH);
digitalWrite (BUTTON_MSG, HIGH);
// setup
setupUSBInterrupt();
// wait
waitUntilUSBGetReady();
}
/**
* Main program loop. Scan for keypresses and send a matching keypress
* event to the host
* FIXME: currently repeats as fast as it can. Add transition detection
*/
void loop()
{
if (digitalRead(BUTTON_A) == LOW) {
UsbKeyboard.sendKeyStroke(KEY_A);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
delayMs(100);
}
if (digitalRead(BUTTON_B) == LOW) {
UsbKeyboard.sendKeyStroke(KEY_B);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
delayMs(100);
}
if (digitalRead(BUTTON_C) == LOW) {
UsbKeyboard.sendKeyStroke(KEY_C);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
delayMs(100);
}
if (digitalRead(BUTTON_MSG) == LOW) {
UsbKeyboard.sendKeyStroke(KEY_H, MOD_SHIFT_LEFT);
UsbKeyboard.sendKeyStroke(KEY_E);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_O);
UsbKeyboard.sendKeyStroke(KEY_SPACE);
UsbKeyboard.sendKeyStroke(KEY_W, MOD_SHIFT_LEFT);
UsbKeyboard.sendKeyStroke(KEY_O);
UsbKeyboard.sendKeyStroke(KEY_R);
UsbKeyboard.sendKeyStroke(KEY_L);
UsbKeyboard.sendKeyStroke(KEY_D);
UsbKeyboard.sendKeyStroke(KEY_ENTER);
delayMs(100);
}
}
/**
* Define our own delay function so that we don't have to rely on
* operation of timer0, the interrupt used by the internal delay()
*/
void delayMs(unsigned int ms)
{
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
// Modified by Takafumi IWAI
void setupUSBInterrupt()
{
// Disable timer0 since it can mess with the USB timing. Note that
// this means some functions such as delay() will no longer work.
TIMSK0&=!(1<<TOIE0);
// Clear interrupts while performing time-critical operations
cli();
// Force re-enumeration so the host will detect us
usbDeviceDisconnect();
delayMs(250);
usbDeviceConnect();
// Set interrupts again
sei();
}
// Added by Takafumi IWAI
void waitUntilUSBGetReady()
{
while(true){
for(int i = 0; i < 10; i++){
UsbKeyboard.update();
delayMs(5);
}
if(usbInterruptIsReady()){
Serial.println("Now USB is ready!");
break;
}else{
Serial.println("USB is not ready. Waiting...");
}
}
digitalWrite(ledPin, HIGH);
}
@malisahin89
Copy link

malisahin89 commented Dec 17, 2018

Really Good Project. Thank You.
but how to make CTRL+ALT+DELETE?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment