/* USB HID Keyboard
modified by: Chregu Müller
Tastenbelegung:
┌-------- USB --------┐
│ [2] │
│ [512] [4] │
│ [128] │
│ [1] [256] │
│ [16] │
└---------------------┘
Codes für Nummerntastatur:
220 Keypad /
221 Keypad *
222 Keypad -
223 Keypad +
224 Keypad ENTER
225 Keypad 1 and End
226 Keypad 2 and Down Arrow
227 Keypad 3 and PageDn
228 Keypad 4 and Left Arrow
229 Keypad 5
230 Keypad 6 and Right Arrow
231 Keypad 7 and Home
232 Keypad 8 and Up Arrow
233 Keypad 9 and PageUp
234 Keypad 0 and Insert
235 Keypad . and Delete
*/
// folgende Pins des Arduino Pro Micro sind an den Tasten angeschlossen:
int keypadPins[7] = {2, 10, 3, 16, 14, 4, 5};
int keypadStatus; // Used to monitor which buttons are pressed.
int timeout; // timeout variable used in loop
void setup()
{
for (int i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs
digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high
}
}
void loop()
{
keypadStatus = getKeypadStatus(); // read which buttons are pressed
if (keypadStatus != 0) // If a button is pressed go into here
{
sendKeyPress(keypadStatus); // send the button over USB
timeout = 2000; // top of the repeat delay
while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down
delayMicroseconds(1);
while (getKeypadStatus() == keypadStatus) // while the same button is held down
{
sendKeyPress(keypadStatus); // continue to send the button over USB
delay(50); // 50ms repeat rate
}
}
}
void sendKeyPress(int key)
{
switch(key)
{
case 1: // 0x001 '☺' 1
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(225); //Keypad 1 and End
Keyboard.release(225);
Keyboard.releaseAll();
break;
case 2: // 0x002 'ü' 129
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(225); //Keypad 1 and End
Keyboard.release(225);
Keyboard.press(226);
Keyboard.release(226);
Keyboard.press(233);
Keyboard.release(233);
Keyboard.releaseAll();
break;
case 4: // 0x004 '@' 64
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(230); //Keypad 6 and Right Arrow
Keyboard.release(230);
Keyboard.press(228);
Keyboard.release(228);
Keyboard.releaseAll();
break;
case 16: // 0x010 'ä' 132
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(225); //Keypad 1 and End
Keyboard.release(225);
Keyboard.press(227);
Keyboard.release(227);
Keyboard.press(226);
Keyboard.release(226);
Keyboard.releaseAll();
break;
case 128: // 0x080 'ö' 148
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(225); //Keypad 1 and End
Keyboard.release(225);
Keyboard.press(228);
Keyboard.release(228);
Keyboard.press(232);
Keyboard.release(232);
Keyboard.releaseAll();
break;
case 256: // 0x100 '♥' 3
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(227); //Keypad 3 and PageDn
Keyboard.release(227);
Keyboard.releaseAll();
break;
case 512: // 0x800 'Ü' 154
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(225); //Keypad 1 and End
Keyboard.release(225);
Keyboard.press(229);
Keyboard.release(229);
Keyboard.press(228);
Keyboard.release(228);
Keyboard.releaseAll();
break;
}
}
/* getKeypadStatus(): This function returns an int that represents
the status of the 12-button keypad. Only the 12 LSb's of the return
value hold any significange. Each bit represents the status of a single
key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ...,
'#' is bit 11.
This function doesn't work for multitouch.
*/
int getKeypadStatus()
{
int rowPins[4] = {keypadPins[2], keypadPins[6], keypadPins[5], keypadPins[0]}; // row pins of the keypad
int columnPins[3] = {keypadPins[1], keypadPins[3], keypadPins[4]}; // column pins of the keypad
int keypadStatus = 0; // this will be what's returned
/* initialize all pins, inputs w/ pull-ups */
for (int i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT);
digitalWrite(keypadPins[i], HIGH);
}
for (int row=0; row<4; row++)
{ // initial for loop to check all 4 rows
pinMode(rowPins[row], OUTPUT); // set the row pin as an output
digitalWrite(rowPins[row], LOW); // pull the row pins low
for (int col=0; col<3; col++)
{ // embedded for loop to check all 3 columns of each row
if (!digitalRead(columnPins[col]))
{
keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value
}
}
pinMode(rowPins[row], INPUT); // reset the row pin as an input
digitalWrite(rowPins[row], HIGH); // pull the row pin high
}
return keypadStatus;
}