电脑的设备管理器没有检测到板子USB,是因为板子电路没有通的原因吗?但是电脑有一个电流上涌的警告
【相关推荐】
#include "Keyboard.h"
#include <PS2KeyAdvanced.h>
//最新的,用于PS2转换到USB
//sample以A为例:按下代码 释放代码
//一般: 41 8041
//CAPS LOCK下: 1041 9041
//SHIFT按住情况下: 4041 C041
//SHIFT按键:4106 8106
/* Keyboard constants Change to suit your Arduino
define pins used for data and clock from keyboard */
#define DATAPIN 2
#define IRQPIN 3
#define UPPER 40
#define RELEASE_UPPER C0
#define SHIFTMODE 0x40
#define FUNCTION 0x80
uint16_t c;
PS2KeyAdvanced keyboard;
void setup( )
{
// Configure the keyboard library
keyboard.begin( DATAPIN, IRQPIN );
Serial.begin( 115200 );
Serial.println( "PS2 Advanced Key Simple Test:" );
Keyboard.begin();
}
void loop( )
{
if ( keyboard.available( ) )
{
// read the next key
c = keyboard.read( );
//For DEBUG
if ( c > 0 )
{
// DisValue(c);//DEBUG用的,获取c的具体值,状态符号和按键值
}
//ACTION
if ((c & 0xFF) > 0) //判断获取的按键值c有没有大于0
{
DisValue(c);
switch (c >> 8)
{
case 0x10://大写锁定下按下字母
UpperKey(c);
break;
case 0x90://大写锁定下释放字母
UpperKey_Release(c);
break;
case 0x40://SHIFT大写按下
UpperKey(c);
break;
case 0xC0://SHIFT大写释放
UpperKey_Release(c);
break;
case 0x11:
if (c == 0x111D)
{
Keyboard.press(KEY_TAB);
}
break;
case 0x91:
if (c == 0x911D)
{
Keyboard.release(KEY_TAB);
}
break;
case 0x00://一般按键按下
standardKey(c);
case 0x80://一般按键释放
standardKey_Release(c);
break;
case 0x01://功能按键
FuncKey(c);
break;
case 0x81://功能键和SHIFT键释放
Serial.println( "FUNC RELEASE" );
FuncKey_Release(c);
break;
case 0x41://SHIFT键按下
break;
break;
case 0x20://CTRL Press And
Keyboard.press(KEY_LEFT_CTRL);
standardKey(c);
break;
case 0xA0://CTRL Release
Keyboard.release(KEY_LEFT_CTRL);
standardKey_Release(c);
break;
case 0x21: //CTRL
Serial.println( "CTRL PRESS" );
Keyboard.press(KEY_LEFT_CTRL);
break;
case 0x9: //LEFT-ALT
Serial.println( "L-ALT PRESS" );
Keyboard.press(KEY_LEFT_ALT);
break;
case 0x5: //RIGHT-ALT
Serial.println( "R-ALT PRESS" );
Keyboard.press(KEY_RIGHT_ALT);
break;
default:
Serial.print( " Unknow Higher code: " );
Serial.println( c >> 8, HEX );
}
// Keyboard.press(c);
}
}
}
///////////////////////
/*
Descript: For DEBUG
*///////////////////////
void DisValue(unsigned int c)
{
Serial.print( "Value " );
Serial.print( c, HEX );
Serial.print( " - Status Bits " );
Serial.print( c >> 8, HEX );
Serial.print( " Code " );
Serial.println( c & 0xFF, HEX );
}
///////////////////////
/*
Descript: abcde and so on,main key
*///////////////////////
#define LEFT_WIN 0x008B//press
#define CAPELOCK 0x00FA//press
void standardKey(unsigned int c)
{
int key = c << 8;
Serial.print("Stand press :");
if(remap(c)!=0)
{
Keyboard.press(remap(c));
return;
}
if (((key >> 8) >= '@') && ((key >> 8) <= 'Z'))
{
Serial.write((key >> 8) + (97 - 65)); //输出小写字符
Keyboard.press(c + ' ');
}
else
{
Serial.write((key >> 8)); //输出其他的字符
Keyboard.press(c);
}
Serial.println("");
}
//return 0: not found
unsigned char remap(unsigned int c)
{
unsigned int key = c << 8;
unsigned char remap_char=0;
switch (key>>8)
{
case 0x3E:
remap_char = '/';
break;
case 0x3B:
remap_char = ',';
break;
case 0x3A: //按键[']
remap_char = 0x27;
break;
case 0x8B:
remap_char = KEY_LEFT_GUI;
break;
case 0x3D:
remap_char = '.';
break;
case 0xFA:
remap_char = KEY_CAPS_LOCK;
break;
default:
break;
}
return remap_char;
}
//CODE:0x 80??
void standardKey_Release(unsigned int c)
{
int key = c << 8;
Serial.print("Stand Release :");
if(remap(c)!=0)
{
Keyboard.release(remap(c));
return;
}
if (((key >> 8) >= 'A') && ((key >> 8) <= 'Z'))
{
Serial.write((key >> 8) + (97 - 65)); //输出小写字符
Keyboard.release(c + ' ');
}
else
{
Serial.write((key >> 8)); //输出其他的字符
Keyboard.release(c);
}
Serial.println("");
}
void UpperKey(unsigned int c)
{
int key = c << 8;
unsigned int chara;
Serial.print("Upper press :");
if (c == 0x403A) //["]
{
Keyboard.press(0x22);
return;
}
if ((key >> 8) >= '0' && (key >> 8) <= '9')
{
switch (key >> 8)
{
case '0':
chara = ')';
break;
case '1':
chara = '!';
break;
case '2':
chara = '@';
break;
case '3':
chara = '#';
break;
case '4':
chara = '$';
break;
case '5':
chara = '%';
break;
case '6':
chara = '^';
break;
case '7':
chara = '&';
break;
case '8':
chara = '*';
break;
case '9':
chara = '(';
break;
default:
break;
}
Serial.write(chara);
Serial.println("");
Keyboard.press(chara);
}
else
{
Serial.write(key >> 8);
Serial.println("");
Keyboard.press(key >> 8);
}
}
void UpperKey_Release(unsigned int c)
{
int key = c << 8;
Serial.print("Upper release :");
Serial.write(key >> 8);
if (c == 0xC03A) //["]
{
Keyboard.release(0x22);
return;
}
Serial.println("");
Keyboard.releaseAll();
}
void rep(String s)
{
Serial.print("Func press:");
Serial.println(s);
}
void FuncKey(unsigned int c)
{
int key = c << 8;
switch (key >> 8)
{case 0x1B :
rep("ESC");
Keyboard.press(KEY_ESC);
break;
case PS2_KEY_ENTER :
rep("ENTER");
Keyboard.press(0x28);
break;
case PS2_KEY_SPACE:
rep("SPACE");
Keyboard.press(0x20);
break;
case PS2_KEY_TAB :
rep("TAB");
Keyboard.press(KEY_TAB);
break;
case PS2_KEY_DELETE :
rep("DELETE");
Keyboard.press(KEY_DELETE);
break;
case PS2_KEY_INSERT :
Keyboard.press(KEY_INSERT);
rep("INSERT");
break;
case PS2_KEY_UP_ARROW :
rep("UP");
Keyboard.press(KEY_UP_ARROW);
break;
case PS2_KEY_DN_ARROW :
Keyboard.press(KEY_DOWN_ARROW );
rep("DOWN");
break;
case PS2_KEY_L_ARROW :
Keyboard.press(KEY_LEFT_ARROW );
rep("L_ARROW");
break;
case PS2_KEY_R_ARROW :
Keyboard.press(KEY_RIGHT_ARROW);
rep("R_ARROW");
break;
case PS2_KEY_PGDN :
Keyboard.press(KEY_PAGE_DOWN );
rep("PGDN");
break;
case PS2_KEY_PGUP :
Keyboard.press(KEY_PAGE_UP );
rep("PGUP");
break;
case PS2_KEY_END :
Keyboard.press(KEY_END );
rep("END");
break;
case PS2_KEY_HOME:
Keyboard.press(KEY_HOME );
rep("HOME");
break;
case PS2_KEY_SYSRQ :
Keyboard.press(PS2_KEY_SYSRQ);
rep("SYSRQ");
break;
case PS2_KEY_BACK:
Keyboard.press(PS2_KEY_BACK);
rep("BACK");
break;
case 0x04:
rep("PRINT SCREEN");
break;
case PS2_KEY_F1 :
rep("F1");
Keyboard.press(KEY_F1);
break;
case PS2_KEY_F2 :
rep("F2");
Keyboard.press(KEY_F2);
break;
case PS2_KEY_F3 :
rep("F3");
Keyboard.press(KEY_F3);
break;
case PS2_KEY_F4 :
rep("F4");
Keyboard.press(KEY_F4);
break;
case PS2_KEY_F5 :
rep("F5");
Keyboard.press(KEY_F5);
break;
case PS2_KEY_F6 :
rep("F6");
Keyboard.press(KEY_F6);
break;
case PS2_KEY_F7 :
rep("F7");
Keyboard.press(KEY_F7);
break;
case PS2_KEY_F8 :
rep("F8");
Keyboard.press(KEY_F8);
break;
case PS2_KEY_F9 :
rep("F9");
Keyboard.press(KEY_F9);
break;
case PS2_KEY_F10 :
rep("F10");
Keyboard.press(KEY_F10);
break;
case PS2_KEY_F11 :
rep("F11");
Keyboard.press(KEY_F11);
break;
case PS2_KEY_F12 :
rep("F12");
Keyboard.press(KEY_F12);
break;
case PS2_KEY_F13 :
rep("F13");
break;
case 0x1C:
Keyboard.press(KEY_BACKSPACE);
break;
default:
Serial.print("UNKNOW CODE:");
Serial.println(c, HEX);
}
}
void FuncKey_Release(unsigned int c)
{
// Keyboard.release(c);
Keyboard.releaseAll();
}
void KeyBoard_Press(unsigned int c)
{
Keyboard.press(c);
}
void KeyBoard_Release(unsigned int c)
{
Keyboard.release(c);
}