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

Thread: Keyboard output codes

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    3

    Keyboard output codes

    Hi
    First time post , Brief intro , I consider myself a maker of sorts but I have just started delving into the Teensy , Ive completed a number of projects using the arduino range of boards, but recently realised how versatile the Teensy boards are.

    Ive created a keyboard matrix for a commodore 64 project, (trying to create a c64 type layout from scratch) the matrix has been tested and works great, providing I use normal key outputs ie 'q','w','e','r','t','y'.
    Ive been hunting around google looking for ascii codes , unicodes, scancodes etc, all the codes that ive tried seem to either not send the correct key output or nothing at all.

    Ive tried using the pre defined constants listed here https://www.pjrc.com/teensy/usb_keyboard.html and here https://www.arduino.cc/reference/en/...oardmodifiers/ but they are producing the wrong outputs.

    The keys Im having trouble with are as follows.

    Page up
    Home
    Delete
    Escape
    Left Control
    Left Shift
    Right Shift
    All cursor Keys.

    I wrote a small program which ran through numbers form 1 to 255 and sent the keystroke to that value which revealed Backspace is 0x7f, Enter 0x0a, and Tab 0x0b.

    I tried to find lists of scan codes that related to these finds but when I found some that matched , the other keys still did not give the desired output.

    Ive set my Arduino Ide up with the teensyduino setup program, set board to Teensy LC which is the board Im using, USB is set to keyboard, layout is set to UK (tried US English and US International too), optimize is set to smallest code.

    Im sure Ive not set something up correctly but Im not sure what.

    This is the code Ive adapted from a previous arduino leonardo project which had a smaller matrix.
    The matrix layout iso shown within the remarks in the script.

    The code is not complete yet , but I want to get the this whole keycode thing sorted before I proceed with that.

    Code:
    // matrix columns
    int cPins[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,14,15};
    
    // total columns
    int cPinsNo = 15;
    
    // matrix rows
    int rPins[] = {16,17,18,19,20};
    
    // total rows
    int rPinsNo = 5;
    
    //will remove not necessary for this script
    int colPrev[15][5] = {0};
    
    
    // keyboard layout                                                 
    //               0      1       2   3    4    5   6   7   8   9  10  11   12       14      15
    //
    //    16        [`]    [1]     [2] [3]  [4]  [5] [6] [7] [8] [9] [0] [-]  [=]     [HOME]  [BCKSPC]    15
    //    17        [TAB]  [Q]     [W] [E]  [R]  [T] [Y] [U] [I] [O] [P] [[]  []]     [DEL]   [PGUP]      15
    //    18        [ESC]  [A]     [S] [D]  [F]  [G] [H] [J] [K] [L] [;] [']  [#]     [RETURN][NULL]      14
    //    19        [LCTRL][LSHFT] [Z] [X]  [C]  [V] [B] [N] [M] [,] [.] [/]  [RSHFT] [UP]    [NULL]      14
    //    20        [-----------------NULL--------][SPACE][----NULL----] [LFT][DWN]   [RGT]   [NULL]      4
    //
    //
    //0x00 ==NULL [no keys]
    
    //  16   17   18   19  20
    uint8_t buttonCodes[15][5] = {
      {0x60,0x0b,0x1b,0x80,0x00},                 //[`][TAB][ESC][LCTRL]            col 0
      {0x31,0x71,0x61,0x81,0x00},                   //[1][Q][A][LSHFT]                col 1
      {0x32,0x77,0x73,0x7A,0x00},                 //[2][W][S][LFT]                  col 2
      {0x33,0x65,0x64,0x78,0x00},                 //[3][E][D][Z]                    col 3
      {0x34,0x72,0x66,0x63,0x00},                 //[4][R][F][[C]                   col 4
      {0x35,0x74,0x67,0x76,0x00},                 //[5][T][G][V]                    col 5
      {0x36,0x79,0x68,0x62,0x20},                 //[6][Y][H][B][space]             col 6
      {0x37,0x75,0x6a,0x6e,0x00},                 //[7][U][J][N]                    col 7
      {0x38,0x69,0x6b,0x6d,0x00},                 //[8][I][K][M]                    col 8
      {0x39,0x6f,0x6c,0x2c,0x00},                 //[9][O][L][,]                    col 9
      {0x30,0x70,0x3b,0x2e,0x00},                 //[0][P][;][.]                    col 10
      {0x2d,0x5b,0x27,0x2f,0xd8},                 //[-][[]['][/][LFT]               col 11
      {0x3d,0x5d,0x23,0x85,0xd9},                 //[=][]][#][RSHFT][DOWN}          col 12
      {0xd2,0x08,0x0a,0x48,0x27},            //[HOME][DEL][RETURN][UP][RGHT]   col 14
      {0x7f,0xd3,0x00,0x00,0x00}           //[BCKSPC][PGUP]         col 15
      };
    
    void setup()
    {
      //Start Serial
      Serial.begin(9600);
      Serial.println("Setting Up");
    
    
    
      for(int cPin = 0; cPin < cPinsNo; cPin++)
      {
        pinMode(cPins[cPin], OUTPUT);
        digitalWrite(cPins[cPin], HIGH);
      }
    
      for(int rPin = 0; rPin < rPinsNo; rPin++)
      {
        pinMode(rPins[rPin], INPUT);
        digitalWrite(rPins[rPin], HIGH);
      }
      Serial.println("Ready!");
    } 
    
    void loop()
    {
      // Loop through the columns
      for(int cPin = 0; cPin < cPinsNo; cPin++)
      {
        digitalWrite(cPins[cPin], LOW);
    
        // Loop through the rows
        for(int rPin = 0; rPin < rPinsNo; rPin++)
        {
          //Check if each switch is pressed
          if(digitalRead(rPins[rPin]) == LOW)
          {
            // Check to see if the state has changed since last time
            if(colPrev[cPin][rPin] == 0)
            {
              Keyboard.press(buttonCodes[cPin][rPin]);
              delay(100);
              Keyboard.release(buttonCodes[cPin][rPin]); 
    
              colPrev[cPin][rPin] = 1;
            }
          } 
          else {
    
    
            if(colPrev[cPin][rPin] == 1)
            {
              colPrev[cPin][rPin] = 0;
            }
          }
        }
        digitalWrite(cPins[cPin], HIGH);
      }
    }

  2. #2
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Found the problem
    For future ref the solution is below
    Code:
    uint8_t buttonCodes[15][5] = {
    should be
    Code:
    int buttonCodes[15][5] = {
    Then the Pre defined constants from https://www.pjrc.com/teensy/td_keyboard.html can be used.

    Was only expecting the pre- defined constants to be 1 byte but evidently not.

    Thanks.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    3
    Fully working code if anyone is interested
    Ive changed it so the key is down until released
    And it allows key modifiers.

    Code:
    // matrix columns
    int cPins[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,14,15};
    
    // total columns
    int cPinsNo = 15;
    
    // matrix rows
    int rPins[] = {16,17,18,19,20};
    
    // total rows
    int rPinsNo = 5;
    
    //will remove not necessary for this script
    int colPrev[15][5] = {0};
    
    
    // keyboard layout                                                 
    //               0      1       2   3    4    5   6   7   8   9  10  11   12       14      15
    //
    //    16        [`]    [1]     [2] [3]  [4]  [5] [6] [7] [8] [9] [0] [-]  [=]     [HOME]  [BCKSPC]   16     15
    //    17        [TAB]  [Q]     [W] [E]  [R]  [T] [Y] [U] [I] [O] [P] [[]  []]     [DEL]   [PGUP]     17     15
    //    18        [ESC]  [A]     [S] [D]  [F]  [G] [H] [J] [K] [L] [;] [']  [#]     [RETURN][NULL]     18     14
    //    19        [LCTRL][LSHFT] [Z] [X]  [C]  [V] [B] [N] [M] [,] [.] [/]  [RSHFT] [UP]    [NULL]     19     14
    //    20        [-----------------NULL--------][SPACE][----NULL----] [LFT][DWN]   [RGT]   [NULL]     20     4
    //
    //
    //0x00 ==NULL [no keys]
    
    //  16   17   18   19  20
    int buttonCodes[15][5] = {
      {0x60,KEY_TAB, KEY_ESC,MODIFIERKEY_CTRL,0x00},                 //[`][TAB][ESC][LCTRL]            col 0
      {0x31,0x71,0x61,MODIFIERKEY_SHIFT,0x00},                   //[1][Q][A][LSHFT]                col 1
      {0x32,0x77,0x73,0x7A,0x00},                 //[2][W][S][Z]                  col 2
      {0x33,0x65,0x64,0x78,0x00},                 //[3][E][D][X]                    col 3
      {0x34,0x72,0x66,0x63,0x00},                 //[4][R][F][[C]                   col 4
      {0x35,0x74,0x67,0x76,0x00},                 //[5][T][G][V]                    col 5
      {0x36,0x79,0x68,0x62,0x20},                 //[6][Y][H][B][space]             col 6
      {0x37,0x75,0x6a,0x6e,0x00},                 //[7][U][J][N]                    col 7
      {0x38,0x69,0x6b,0x6d,0x00},                 //[8][I][K][M]                    col 8
      {0x39,0x6f,0x6c,0x2c,0x00},                 //[9][O][L][,]                    col 9
      {0x30,0x70,0x3b,0x2e,0x00},                 //[0][P][;][.]                    col 10
      {0x2d,0x5b,0x27,0x2f,KEY_LEFT},                 //[-][[]['][/][LFT]               col 11
      {0x3d,0x5d,0x23,MODIFIERKEY_RIGHT_SHIFT,KEY_DOWN},                 //[=][]][#][RSHFT][DOWN}          col 12
      {KEY_HOME,KEY_DELETE,KEY_ENTER,KEY_UP,KEY_RIGHT},            //[HOME][DEL][RETURN][UP][RGHT]   col 14
      {0x7f,0xd3,0x00,0x00,0x00}           //[BCKSPC][PGUP]         col 15
      };
    
    void setup()
    {
      //Start Serial
      Serial.begin(9600);
      Serial.println("Setting Up");
    
    
    
      for(int cPin = 0; cPin < cPinsNo; cPin++)
      {
        pinMode(cPins[cPin], OUTPUT);
        digitalWrite(cPins[cPin], HIGH);
      }
    
      for(int rPin = 0; rPin < rPinsNo; rPin++)
      {
        pinMode(rPins[rPin], INPUT);
        digitalWrite(rPins[rPin], HIGH);
      }
      Serial.println("Ready!");
    } 
    
    void loop()
    {
      // Loop through the columns
      for(int cPin = 0; cPin < cPinsNo; cPin++)
      {
        digitalWrite(cPins[cPin], LOW);
    
        // Loop through the rows
        for(int rPin = 0; rPin < rPinsNo; rPin++)
        {
          //Check if each switch is pressed
          if(digitalRead(rPins[rPin]) == LOW)
          {
            // Check to see if the state has changed since last time
            
            
              Keyboard.press(buttonCodes[cPin][rPin]);
              delay(100);
               
    
              
              colPrev[cPin][rPin] = 1;
            
          } 
          else {
    
            
            if(colPrev[cPin][rPin] == 1)
            {
              Keyboard.release(buttonCodes[cPin][rPin]);
              colPrev[cPin][rPin] = 0;
            }
          }
        }
        digitalWrite(cPins[cPin], HIGH);
      }
    }

Posting Permissions

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