KeyCode to character function?

Status
Not open for further replies.

wolfv

Well-known member
Is there a C/C++ function that maps KeyCode to character?
This function will run on a PC (not on Teensy).
I am writing a keyboard simulator to test keyboard software on my PC before uploading it to Teensy.

I am looking for something like KeyCode_to_char(), which does not exist as far as I know:
Code:
#include <iostream>

// key code copied from arduino-1.0.5\hardware\teensy\cores\teensy\keylayouts.h
#define KEY_A           ( 4   | 0x4000 )

int main()
{
	std::cout << KeyCode_to_char(KEY_A);
}

Thank you.
 
update

Apparently there is no such function, so I will map KeyCode to character.
Code:
// this works when compiled with C++11: g++ -std=c++11 fileName.cpp
#include <iostream>
#include <map>
#include "keylayouts.h"

class A
{
	private:
		const int key;
		typedef std::map<const int, const char> KeyMap;
		static KeyMap km;
	public:
		A(const int k): key(k) { };
		char getChar() const { return km[key]; };
};
// initialize static map out of class
A::KeyMap A::km = {{KEY_A, 'a'}, {KEY_B, 'b'}};

int main()
{
	A a(1);

	std::cout << a.getChar(); // prints 'a'
}
 
Bear in mind that the mapping of keycode to character depends on the keyboard locale. Teensy supports a fair range of Western/European keyboards andf is starting to support others, too.

For example, on a French or french Canadian keyboard, KEY_A correspons to the character "q".

You can see the list of supported keyboards from the drop down menu in Teensyduino.
 
Status
Not open for further replies.
Back
Top