0

No matching function for call to 'Trinket_Keyboard::pressKey
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

No matching function for call to 'Trinket_Keyboard::pressKey

by PeteH51 on Sat Jan 19, 2019 1:22 pm

Hi all,

again I'm kind of a newbie when it comes to code, but I'm slowly figuring things out. My code does what I want for one switch, but I want to eventually do three or four switches. This code, I just added the second switch but now I get the above error when compiling. I'm sure something is in the wrong place, or not included in the loop, but I can't figure this one out. Any help would be appreciated. My crude debounce delay does work.
Code: Select all | TOGGLE FULL SIZE
      /*
Trinket USB Foot Switch
 
Based on TrinketKeyboard example using the Trinket Keyboard Library
For Trinket (ATtiny85 based board) by Adafruit Industries
 
Version 1.0  2015-01-19 Initial version     Mike Barela
 
Support Adafruit tutorials by buying parts from Adafruit.com
*/
 
#include <TrinketKeyboard.h>  // Trinket keyboard library
 
const int PIN_SWITCH_0 = 0;    // Trinket pin to connect to switch 0
const int PIN_SWITCH_1 = 1;    // Trinket pin to connect to switch 1
 
void setup()
{
  // Set button pin as input with an internal pullup resistor
  // The button is active-low, they read LOW when they are not pressed
  pinMode(PIN_SWITCH_0, INPUT_PULLUP);
  pinMode(PIN_SWITCH_1, INPUT_PULLUP);
  TrinketKeyboard.begin();  // initialize keyboard library
}
 
void loop()
{
  TrinketKeyboard.poll();
  // the poll function must be called at least once every 10 ms
  // or the computer may think that the device
  // has stopped working, and give driver errors
 
  if (digitalRead(PIN_SWITCH_0) == LOW)  // If the foot switch grounds the pin
  {
    // Select what key to press when the switch is tripped
    //   Possible keys are defined in TrinketKeyboard.h
    TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_CONTROL, KEYCODE_R); // start/stop record
    TrinketKeyboard.pressKey(0, 0);  // release key
    delay(500); // debounce

    // read next switch
  }
  if (digitalRead(PIN_SWITCH_1) == LOW)
  {
    // Select what key to press when the switch is tripped
    //   Possible keys are defined in TrinketKeyboard.h
    TrinketKeyboard.pressKey(KEYCODE_SPACE);  // start/stop playback <---- no matching function for call to 'Trinket_Keyboard::pressKey(int)
    TrinketKeyboard.pressKey(0, 0);  // release key
    delay(500);
  }
}

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: No matching function for call to 'Trinket_Keyboard::pres

by adafruit_support_mike on Sun Jan 20, 2019 5:36 am

Ah.. that's a function signature thing.

For the sake of defining jargon, a function's 'signature' is what the compiler uses to allocate memory on the stack when you call the function. A function like:

Code: Select all | TOGGLE FULL SIZE
void setup ();
has a signature of:

Code: Select all | TOGGLE FULL SIZE
void( void )
meaning it doesn't take any input parameters, and doesn't return any values. A function like:

Code: Select all | TOGGLE FULL SIZE
int doSomethingWith( float arg1, uint8_t arg2 );
has a signature of:

Code: Select all | TOGGLE FULL SIZE
int( float, uint8_t )
meaning the compiler has to push one float and one uint8_t on the stack before calling the function, and has to pull one int off the stack when the function returns.

C++ lets you give multiple functions the same name as long as they all have different signatures. The practice is called 'overloading' the function name.

In this case, the function .pressKey() is overloaded with the following declarations in the header file:

Code: Select all | TOGGLE FULL SIZE
      void pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6);
      void pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5);
      void pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4);
      void pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3);
      void pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2);
      void pressKey(uint8_t modifiers, uint8_t keycode1);
which reduce to the following signatures:

Code: Select all | TOGGLE FULL SIZE
      void( uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t );
      void( uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t );
      void( uint8_t, uint8_t, uint8_t, uint8_t, uint8_t );
      void( uint8_t, uint8_t, uint8_t, uint8_t );
      void( uint8_t, uint8_t, uint8_t );
      void( uint8_t, uint8_t );
That's why we #include header files BTW.. the declarations tell the compiler what signatures are available for use.

The function call generating the error is:

Code: Select all | TOGGLE FULL SIZE
    TrinketKeyboard.pressKey(KEYCODE_SPACE);  // start/stop playback <---- no matching function for call to 'Trinket_Keyboard::pressKey(int)
which has the signature:
Code: Select all | TOGGLE FULL SIZE
      void( int );
The compiler is smart enough to convert an int to a uint8_t if necessary, but the shortest signature it can find for that function takes two parameters. The function call causing the error only has one parameter, and the compiler doesn't know what to do with that.

In much shorter terms, you forgot to include the 'modifiers' parameter in that function call.

adafruit_support_mike
 
Posts: 58825
Joined: Thu Feb 11, 2010 2:51 pm

Re: No matching function for call to 'Trinket_Keyboard::pres

by PeteH51 on Sun Jan 20, 2019 8:14 pm

OK, I get that there should be a modifier or second argument. I'll be darned if I can find out which one it is for the HOME button. I've read through TrinketKeyboard.h til I'm blue in the face. Guess I just can't read code very well yet. Any ideas what the modifier is?

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: No matching function for call to 'Trinket_Keyboard::pres

by adafruit_support_mike on Mon Jan 21, 2019 7:38 am

The modifier keys are things like shift, control, and alt.

The HID keycode for the HOME key is:

Code: Select all | TOGGLE FULL SIZE
#define KEYCODE_HOME         0x4A
It doesn't need any modifiers, so just set that parameter to zero:

Code: Select all | TOGGLE FULL SIZE
    TrinketKeyboard.pressKey( 0, KEYCODE_HOME );

adafruit_support_mike
 
Posts: 58825
Joined: Thu Feb 11, 2010 2:51 pm

Re: No matching function for call to 'Trinket_Keyboard::pres

by PeteH51 on Mon Jan 21, 2019 11:34 pm

Mike,

Thanks a lot, works perfectly!

In the interest of clarity for those searching later, how did you come up with that answer? It seems obvious, but my guess is somehow you knew the Keystroke has to be defined first, as this may be the only way to output a hex value. Maybe?

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: No matching function for call to 'Trinket_Keyboard::pres

by adafruit_support_mike on Tue Jan 22, 2019 6:37 am

It's about 40% trusting our programmers to do the right thing, and 60% learning from experience that I can trust our programmers to do the right thing.

In this case, the header "ProTrinketKeyboard.h" contains a list of predefined USB HID key codes:

https://github.com/adafruit/Pro_Trinket ... Keyboard.h

I know that function keys like HOME have their own key codes, so I skipped past the standard printing characters to the end of the file. I found it on line 157.

adafruit_support_mike
 
Posts: 58825
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.