1

I have a Raspberry Pi 3b+ setup that controls a robot. I am looking to control it using a small wireless bluetooth button.

After much research I was able to get this button connected https://www.amazon.com/HITSLAM-Bluetooth-Remote-Control/dp/B07L9V5FLL/

I use Pynput (https://pypi.org/project/pynput/) to listen for keypresses from it, using this code:

from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

Currently when I press the Bluetooth button I get the following output:

alphanumeric key None pressed

Which I understand to mean the key isn't mapped to anything. I am able to use "None" to use the button to trigger events, so that is OK for my purposes.

Here's where I get lost. When I RELEASE the button I get:

<269025043> released

by comparison when I press a regular keyboard key like "a" I get:

alphanumberic key a pressed

au'a' released

The even more confusing thing is, when you HOLD the button down, it constantly sends the None signal (I didn't time it, but approx every 1/10th of a second) AND raises the VOLUME of the Raspberry Pi. I have no idea why or how to control that functionality.

So questions...

1) Is there any way to map the first key to something other than "None"?

2) What exactly does that 9 digit number code correspond to? It doesn't seem to be a keycode, a character or anything I can match but it is returned by key()

3) Can I stop it from autorepeating? I'm afraid I cannot if it's driver based.

4) Is there any way to figure out why the repeating signal is raising volume? I would think in order for it to have functionality like that it would need to send a signal other than None

Thanks so much for any help or insight into how to proceed next!

  • 1
    Welcome @Myka :) So I'd look into the files that make up pynput, and perhaps you'd be able to edit something to produce your desired output. – user96931 Dec 18 '19 at 22:01
1

From the pyinput documentation :

The key parameter passed to callbacks is a pynput.keyboard.Key, for special keys, a pynput.keyboard.KeyCode for normal alphanumeric keys, or just None for unknown keys.

1) Is there any way to map the first key to something other than "None"?

The type is : pynput.keyboard.Key. Key.media_volume_up and Key.media_volume_down already exists in pynput. Your print function use key.char (pynput.keyboard.KeyCode) but your volume key should be key (pynput.keyboard.Key) see my code.

2) What exactly does that 9 digit number code correspond to? It doesn't seem to be a keycode, a character or anything I can match but it is returned by key()

This is the instance id of object None

3) Can I stop it from autorepeating? I'm afraid I cannot if it's driver based.

Yes maintaining a BOOLEAN variable for button state, PSEUDO CODE:

    from pynput import keyboard

    global VOLUMEUP_pressed = False
    global VOLUMEDOWN_pressed = False

    def on_press(key):
 ,      global VOLUMEUP_pressed, VOLUMEDOWN_pressed 
        try:
            # pynput.keyboard.KeyCode
            print('Alphanumeric key {0} pressed'.format(key.char))
            # pynput.keyboard.Key (you can find volume keys here)
            print('Special Key {0} pressed'.format(key))
            # set sound up only after 1 key press...
            if key == Key.media_volume_up and not VOLUMEUP_pressed :
                VOLUMEUP_pressed = True
                VOLUME_UP()                    
            elif key == Key.media_volume_down and not VOLUMEDOWN_pressed :
                VOLUMEDOWN_pressed = True
                VOLUME_DOWN()                    
        except AttributeError:
            print('special key {0} pressed'.format(
                key))

    def on_release(key):
        global VOLUMEUP_pressed, VOLUMEDOWN_pressed 
        print('{0} released'.format(
            key))
        # release all key states
        VOLUMEUP_pressed = False
        VOLUMEDOWN_pressed = False
        if key == keyboard.Key.esc:
            # Stop listener
            return False

    # Collect events until released
    with keyboard.Listener(
            on_press=on_press,
            on_release=on_release) as listener:
        listener.join()

NOTE: Your OS check the sound outside this python script, that mean't if you press and maintain your VOLUME_UP keyboard key, the sound will be UP by the OS. If you want to control the sound in other way use another key.

4) Is there any way to figure out why the repeating signal is raising volume? I would think in order for it to have functionality like that it would need to send a signal other than None

I deduce you are using VOLUME UP and VOLUME DOWN key from your question which would explain why the repeating signal is raising volume because you have any declaration in your python code for that and why the key is None.

https://pynput.readthedocs.io/en/latest/keyboard.html

media_volume_down = 0
The volume down button.

media_volume_mute = 0
The volume mute button.

media_volume_up = 0
The volume up button.

https://pynput.readthedocs.io/en/latest/_modules/pynput/keyboard/_base.html#Key

| improve this answer | |
  • 1
    This is amazing info thanks so much! I have started to integrate it but still trying to figure out how to change the codes in #4. Will update answer after I've tried all the stuff. – Myka Dec 19 '19 at 18:15
  • @Myka, Did you read the NOTE at the bottom of the python pseudo-code ? – Ephemeral Dec 19 '19 at 20:02
  • 1
    yes but I didn't quite understand because I'm not using Volume Up keyboard key, I'm using the blueooth button which is registering as "None". I was hoping to understand better as I implemented your code and modified the files, but please correct me if I'm proceeding wrong. – Myka Dec 19 '19 at 21:40
  • @Myka, ok I think understand sorry maybe I got lost a bit. If the key is None then it is not recognized by pynput. Have you try with evdev python lib maybe ? – Ephemeral Dec 19 '19 at 23:41
  • No, I will look into it! – Myka Dec 20 '19 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.