用Arduino Leonardo制做虚拟键盘(简易badusb)

Leonardo是Arduino开发板的一种型号,能够像其余Arduino板同样对Leonardo进行编程和使用。可是,有一些重要的区别:html

The Leonardo differ from other Arduino boards in that they use a single microcontroller to both run your sketches and for USB communication with the computer. The Uno and other boards use separate microcontrollers for these two functions, meaning that the USB connection to the computer remains established regardless of the state of the main microcontroller. By combining these two functions onto a single processor, the Leonardo allows for more flexibility in its communication with the computer. It also helps to lower the cost of the board by removing the need for an additional processor.android

Arduino Leonardo不一样于以前所有的Arduino控制器,他直接使用了ATmega32u4的USB通讯功能,取消了USB转UART芯片。这使得Leonardo不只能够做为一个虚拟的COM端口,还能够做为鼠标或者键盘链接到计算机。web

今天用Arduino Leonardo作的东西很花里胡哨,先来看看效果吧:编程

虚拟键盘演示(badusb)bash


如下是视频连接:
http://v.youku.com/v_show/id_XNDQ3ODExMDIwOA==.html?x&sharefrom=android&sharekey=4c06da3938ffd6ead4184f1d59feef9f6

先在Arduino IDE里新建一个文件,导入资源库:less

#include <Keyboard.h>

这是键盘操做的库,只能在Arduino Leonardo系列的开发板上使用,若是在Arduino UNO 上编译的话,会报错dom

接下来咱们须要学习一些虚拟键盘的语句:ide

开始键盘通信:svg

Keyboard.begin();//开始键盘通信 

按键操做:oop

Keyboard.press();

与按键对应的是释放:

Keyboard.release();

固然还能够输入:

Keyboard.println("");

最后是结束键盘通信:

Keyboard.end();//结束键盘通信 

有了这些基础,就能够大胆地发挥想象啦!

好比说,我想要的效果是:把开发板插入电脑后,打开命令行,给命令行变颜色

for(int i=0;i<10;i++)
  {
  Keyboard.println("color 1");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 2");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 3");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 4");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 5");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 6");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 7");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 8");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  Keyboard.println("color 9");
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  delay(100); 
  }

原理也很简单:
在这里插入图片描述
再来个简单的:

Keyboard.println("telnet towel.blinkenlights.nl");

看看用字符串演的电影也不错
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
能看出这是什么电影嘛?哈哈哈…

除此以外,还有视频里的代码雨:

@echo off
color 0a
setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (0) do (
set "line="
for /l %%j in (1,1,80) do (
set /a Down%%j-=2
set "x=!Down%%j!"
if !x! LSS 0 (
set /a Arrow%%j=!random!%%3
set /a Down%%j=!random!%%15+10 )
set "x=!Arrow%%j!"
if "!x!" == "2" (
set "line=!line!!random:~-1! "
) else (set "line=!line! ")
)
set /p=!line!<nul
)

代码雨的代码要提早写在电脑上,保存为 .bat 文件,运行时直接拖到命令行便可.
在这里插入图片描述
还有不少花里胡哨的玩法,你们能够本身琢磨一下,总结一下,加上打开命令行的代码:

#include <Keyboard.h>

void setup() {//初始化
  Keyboard.begin();//开始键盘通信 
  delay(1000);//延时
  Keyboard.press(KEY_LEFT_GUI);//win键 
  Keyboard.press('r');//r键 
  Keyboard.release('r');
  delay(1000); 
  Keyboard.release(KEY_LEFT_GUI); 
  Keyboard.press(KEY_CAPS_LOCK);//利用开大写输小写绕过输入法
  Keyboard.release(KEY_CAPS_LOCK);
  delay(1000); 
  Keyboard.println("CMD");
  delay(500); 
  Keyboard.press(KEY_RETURN);
  Keyboard.release(KEY_RETURN);
  /* 插入你想实现的功能 */
  Keyboard.end();//结束键盘通信 
}
void loop()//循环
{}

将代码编译上传后,把Arduino Leonardo开发板插到其余电脑上试试看吧!