0

Trinket and Media Keys?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trinket and Media Keys?

by mikeebean on Fri May 19, 2017 5:23 am

I purchased the 5V Trinket with two foot pedals to use for audio transcription. I successfully installed and tested it, and both pedals work fine, but I cannot figure out how to emulate a "Play/Pause" media keypress in Windows.

I've tried printing char(179) (hex 0xB3) as shown on many websites, as well as char(205) (hex 0xCD) which I found in the TrinketHidCombo library.

Could anyone give me an idea of what I might be doing wrong? Thanks for any advice.

mikeebean
 
Posts: 4
Joined: Fri May 19, 2017 2:10 am

Re: Trinket and Media Keys?

by adafruit_support_rick on Fri May 19, 2017 12:42 pm

You say the pedals work fine? Does that mean that they register presses on the trinket, or does that mean that they successfully transmit other virtual key codes to Windows? What virtual keycodes, if any, do work?

0xB3 should be the correct play/pause key code, according to Microsoft documentation.
https://msdn.microsoft.com/en-us/librar ... 31(v=vs.85).aspx

adafruit_support_rick
 
Posts: 34838
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Trinket and Media Keys?

by mikeebean on Fri May 19, 2017 3:55 pm

Both pedals are able to send key codes, such as the predefined ones from TrinketKeyboard.h, standard ASCII codes using char(), and text strings like "Hello world." (So I know they're wired properly and that the Trinket works.)

It seems that it's a programming issue. Apparently the media buttons are extended ASCII, and might require special handling. I also read that they are also HID "0x0C Consumer codes" instead of "0x07 Keyboard codes", but I'm not sure what that means.



Also, I'm really surprised at the amount of "key bounce" the Trinket produces. One quick push of a button generates many repeated key presses. There are a few tricks shown on the Adafruit site to help minimize bounce, but nothing seems to eliminate it.
Last edited by mikeebean on Fri May 19, 2017 4:18 pm, edited 4 times in total.

mikeebean
 
Posts: 4
Joined: Fri May 19, 2017 2:10 am

Re: Trinket and Media Keys?

by adafruit_support_rick on Fri May 19, 2017 4:01 pm

You may need to implement some additional logic beside debouncing to prevent multiple hits.

Can you post your code?

adafruit_support_rick
 
Posts: 34838
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Trinket and Media Keys?

by mikeebean on Fri May 19, 2017 5:39 pm

My code is taken from the Adafruit website. I added another input, and I changed the output. The following prints "Pedal 1" or "Pedal 2" as each pedal is pressed. (I suspect that in order to send Media Key Events, I will need to use the TrinketHidCombo library instead.)

[EDIT: I removed the code, because it was using the wrong library, and the thread was cluttered.]
Last edited by mikeebean on Sat May 20, 2017 4:39 am, edited 1 time in total.

mikeebean
 
Posts: 4
Joined: Fri May 19, 2017 2:10 am

Re: Trinket and Media Keys?

by mikeebean on Sat May 20, 2017 4:37 am

Solved it! Media Keys do indeed require the TrinketHidCombo library. I rewrote Frank Z's "Trinket USB Volume Knob" code from the Adafruit site. The new code uses one pedal connected to Pin 0 to emulate a Play/Pause key press, and the other pedal connected to Pin 2 will emulate an F7 key press (in the transcription software, F7 is an incremental rewind). Also Frank's code has a very nice de-bounce routine. I hope this will be helpful to someone else!
[EDIT: The forum software thinks Frank's last name is a banned word! lol]

Code: Select all | TOGGLE FULL SIZE
#include "TrinketHidCombo.h"
#define PEDAL1 0
#define PEDAL2 2

static char    sw1_was_pressed = 0;
static char    sw2_was_pressed = 0;

void setup()
{
  // set pins as input with internal pull-up resistors enabled
  pinMode(PEDAL1, INPUT_PULLUP);
  pinMode(PEDAL2, INPUT_PULLUP);

  TrinketHidCombo.begin(); // start the USB device engine and enumerate
}

void loop()
{
  if (digitalRead(PEDAL1) == LOW)
  {
    if (sw1_was_pressed == 0) // only on initial press, so the keystroke is not repeated while the button is held down
    {
      //TrinketHidCombo.pressMultimediaKey(MMKEY_MUTE);
    TrinketHidCombo.pressKey(0,KEYCODE_F7);
    TrinketHidCombo.pressKey(0,0);
      delay(5); // debounce delay
    }
    sw1_was_pressed = 1;
  }
  else
  {
    if (sw1_was_pressed != 0) {
      delay(5); // debounce delay
    }
    sw1_was_pressed = 0;
  }


if (digitalRead(PEDAL2) == LOW)
  {
    if (sw2_was_pressed == 0) // only on initial press, so the keystroke is not repeated while the button is held down
    {
      TrinketHidCombo.pressMultimediaKey(MMKEY_PLAYPAUSE);
    //TrinketHidCombo.pressKey(0,KEYCODE_2);
    //TrinketHidCombo.pressKey(0,0);
      delay(5); // debounce delay
    }
    sw2_was_pressed = 1;
  }
  else
  {
    if (sw2_was_pressed != 0) {
      delay(5); // debounce delay
    }
    sw2_was_pressed = 0;
  }

  TrinketHidCombo.poll(); // check if USB needs anything done
}
Last edited by adafruit_support_rick on Sat May 20, 2017 8:41 am, edited 1 time in total.
Reason: please use [code] tags when posting code or logs to the forums by clicking the </> button above the reply box and pasting your code between the tags created.

mikeebean
 
Posts: 4
Joined: Fri May 19, 2017 2:10 am

Re: Trinket and Media Keys?

by adafruit_support_rick on Sat May 20, 2017 8:42 am

Yeah - I was wondering if that was the problem.
Nice find!

adafruit_support_rick
 
Posts: 34838
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Please be positive and constructive with your questions and comments.