Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: KeyCode to character function?

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Posts
    123

    Question KeyCode to character function?

    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.

  2. #2
    Senior Member
    Join Date
    Jul 2013
    Posts
    123

    Post 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'
    }

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Location
    Boston, MA, USA
    Posts
    1,114
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •