SHARE
TWEET

MC-10 Keyboard Arduino for Teensy 2.0

a guest Oct 29th, 2012 150 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MC-10 Keyboard Arduino for Teensy 2.0
  2. // Ray Dios Haque - ray@oddree.com - 10/2012
  3.  
  4. #include "Keypad.h"
  5.  
  6. const byte ROWS = 7; //seven rows
  7. const byte COLS = 8; //eight columns
  8. char keys[ROWS][COLS] = {
  9. /*  1   2   3   4   5   6   7   8  */
  10.   {'@','a','b','c','d','e','f','g'}, // ROW 1
  11.   {'h','i','j','k','l','m','n','o'}, // ROW 2
  12.   {'p','q','r','s','t','u','v','w'}, // ROW 3
  13.   {'x','y','z','NO_KEY','NO_KEY','NO_KEY','KEY_ENTER',' '}, // ROW 4
  14.   {'0','1','2','3','4','5','6','7'}, // ROW 5
  15.   {'8','9',':',';',',','-','.','/'}, // ROW 6
  16.   {'KEY_CONTROL','NO_KEY','KEY_BACKSPACE','NO_KEY','NO_KEY','NO_KEY','NO_KEY','KEY_RIGHTSHIFT'}  // ROW 7
  17. };  
  18.  
  19. byte rowPins[ROWS] = {14, 15, 17, 18, 19, 20, 21}; //connect to the row pinouts of the keypad
  20. byte colPins[COLS] = {0, 1, 2, 3, 4, 5, 6, 7}; //connect to the column pinouts of the keypad
  21.  
  22. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  23.  
  24. boolean shiftkey = false;  // Assuming from the get-go that SHIFT is not pressed!
  25. boolean controlkey = false; // Assuming from the get-go that the Control Key is not pressed!
  26.  
  27. void setup(){
  28.   //Serial.begin(9600);
  29. }
  30.  
  31. void loop(){
  32.  
  33.   char key = keypad.getKey();
  34.  
  35.   if (key != NO_KEY){
  36.       if (controlkey == true) {
  37.             switch (key) {
  38.             // F1 - F12 keys
  39.               case'1': Keyboard.set_key1(KEY_F1); Keyboard.send_now(); break;
  40.               case'2': Keyboard.set_key1(KEY_F2); Keyboard.send_now(); break;
  41.               case'3': Keyboard.set_key1(KEY_F3); Keyboard.send_now(); break;
  42.               case'4': Keyboard.set_key1(KEY_F4); Keyboard.send_now(); break;
  43.               case'5': Keyboard.set_key1(KEY_F5); Keyboard.send_now(); break;
  44.               case'6': Keyboard.set_key1(KEY_F6); Keyboard.send_now(); break;
  45.               case'7': Keyboard.set_key1(KEY_F7); Keyboard.send_now(); break;
  46.               case'8': Keyboard.set_key1(KEY_F8); Keyboard.send_now(); break;
  47.               case'9': Keyboard.set_key1(KEY_F9); Keyboard.send_now(); break;
  48.               case'0': Keyboard.set_key1(KEY_F10); Keyboard.send_now(); break;
  49.               case':': Keyboard.set_key1(KEY_F11); Keyboard.send_now(); break;
  50.               case'_': Keyboard.set_key1(KEY_F12); Keyboard.send_now(); break;
  51.             // Arrow keys
  52.               case'w': Keyboard.set_key1(KEY_UP); Keyboard.send_now(); break;
  53.               case'z': Keyboard.set_key1(KEY_DOWN); Keyboard.send_now(); break;
  54.               case'a': Keyboard.set_key1(KEY_LEFT); Keyboard.send_now(); break;
  55.               case's': Keyboard.set_key1(KEY_RIGHT); Keyboard.send_now(); break;
  56.             // Need a TAB key, of course
  57.               case'KEY_ENTER': Keyboard.set_key1(KEY_TAB); Keyboard.send_now(); break;
  58.             // Control Backspace (break key) = DELETE
  59.               case'KEY_BACKSPACE': Keyboard.set_key1(KEY_DELETE); Keyboard.send_now(); break;
  60.             // Control twice = ESC
  61.               case'KEY_CONTROL': Keyboard.set_key1(KEY_ESC); Keyboard.send_now(); break;
  62.               }
  63.             Keyboard.set_key1(0); Keyboard.send_now(); // Reset to no keys being pressed
  64.             controlkey = false; // Now leaving control key routine - so we reset the control.
  65.       }
  66.       else if (shiftkey == true) {
  67.             switch (key) {
  68.             // We pressed the shift key, so this is a CAP or uppper
  69.               case '1': Keyboard.print('!'); break;
  70.               case '2': Keyboard.print('"'); break;
  71.               case '3': Keyboard.print('#'); break;
  72.               case '4': Keyboard.print('$'); break;
  73.               case '5': Keyboard.print('%'); break;
  74.               case '6': Keyboard.print('&'); break;
  75.               case '7': Keyboard.print('\''); break; // Note the backslash to escape the single quote!
  76.               case '8': Keyboard.print('('); break;
  77.               case '9': Keyboard.print(')'); break;
  78.               case ':': Keyboard.print('*'); break;
  79.               case '-': Keyboard.print('='); break;
  80.              
  81.               case ';': Keyboard.print('+'); break;
  82.               case ',': Keyboard.print('<'); break;
  83.               case '.': Keyboard.print('>'); break;
  84.               case '/': Keyboard.print('?'); break;
  85.              
  86.               case 'a': Keyboard.print('A'); break;
  87.               case 'b': Keyboard.print('B'); break;
  88.               case 'c': Keyboard.print('C'); break;
  89.               case 'd': Keyboard.print('D'); break;
  90.               case 'e': Keyboard.print('E'); break;
  91.               case 'f': Keyboard.print('F'); break;
  92.               case 'g': Keyboard.print('G'); break;
  93.               case 'h': Keyboard.print('H'); break;
  94.               case 'i': Keyboard.print('I'); break;
  95.               case 'j': Keyboard.print('J'); break;
  96.               case 'k': Keyboard.print('K'); break;
  97.               case 'l': Keyboard.print('L'); break;
  98.               case 'm': Keyboard.print('M'); break;
  99.               case 'n': Keyboard.print('N'); break;
  100.               case 'o': Keyboard.print('O'); break;
  101.               case 'p': Keyboard.print('P'); break;
  102.               case 'q': Keyboard.print('Q'); break;
  103.               case 'r': Keyboard.print('R'); break;
  104.               case 's': Keyboard.print('S'); break;
  105.               case 't': Keyboard.print('T'); break;
  106.               case 'u': Keyboard.print('U'); break;
  107.               case 'v': Keyboard.print('V'); break;
  108.               case 'w': Keyboard.print('W'); break;
  109.               case 'x': Keyboard.print('X'); break;
  110.               case 'y': Keyboard.print('Y'); break;
  111.               case 'z': Keyboard.print('Z'); break;
  112.              
  113.               case 'KEY_RIGHTSHIFT':
  114.               // Tap SHIFT twice to CTRL+ALT+DELETE
  115.                   Keyboard.set_modifier(MODIFIERKEY_CTRL);
  116.                   Keyboard.send_now();
  117.                   Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT);
  118.                   Keyboard.send_now();
  119.                   Keyboard.set_key1(KEY_DELETE);
  120.                   Keyboard.send_now();
  121.                   Keyboard.set_modifier(0);
  122.                   Keyboard.set_key1(0);
  123.                   Keyboard.send_now();
  124.                   break;
  125.             }
  126.         shiftkey = false; // Now leaving shift key routine - so we reset the shift.
  127.       }
  128.       // Normal key presses
  129.       else {
  130.       switch (key) {
  131.         case 'KEY_BACKSPACE':
  132.           Keyboard.set_key1(KEY_BACKSPACE);
  133.           Keyboard.send_now();
  134.           Keyboard.set_key1(0);
  135.           Keyboard.send_now();
  136.         break;
  137.         case 'KEY_ENTER':
  138.           Keyboard.set_key1(KEY_ENTER);
  139.           Keyboard.send_now();
  140.           Keyboard.set_key1(0);
  141.           Keyboard.send_now();
  142.         break;
  143.         case 'KEY_CONTROL':
  144.           controlkey = true; // Next key will be a CONTROL key
  145.         break;
  146.         case 'KEY_RIGHTSHIFT':
  147.           shiftkey = true; // Next key will be a CAP KEY or upper key
  148.         break;
  149.         default:
  150.           //Serial.println(key);
  151.           Keyboard.print(key);
  152.         break;
  153.         }
  154.       }
  155.   }
  156. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top