-
Notifications
You must be signed in to change notification settings - Fork 0
Home
PS2Keyboard is a simple library used to read a PS2Keyboard using the PS/2 protocol. Many modern PS2Keyboards still support the PS/2 protocol! You can use it through usb by connecting D+ (the CLOCK pin) to an interrupt pin and D- (the DATA pin) to any digital input pin. (you also have to supply the PS2Keyboard ofcourse)
This library is far from finished and may still contain some bugs. It only supports FR and USkeyboards for now but feel free to contribute.
I decided to make this library since there aren't any good while still simple libraries out there that are currently maintained.
In order to explain, let's start with an example:
#include <PS2Keyboard.h>
PS2Keyboard keyboard;
void setup() {
Serial.begin(9600);
keyboard.begin(3, 4);
}
void loop() {
if(keyboard.has_data()) {
key_event key = keyboard.read_data();
Serial.println(key.scancode);
}
}This example prints out any scancode we receive form the keyboard.
- First, we have
PS2Keyboard keyboard;, this creates a keyboard with the namekeyboardnote that you can only have one keyboard. This is the main way we interract with the keyboard by calling methods (EG:keyboard.<method_name>) - Then in our setup, we have this line
keyboard.begin(3, 4);this initializes the keyboard. This method takes 2 integers, they are used to set which pins we connected the keyboard to. We have our CLOCK (D+) connected to pin 3, please make sure that your clock pin is connected to an interrupt pin - Next, we have this
keyboard.has_data(). This method returns true if we have received valid data from the keyboard. You can use this to know when you should start reading the keyboard (so you don't waste time reading the keyboard when there is no data). - Fourth, we have this piece of code
key_event key = keyboard.read_data();this reads the data form the buffer. When the keyboard receives data, it gets added to the buffer so we can read it at a later moment. When you call this reading function that data gets removed from the buffer so the next time you read, it outputs the next data. This function also returns a custom data structure this is a collection of variables which can be accessed like so:-
key.scancode(int) this is the unique code of the key that is pressed (note that normal and extended key codes can overlap) -
key.is_extended(bool) this is true for special key likeup_arrowordeletesince their codes may overlap with normal scancodes -
key.is_pressed(bool) this is true when the key is pressed. A keyboard sends 2 different events. When a key is pressed and when it's released so we can use this variable to differenciate those events
-
- Lastly but surely, we have
Serial.println(key.scancode);This just prints the scancode of the key_press we received to the serial monitor
void PS2Keyboard::begin(int clock_pin, int data_pin);- Description: Initializes the keyboard with clock_pin and data_pin.
-
Arguments:
-
int clock_pinthe pin to which your clock (D+ of the usb) is connected to. This must be an interrupt -
int data_pinthe pin to which your data (D- of the usb) is connected to.
-
-
Returns:
void
bool PS2Keyboard::has_data(void);- Description: Returns true when there is valid data in the internal buffer ready to be read.
- Arguments: none
-
Returns:
booleanWhenever there is any data to be read.
key_event PS2Keyboard::read_data(void);- Description: Read the oldest data from the internal buffer and remove it from the buffer
- Arguments: none
-
Returns:
key_eventcustom data struct
struct key_event {
int scancode; //the unique code for this key
int is_extended; //true for special keys like _up_arrow_ or _delete_ because their scancodes may overlap with the normal keys
int is_pressed; //Whenever the key is pressed (true) or released (false)
};char PS2Keyboard::key_to_char_azerty(key_event data, bool shift);- Description: Converts key data into a charracter according to the azerty (FR) layout
-
Arguments:
-
key_event datathe key data of the key you have read. -
shiftwhenever shift is currently pressed so we can change the output char
-
-
Returns:
charthe key in char form
char PS2Keyboard::key_to_char_querty(key_event data, bool shift);- Description: Converts key data into a charracter according to the querty (US) layout
-
Arguments:
-
key_event datathe key data of the key you have read. -
shiftwhenever shift is currently pressed so we can change the output char
-
-
Returns:
charthe key in char form
This library also includes some defines to easily get the scancodes for special keys like KEY_DOWN_EXTENDED
- KEY_TAB
- KEY_BACKSPACE
- KEY_ENTER
- KEY_CAPSLOCK
- KEY_LSHIFT
- KEY_RSHIFT
- KEY_ESC
- KEY_NUMLOCK
- KEY_SCROLLLOCK
- KEY_PAUSE
-
KEY_RCTRL_EXTENDED
-
KEY_RALT_EXTENDED
-
KEY_LEFT_EXTENDED
-
KEY_RIGHT_EXTENDED
-
KEY_UP_EXTENDED
-
KEY_DOWN_EXTENDED
-
KEY_INSERT_EXTENDED
-
KEY_DELETE_EXTENDED
-
KEY_HOME_EXTENDED
-
KEY_END_EXTENDED
-
KEY_PAGE_UP_EXTENDED
-
KEY_PAGE_DOWN_EXTENDED
-
KEY_KP_ENTER_EXTENDED
-
KEY_KP_SLASH_EXTENDED
-
KEY_WINDOWS_L_EXTENDED
-
KEY_WINDOWS_R_EXTENDED
-
KEY_APPS_EXTENDED
-
KEY_PRINT_SCREEN_EXTENDED
-
KEY_BREAK_EXTENDED
-
KEY_MEDIA_PLAY
-
KEY_MEDIA_NEXT
-
KEY_MEDIA_PREV
-
KEY_MEDIA_VOLUP
-
KEY_MEDIA_VOLDOWN